VueJs避免对在创建的生命周期挂钩上初始化的道具进行突变

时间:2019-05-09 15:04:10

标签: vuejs2 vue-component

我是VueJ的新手,我有一个props属性,并在创建的钩子上获取了属性数据。

但是我在控制台上收到此消息:

[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. 
Instead, use a data or computed property based on the prop's value. 
Prop being mutated: "markets"

我在道具部分声明市场

 props: {
      markets: Object           
    },

我在创建的钩子处获得了属性数据:

created() {

        Axios.get('/api')
          .then(response => {
            this.markets = response.data
          })
          .catch(error => {
            console.log('There was an error:', error.response)
          }),

我如何适应这种情况以停止警告消息?

1 个答案:

答案 0 :(得分:0)

official docs中所述,应按以下步骤进行操作:

props: {
   markets: Object           
},
data: function () {
  return {
    innerMarkets: this.markets
  }
}