vue2.5.16 警告: 组件内不推荐修改Propsvoid mutating a prop directly since the value will be overwritten whenev

场景

给组件传递参数设置 props : ['question_lists'], .getJsonquestionlists . g e t J s o n 获 取 到 了 q u e s t i o n l i s t s 的 数 据 在 .getJson的的回调函数中 自然需要将得到的数据赋值给question_lists
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: "question_lists"

分析

1. 翻译:  避免直接修改prop属性, 因为父级在重新加载的时候  prop也会被复写
2. Vue从1.x升级到2.x之后 就不推荐组件内直接修改  推荐使用data 或者computed来解决 
    其实只需要在组件内部data 声明一个空的属性就可以了

参考文档

https://cn.vuejs.org/v2/guide/migration.html#%E4%BF%AE%E6%94%B9-Props-%E5%BC%83%E7%94%A8

解决

    <script>
        Vue.component('list_question_template', {
            template : '#list_question',
            // props : ['question_lists'],
            data : function(){
                return {
                    question_lists : {}
                 }
            },
            created : function () {
                var vm = this;
                var url = '/api/test';
                $.getJSON(url).done(function(response){
                    vm.question_lists = response;
                });
            },
            methods : {
                deleteItem : function (question) {
                    var index = this.question_lists.indexOf(question);
                    this.question_lists.splice(index, 1)
                }
            }
        });

        new Vue({
            el: '#app_test'
        });
    </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值