Terence Chow
Terence Chow

Reputation: 11153

vue how to assign value in html?

This is my usecase:

<div v-if="getObject()">
    <div v-if="getObject().someBoolean">
        {{getObject().someOtherKey}}
    </div>
</div>

I don't want to be calling getObject everywhere so I'm wondering if there is a way to simply assign a value after calling getObject and then reusing that within the same div?

Note I can't use v-for since it iterates over the keys, and in my example I need 2 keys in the same iteration.

Upvotes: 1

Views: 2053

Answers (2)

Edmundo Santos
Edmundo Santos

Reputation: 8287

Use the return function of getObject as a computed property, then access it later.

Edit:

data: {
   ids: [],
},
computed: {
   objects() {
     return this.ids.map(getObject);
   }
}

Then you iterate over objects, instead your ids.

Upvotes: 2

Bill Criswell
Bill Criswell

Reputation: 32921

You should be able to do something like:

computed: {
  theObject () {
    return this.getObject()
  },
},

Then just use theObject.whatever in your templates.

Upvotes: 0

Related Questions