首页 > 解决方案 > 如何在vue组件外部的外部js modul文件中调用vue组件方法

问题描述

我不知道如何在外部 js 模块中调用 Vue 设置组件方法 getStageLine。

我的 vue 组件 isis 的代码示例:

const settings = {
        template: '#templateName',
        created:function(){
            this.getStageLine();

        },
        data:function(){
            return{
                test1:[],
                test:[],

            }
        },
        mounted(){

        },
        methods:{
            getStageLine:function(){
                axios.get(databaseConf.url + '/' + blaa)
                    .then(response => {

                        this.test1 = response.data

                    })
                    .catch(e => {
                        this.errors.push(e)
                    })
                axios.get(databaseConf.url + '/' + lol)
                    .then(response => {

                        this.test = response.data


                    })
                    .catch(e => {
                        this.errors.push(e)
                    })
            }

我的外部 js 模块的代码示例:

var dash = (function(){
function addSomething(data){
        console.log(data);
        $.ajax({
            url:'',
            type: 'POST',
            data: data ,
            success: function (response) {
                console.log(response.status);
                location.reload();
            },
            error: function () {
                alert("error");
            }
        });
    }
});

我想在 dash.addSomething(data) 函数内调用 Vue 组件之外的 getStageLine 方法。

我试过 $refs 调用然后 Vue.component.componentName.methods.methodName 但这没有用,所以任何建议如何在 Vue 之外调用该函数,我尝试了堆栈中的其他示例,但它们对我不起作用。

标签: javascriptfunctionvue.jsvuejs2vue-component

解决方案


推荐阅读