1

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.

1
  • Change getObject to a computed? Commented Mar 19, 2017 at 17:16

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What if getObject is really getObject(id) and my function is used within v-for block that iterates over some ids?
I've made some edition with a suggest. Also, see that link: github.com/vuejs/vue/issues/2025 This feature doesn't exist because is bad in 99% percent of the cases.
0

You should be able to do something like:

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

Then just use theObject.whatever in your templates.

2 Comments

What if getObject is really getObject(id) and my function is used within v-for block that iterates over some ids?
It sounds like each iteration should be its own component in that case. Otherwise, you could a computed property of them all and just loop through that like Edmundo suggests above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.