Avoid mutating a prop directly since the value will be overwritten whenever the parent component

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: “isShowData”;

原因: 直接在子组件修改了父组件的值
解决办法

在子组件不要修改父组件的值,用一个参数接收父组件的值,如下:
父组件:

<template>
    <Card>
        <child-properties-tab :cityList="cityList"
                                  @on-after-save="afterSave"/>
    </Card>
</template>
<script>
 export default {
        name: "child-properties-tab",
        data() {
            return {
                 cityList: [
                    {
                        value: 'New York',
                        label: 'New York'
                    },
                    {
                        value: 'London',
                        label: 'London'
                    }
                ],
            }
        },
      }
</script>

子组件:

<template>
    <div class="meta main" >
     <Select v-model="model1" style="width:200px">
        <Option v-for="item in cityList" :value="item.value" 
        :key="item.value" @on-change="onChange">{{ item.label }}</Option>
    </Select>

    </div>
</template>
<script>
 export default {
        name: "child-properties-tab",
        props: [
          "cityList"
        ]
         data() {
            return {           
                model1: ''
               }
        },
        methods: {
             onChange (v) {
                 //这里修改了父组件传的值,就会报错
                 if(v == 'London') {
                    this.cityList = [
                    {
                        value: 'New York',
                        label: 'New York'
                    },
                    {
                        value: 'London',
                        label: 'London'
                    },
                    {
                      value: 'shanghai',
                      label: 'shanghai'
                    }
                   ];
                 }
             }
        }
      }
</script>

上面的运行就会报错,修改子组件:

<script>
 export default {
        name: "child-properties-tab",
        props: [
          "cityList"
        ]
         data() {
            return {         
                model1: '',
                cityData: this.cityList
               }
        },
        methods: {
             onChange (v) {
                 if(v == 'London') {
                    this.cityData = [......]; //...是省略的内容
                 }
             }
        }
      }
</script>

或者:computed

<script>
 export default {
        name: "child-properties-tab",
        props: [
          "cityList"
        ]
         data() {
            return {
               cityData: [],
                model1: ''
               }
        },
        computed: {
            cityData() {
               return this.cityList;
             }
        },
        methods: {
             onChange (v) {
                 if(v == 'London') {
                    this.cityData = [......]; //...是省略的内容
                 }
             }
        }
      }
</script>

watch

 export default {
        name: "child-properties-tab",
        props: [
          "cityList"
        ]
         data() {
            return {
                cityData: [],
                model1: ''
            }
        },
        watch: {
            cityList(v) {
               this.cityData = val;
             }
        },
        methods: {
             onChange (v) {
                 if(v == 'London') {
                    this.cityData = [.....]; //...是省略的内容
                 }
             }
        }
      }
</script>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值