VueJS accessing a method from another method

后端 未结 3 1998
野趣味
野趣味 2020-12-05 05:59

I\'m using VueJS to make a simple enough resource management game/interface. At the minute I\'m looking to activate the roll function every 12.5 seconds and use

3条回答
  •  一生所求
    2020-12-05 06:52

    You can access these methods directly on the VM instance, or use them in directive expressions. All methods will have their this context automatically bound to the Vue instance.

    – Vue API Guide on methods

    Within a method on a Vue instance you can access other methods on the instance using this.

    var vm = new Vue({
      ...
      methods: {
        methodA() {
          // Method A
        },
        methodB() {
          // Method B
    
          // Call `methodA` from inside `methodB`
          this.methodA()
        },
      },
      ...
    });
    

    To access a method outside of a Vue instance you can assign the instance to a variable (such as vm in the example above) and call the method:

    vm.methodA();
    

提交回复
热议问题