data动态获取props 中的数据
props是在开发时,预先设置好,传给组件的。在运行过程中,这些设置是一锤子买卖,以后就不起作用了。假如我们想调用该组件的一个方法,而该方法又依赖于props,问题就出来了:props是旧的。
在组件内部,应该依赖于data,而data的初始值来自于props
当前data 想从props 进行初始化,
data(){
return {
currentindex:this.titles[0]
}
}
,
props:{
titles:{
type:Array,
default:[]
},
},
此时currentindex是undefind,就算后面props 有更新,currentindex 也不会刷新

可以用到watch
watch:{
titles:function (news) {
this.currentindex=news[0]
}
}

浙公网安备 33010602011771号