报错,props 不可重写,需要复制给data重新定义

报错:[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: "result" (found in component )

项目中遇到父组件传值 activeIndex

<Tabs :tabs="tabs" :activeIndex="activeIndex" ></Tabs>
<script >
export default{
 updated(){
          let currentRoute=this.$route.name;
           var arr=Array.from(this.$store.state.app.tabs);
           if(arr.indexOf(currentRoute)!=-1){

             this.activeIndex=this.$store.state.app.activeIndex=arr.indexOf(currentRoute).toString();
           }

        }
}
</script>

子组件接收该值

<template>
      <el-tabs type="card" v-model="activeIndex"    >
        <el-tab-pane v-for="(item,index) in tabs" :label="item"  :closable="index==0?false:true" :name="index.toString()"  ></el-tab-pane>
      </el-tabs>
</template>

<script>
  export default{
      data(){
            return {
              tabs:[],
          }
        },

      props:['activeIndex']

    }
</script>

6ebadbe553e2f174006b.jpg

参考网址https://juejin.im/entry/5843abb1a22b9d007a97854c
由于父组件updated()方法中更改了this.activeIndex值,update方法除了父组件触发这个方法,子组件也会触发,即子组件会更改activeIndex的值,但是由于父子组件的传递机制,会造成报错Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders....
因此在子组件使用该值时需要经过新变量(currentIndex)重新传递,这样当这个值变更时,不会造activeIndex的更改

//v-model绑定更改
<el-tabs type="card" v-model="currentIndex"  >   
</el-tabs>
<script>
  export default{
      data(){
            return {
              tabs:[],
              currentIndex:this.activeIndex //currentIndex接收父组件传来的activeIndex值;
          }
        },

      props:['activeIndex']

    }
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值