<template>
<div id="test">
<counter v-if="modelLock"></counter>
</div>
</template>
<script>
import Vue from 'vue'
export default {
name:'Test',
data(){
return {
modelLock:false,
obj:{},
templateModal:''
}
},
created() {
this.obj={
list:[
{ text:11111 },
{ text:22222 },
{ text:33333 }
]
}
this.templateModal ='<div><span v-for="item in data.list">{{item.text}}</span></div>'
this.templateInit(this.obj, this.templateModal)
var that = this
setTimeout(function(){
that.obj={
list:[
{ text:4444 },
{ text:5555 },
{ text:6666 }
]
}
},3000)
},
methods:{
templateInit(data,temp){
Vue.component("counter",{
data:function(){
return{
data: data
}
},
template:temp,
methods:{
showFn(item){
alert(item.text)
}
}
})
this.modelLock = true
}
}
}
</script>
如题上面这种写法无法双向绑定
that.obj={
list:[
{ text:4444 },
{ text:5555 },
{ text:6666 }
]
}
而这种写法却可以
that.obj.list =[
{ text:4444 },
{ text:5555 },
{ text:6666 }
]
这样写能双向绑定,有什么办法让这种写法也能双向绑定
that.obj={
list:[
{ text:4444 },
{ text:5555 },
{ text:6666 }
]
}
因为我obj里的数据从接口获取是未知的,现在组件里的数据只绑定一次
求指教
可以通过Vue 提供的
set
方法实现,Vue set