javascript - vue中data的值改变不了
本文介绍了javascript - vue中data的值改变不了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
我在created中alert的name值和long值都是正确的,为什么在chose中name的值就是undefined,而且在created中改变了的long和short值为true,在页面中还是false
<li v-bind:class={active:long} @click='chose'>
<router-link to='/long'>第一页{{long}}</router-link>
</li>
<li v-bind:class={active:short}>
<router-link to='/short'>第二页{{short}}</router-link>
</li>
不管传过来的name是什么值,页面中的long和short都是false
import Vue from 'vue';
export default {
props: ['name'],
data() {
return {
long:false,
short:false,
}
},
methods:{
chose(){
console.log(this.name);//这里输出undefined
console.log(this.short);
}
},
created(){
var name=this.name;
console.log(name);//这里值是正确的
switch (name){
case "long":
this.long=true;
alert(that.long+"long");//当name值为long时弹出true
break;
case "short":
alert(name);
this.short=true;
break;
}
},
}
解决方案
你这代码看着逻辑很混乱,点击事件里面有页面跳转。that还没有看见你定义,this.shop也没见到,一步步认真检查下。
这篇关于javascript - vue中data的值改变不了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文