Vue: 通过props绑定数据到子组件,视图不会自动更新

Created on 24 May 2016  ·  2Comments  ·  Source: vuejs/vue

我通过v-bind绑定了一个对象到子组件,也就是以下的psychologist-list,并且在子组件的props也声明了
users。users通过restapi获取,并绑定到vm.$data上。但是这时候子视图并没有更新视图。但是
当我在父组件中用v-if去切换这个子组件的显示与否的时候,这个子组件就被渲染出来了。
这让我想到angualr1.x中的$scope.$apply(),手动去作脏检查来更新视图。但是我了解Vue是通过setter
和getter的方式来实现视图更新的,但是我在api文档中也没有找到合适的方式来解决我碰到的这个问
题。

附上代码片段:
父组件:{
html:

js:
ready: function() {
var self = this;
UserService.psychologistsFollowedByUser(self.$route.params.userId, function(result) {
self.users = result.data;
}, function(error) {

});

}
}

子组件:{
html:
js: props: {
users:[]
}
}

Most helpful comment

十之八九是因为你没有在父组件的 data 里面声明 users。请仔细看文档并学会如何用 markdown 贴代码。

All 2 comments

十之八九是因为你没有在父组件的 data 里面声明 users。请仔细看文档并学会如何用 markdown 贴代码。

使用watch就可以解决

Was this page helpful?
0 / 5 - 0 ratings

Related issues

6pm picture 6pm  ·  3Comments

lmnsg picture lmnsg  ·  3Comments

paceband picture paceband  ·  3Comments

franciscolourenco picture franciscolourenco  ·  3Comments

gkiely picture gkiely  ·  3Comments