1

我正在尝试在 vuejs 2 中使用 nextTick() 返回一些数据,如下所示

getProperty() {
   this.$nextTick(function() {
      return 'hello';
   });
}

它不起作用。有什么线索吗?

4

1 回答 1

4

this.$nextTick这个函数不返回任何东西;它只是在刷新所有新数据后执行您的回调。

所以如果你想设置一些标志或数据,你可以使用模态/变量。

new Vue({
  data: {
    msg: 'hello'
  },
  methods: {
    someTask: function () {
      this.msg = 'hello next tick';
      this.$nextTick(function() {
        this.printVar();
      });
    },
    printVar: function() {
      // here this variable will be changed to latest value
      // or call another function where this value is used
      // this.anotherFunction();
      console.log(this.msg);
    }
  },   
  ready: function () {
    this.someTask();
  }   
});

或者只是让我们知道您想用它做什么,以便我们为您提供更好的答案。

于 2016-11-04T05:53:31.430 回答