【问题标题】:How do I execute a method from within a vue.js component如何从 vue.js 组件中执行方法
【发布时间】:2018-10-19 09:47:49
【问题描述】:

我想在 vue.js 组件中从函数 a 内部调用函数 b

这是我的代码

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

【问题讨论】:

    标签: vue.js nested-function


    【解决方案1】:

    您可以在函数前添加thisthis.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() 变量

    【讨论】:

    • 您也可以这样做来使用您的 data() 变量——意思是?你能提供例如
    • if里面,我用了data1data2
    【解决方案2】:

    使用它会解决问题。

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

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

    Documentation reference

    【讨论】:

    • 我只是指外部方法而不是导出或 vue :)
    • 你没关系,如果不标记只是保持发布它会有所帮助:)
    猜你喜欢
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 2017-07-08
    • 2018-03-13
    • 2018-04-27
    相关资源
    最近更新 更多