我一开始可以很好地加载数据,并且我有一些函数可以成功调用以修改字典。但是,直到我重新加载页面,Vue才会显示更新。我已经阅读了文档,似乎我的字典应该是被动的,因为它最初是在数据字段中设置的。我是在做错什么还是需要手动触发刷新?
示例用法是调用add_link
的按钮。
代码:
var app = new Vue({
el: '#app',
data: {
domains: null,
},
mounted () {
this.update()
},
methods: {
update(){
axios.get('http://localhost:5000/list').then(response => (this.domains = response.data));
},
add_link(domain,tag,url) {
axios.get('http://localhost:5000/addlink?domain='+encodeURIComponent(domain)+'&tag='+encodeURIComponent(tag)+'&url='+encodeURIComponent(url))
this.update()
},
remove_link(domain,tag) {
axios.get('http://localhost:5000/removelink?domain='+encodeURIComponent(domain)+'&tag='+encodeURIComponent(tag))
this.update()
}
}
});
由于axios
返回一个承诺,您要么需要利用async await
即
async add_link(domain,tag,url) {
await axios.get('http://localhost:5000/addlink?domain='+encodeURIComponent(domain)+'&tag='+encodeURIComponent(tag)+'&url='+encodeURIComponent(url))
this.update()
},
或只是将this.update
放入then
add_link(domain,tag,url) {
axios.get('http://localhost:5000/addlink?domain='+encodeURIComponent(domain)+'&tag='+encodeURIComponent(tag)+'&url='+encodeURIComponent(url))
.then( () => this.update() )
},