Avoid mutating a prop directly since the value will be overwritten

前端 未结 11 918
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 01:07

I have very common problem with upgrading to Vue 2.0

I am getting warning:

Avoid mutating a prop directly since the value will be overwritte

11条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 01:43

    Vue.js consider this as anti-pattern. For example, declaring and setting some props like

    this.propsVal = 'new Props Value'
    

    So to solve this issue you have to take in value from the props to data or computed property of Vue instance. like...

    props: ['propsVal'],
    data: function() {
       return {
           propVal: this.propsVal
       };
    },
    methods: {
    ...
    }
    

    and you can use your props value like normally.

提交回复
热议问题