Vue.js props copy and reactivity

一笑奈何 提交于 2021-02-11 12:29:34

问题


Here's my problem: I have a v-for loop on a component. The v-for loop is based on an array filtered by a search term entered in an input and returned by a computed.

The child component displays some data based on a copy of past props because I need to modify them.

The filtering works well but the content of the child components doesn't update correctly, being based on a copy of the props.

Here is a minimalist example: https://codesandbox.io/s/friendly-engelbart-8iu4u?file=/src/App.vue

You can see in the console that the filtering is good, but the visual result is not (try filtering for example with the letters "ba"). How can I combine reactivity and props copy in a component ?


回答1:


This caused by the :key isn't changing when you change the data since the :key value is based on the v-for index, not unique value that represent each user data.

Try adding id to the user object, and use it as the :key to make sure that each user data has its own unique :key

users: [
    {
        id: 1,
        lastname: "Einstein",
        firstname: "Albert"
    },
    ...
]

Demo: https://codesandbox.io/s/jolly-sun-optlr?file=/src/App.vue



来源:https://stackoverflow.com/questions/61804086/vue-js-props-copy-and-reactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!