猿问

关于Vue的computed方法中set方法疑问

我的理解是set方法会将data里面的原始数据改掉啊,但是为啥监控发现并没有变化,而且set和get打个对调,get输出的值还是zhangsan

HTML代码:


<div id="app"> 

    <span>{{fullName2}}</span>

</div>

JS代码:

   var app=new Vue({

        el:"#app",

        data:{

            firstName:"zhang",

            lastName:"san",

            fullName:"",

                  },

        computed:{

            fullName2:{

                get:function(){

                    return this.fullName=this.firstName+this.lastName;

                },

                set:function(){

                    this.firstName="Xiao";

                    this.lastName="Ming";

                },

            }

        },

    });


呼啦一阵风
浏览 1809回答 1
1回答

慕少森

你应该想要的是这样的吧:<div id="app">&nbsp;&nbsp; &nbsp; <span>{{fullName}}</span></div>var app=new Vue({&nbsp; &nbsp; &nbsp; &nbsp; el:"#app",&nbsp; &nbsp; &nbsp; &nbsp; data:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstName:"zhang",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastName:"san"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; computed:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fullName:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get:function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.firstName+this.lastName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set:function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.firstName="Xiao";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.lastName="Ming";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; });
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答