简体   繁体   English

用Vue导入外部js

[英]Import external js with Vue

Say I have 2 js files in resources/assets/js , one is app.js & the other is ext_app.js 假设我在resources/assets/js有2个js文件,一个是app.js ,另一个是ext_app.js

There is a function in ext_app.js as below: ext_app.js有一个函数,如下所示:

function testFunction() {
    // function code
}

And in app.js : 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();
    }
});

Ran npm run dev & look into public/js/app.js , the ext_app.js code is there, pretty good anyway. npm run dev并查看public/js/app.jsext_app.js代码在那里,无论如何都很好。 But, the app returns the following error when run on Chrome: 但是,该应用在Chrome上运行时返回以下错误:

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

What have I miss? 我想念什么?

You need to export the testFunction before you can require it. 您需要先导出testFunction,然后才能使用它。

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

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

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