简体   繁体   English

从url检索后,Vue2数据未在模板中更新

[英]Vue2 data not being updated in template after retrieving from url

I am using Vue.js version 2, trying to update a variable which is used in the view. 我正在使用Vue.js版本2,尝试更新视图中使用的变量。

Bear with me, I have the following code: 忍受我,我有以下代码:

new Vue({
    el: '#products',

    data: {
       showProductModal: false,
       product: {}

    },

    methods: {

      retrieveProduct(id) {
        axios.get('/product/'+id).then(function (response) {
            this.product = response.data;
            console.log(this.product);
        });
      }
  }

Please note that: 请注意:

  • The product variable is initialized as an empty object {}; product变量初始化为空对象{};
  • The "retrieveProduto()" method is called after the user clicks a button. 用户单击按钮后调用“retrieveProduto()”方法。 It then makes an API call and, after the promise resolves, updates the product variable. 然后进行API调用,并在promise解析后更新产品变量。
  • I am using axios to make the http request, and it returns a response object, where response.data is the content of the returned object. 我使用axios来发出http请求,它返回一个响应对象,其中response.data是返回对象的内容。

The console.log(this.product) results in console.log(this.product)导致

{id: 1, name: "Lemon cake", description: "Traditional lemon cake", pic_url: "", category_id: 1, …} {id:1,名称:“柠檬蛋糕”,描述:“传统柠檬蛋糕”,pic_url:“”,category_id:1,...}

which is exactly the expected output. 这正是预期的产出。 However, upon calling {{ product }} in the view, the template is not updated in any way, and gets stuck forever in {}. 但是,在视图中调用{{product}}后,模板不会以任何方式更新,并永远停留在{}中。

I have also tested if two-way databinding is working, and the answer is yes. 我还测试了双向数据绑定是否有效,答案是肯定的。 Actually, everything is working perfectly apart from this. 实际上,一切都与此完全不同。 Am I doing something wrong? 难道我做错了什么? Should I expect that the template is not updated after retrieving an object from a promise? 从promise中检索对象后,我是否应该期望模板未更新?

Any help is appreciated. 任何帮助表示赞赏。 Thank you in advance. 先感谢您。

EDIT: 编辑:

  • The part of the template in which I call the variable is just a {{product}} for testing purposes. 我称之为变量的模板部分只是用于测试目的的{{product}}。
  • The reason why I am calling self is that I was using this.product in my first attempts. 为什么我打电话自我的原因是,我在我的第一次尝试使用this.product。 I have seen this solution elsewhere and changed it so I could give it a shot. 我已经在其他地方看到了这个解决方案并改变了它,所以我可以试一试。 Since it didn't work, I am changing it back to this 由于它不起作用,我正在改回
  • Printing console.log(this) returns the complete Vue object, and it has the product property set correctly, but still not reflecting in the template. 打印console.log(this)返回完整的Vue对象,并且正确设置了product属性,但仍未在模板中反映。

You could be running into issues with self , whether due to lack of declaration or scoping issues. 无论是由于缺乏声明还是范围问题,您都可能遇到self问题。 Try the following instead: 请尝试以下方法:

retrieveProduct(id) {
    var this_vue_instance = this;
    axios.get('/product/'+id).then(function (response) {
        this_vue_instance.product = response.data;
        console.log(this_vue_instance.product);
    });
}

I think it should be some issues with scoping. 我认为这应该是范围界定的一些问题。 Try this: 尝试这个:

  retrieveProduct(id) {
    axios.get('/product/' + id).then(response => {
      this.product = response.data;
    });
  }

And then use vue-devtools to check if the data is properly changed. 然后使用vue-devtools检查数据是否正确更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM