Why does my Vue component require :key?

后端 未结 4 1185
渐次进展
渐次进展 2020-12-11 18:16

I have a small Vue.js component which displays a favorite star icon. Clicking on the icon favorites/unfavorites the element. So far I have only implemented the UI part, whic

4条回答
  •  再見小時候
    2020-12-11 18:45

    Ok I think the problem here is that you're changing your root data object. To preserve reactivity, you shouldn't change the root data object after you've instantiated Vue.

    Here is your code in a simple Vue. I didn't need :key to make it work. I would keep :key for inside loops.

    markup

    
    

    code

    vm = new Vue({
      el : "#vueRoot",
      data() {
        return { store :{
          favorite: true
        }}
      },
      mounted() {
      },
      methods: {
        toggleFavorite() {
          this.store.favorite = !this.store.favorite
        }
      }
    }
    );
    

    This is a working example with minimal changes. From what you've showed us, you should just have element, then do what you want with a dynamic class list, like...

    
    

提交回复
热议问题