Javascript vuejs从承诺更新模板中的值

Javascript vuejs从承诺更新模板中的值,javascript,vue.js,Javascript,Vue.js,我是VueJS新手,发现缺少文档,但我正在尝试使用一个组件,该组件将默认值设置为comment count为0,但当承诺返回值时,我希望在视图中更新该值 Vue.component('commentsCount', { template: ` <strong class="t-bold">{{count}}</strong> `, data: () => ({ count: 0 }), created: () =>

我是VueJS新手,发现缺少文档,但我正在尝试使用一个组件,该组件将默认值设置为comment count为0,但当承诺返回值时,我希望在视图中更新该值

Vue.component('commentsCount', {
    template: `
      <strong class="t-bold">{{count}}</strong>
    `,
    data: () => ({ count: 0 }),
    created: () => {
      this.count = Review.stats(productCode)
        .then((res) => {
          console.log(res.count);
          return res.count;
        });
      console.log(this);
    },
  });
Vue.component('commentsCount'{
模板:`
{{count}}
`,
数据:()=>({count:0}),
创建:()=>{
this.count=Review.stats(productCode)
。然后((res)=>{
console.log(res.count);
返回res.count;
});
console.log(this);
},
});
我不确定我做错了什么,但这给了我以下的错误,我已经做了一个小时了

[Vue warn]:创建的挂钩中出错:“TypeError:无法设置属性 未定义的“计数”

发现于

--->

这一错误出现在
this.count=Review.stats(productCode)

TypeError:无法设置未定义的属性“count”


您应该在
中设置该值,然后在
回调中设置该值。也不要使用箭头函数来创建
(和其他方法),否则您将丢失此
的上下文。试试这个:

Vue.component('commentsCount', {
  template: `
    <strong class="t-bold">{{count}}</strong>
  `,
  data: () => ({ count: 0 }),
  created () {  // can't be an arrow function
    Review.stats(productCode)
      .then((res) => {
        console.log(res.count);
        this.count = res.count;  // set count here
      });
    console.log(this);
  },
});
Vue.component('commentsCount'{
模板:`
{{count}}
`,
数据:()=>({count:0}),
已创建(){//不能是箭头函数
Review.stats(产品代码)
。然后((res)=>{
console.log(res.count);
this.count=res.count;//在此处设置计数
});
console.log(this);
},
});

我在
this.count=res.count
>Uncaught(承诺中)类型错误上遇到以下错误:无法设置未定义的属性'count',不要使用箭头函数来
创建()
实际上取消了此操作,我不得不将布局更改为
创建(){