繁体   English   中英

用Vue导入外部js

[英]Import external js with Vue

假设我在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"

我想念什么?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM