skyboyer
skyboyer

Reputation: 23705

how to avoid reference error in directives

Is there a way to avoid long checks in v-if(and other directives but mostly it's about v-if) like:

... v-if="user && user.data && user.data.password"

?

I mean if user is null then I would get reference error without such checks.

Is it possible to register own modifier all over app? Something like

... v-if.noDataAsFalse="user.data.password" 

I can use global helper like lodash's _.get() but want to find more declarative way to achieve the same results.

Upvotes: 2

Views: 49

Answers (1)

8bit
8bit

Reputation: 597

I think this is the most straightforward way of doing this in Vue at the moment.

In Angular 5 (yes, 5 is here) they have a so called elvis operator (object?.prop?.anotherProp), which allows for safe object traversal.

This idea was brought up on the Vue Github page, but the creator of Vue has some solid arguments against it. You can read more here.

Upvotes: 1

Related Questions