vue 报错 Avoid mutating a prop directly since the value will be overwritten whenever the parent compo

本文解决Vue中子组件修改父组件Prop值的错误实践,介绍如何通过data或computed属性正确响应Prop变化,避免程序运行时错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()
  }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值