【问题标题】:Import external js with Vue使用 Vue 导入外部 js
【发布时间】:2018-07-27 10:46:01
【问题描述】:

假设我在resources/assets/js中有2个js文件,一个是app.js,另一个是ext_app.js

ext_app.js中有一个函数如下:

function testFunction() {
    // function code
}

app.js:

require('./bootstrap');
require('./ext_app.js');

const app = new Vue({
    // other stuff

    mounted: function() {
        // Call my test function from ext_app.js
        testFunction();
    }
});

运行npm run dev 并查看public/js/app.jsext_app.js 代码在那里,无论如何都很好。但是,该应用在 Chrome 上运行时返回以下错误:

[Vue warn]: Error in mounted hook: "ReferenceError: testFunction is not defined"

我错过了什么?

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

    您需要先导出 testFunction,然后才能使用它。

    module.exports = function testFunction() {
       // function code
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 2021-02-05
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      • 2022-01-09
      • 2021-01-13
      • 2022-01-22
      • 1970-01-01
      相关资源
      最近更新 更多