1. 程式人生 > >vue 報錯 Avoid mutating a prop directly since the value will be overwritten whenever the parent compo

vue 報錯 Avoid mutating a prop directly since the value will be overwritten whenever the parent compo

Vue報錯: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: “selectedPage”;

這裡發生的問題是:

子元件修改父元件的值導致報錯,這裡應該避免直接修改父元件的值。應該在data裡面重新命名selectedPage這個欄位。就可以解決問題。

import {mapState} from 'vuex'
export default {
  name: 'DynamicTabs',
  props: ['selectedPage'],
  computed: {
    ...mapState({
      tabs: state => state.aliveTabs.aliveTabs
    })
  },
  methods: {
    async initData () {
      // const datas = await getAddress()
      console.log(this.tabs)
    },
    handleRadioChange (key, keyPath) {
      console.log(key, keyPath)
      this.$emit('handleTabsChecked', key, keyPath)
      this.$router.push(key)
    },
    handleSelectedAside (key, keyPath) {
      console.log(key, keyPath)
    }
  },
  data () {
    return {
    //這裡重新命名這個selectedPage欄位,解除報錯。
    //刪除這句,就會報錯
      selectedTabsPage: this.selectedPage
    }
  },
  created () {
    this.initData()
  }
}