如何从 vue.js 组件中执行方法

How do I execute a method from within a vue.js component

我想在 vue.js 组件

中从函数 a 内部调用函数 b

这是我的代码

   methods:{
      a(){ 
            console.log("a")
            b();
       }
      b(){
           console.log("b")
       }
    }

你可以通过在函数前添加 this 来实现:this.yourFunction

export default{
    data(){
        return{
            data1: 1,
            data2: 1
        }
    },
    methods:{
        a(){
            if(this.data1 == this.data2){
                this.b(); //call b() function
            }
        },
        b(){
            //do something
        },
    }
}

您也可以使用您的 data() 变量

用这个就可以解决问题

 methods:{
  a(){ 
        console.log("a")
        this.b();
   }
  b(){
       console.log("b")
   }
}

如果您想在方法外调用它,请使用 this.method()

Documentation reference