W
W
WebDev2018-06-15 13:06:19
Vue.js
WebDev, 2018-06-15 13:06:19

How to call a method when data changes?

<ul>
    <li v-for="user in users">{{ showName(user) }}<li>
</ul>

...
methods: {
    showName(user) {
        if (user.name && !user.lastname) {
            return 'Some value';
        } else if (...) {
            ...
        } else {
            return user.name;
        }
    }
}
...

In the code above, some value in the users list may change, but the showName method will not be called again. What to do? Filters can be used when I pass a specific value, but here I have an object and, by condition, I return different fields of the object.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Kulakov, 2018-06-15
@kulakoff Vue.js

Should change, add key:

<li v-for="user in users" :key="user.id">{{ showName(user) }}<li>

H
hopeful_romantic, 2018-06-15
@hopeful_romantic

try computed

computed: {
        showName(user) {
            if (user.name && !user.lastname) {
                return 'Some value';
            } else if (...) {
            ...
            } else {
                return user.name;
            }
         }
      }

L
Little Vasya, 2018-06-15
@emilov

create() hook

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question