通过axios更改数据后,Vue不更新

问题描述 投票:0回答:1

我一开始可以很好地加载数据,并且我有一些函数可以成功调用以修改字典。但是,直到我重新加载页面,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()
        }
    }
});
vue.js
1个回答
0
投票

由于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() )

    },
© www.soinside.com 2019 - 2024. All rights reserved.