方法一:直接在Vue項目的index.html 中使用全局的方式引入,js文件放public;
<script src="../xxx.js"></script> // 暴力引入
缺點:不使用該js插件的組件也會加載。
方法二:使用import 的方式導(dǎo)入
import { xxx } from '../js/xxx.js' //注意路徑
缺點:下載的本地靜態(tài)文件才可以,遠(yuǎn)程js文件不可以。
方法三:在Vue組件加載完后,手動操作DOM插入js插件
mounted() {
let script = document.createElement('script');
script.type = 'text/javascript';
script.src = '你的js文件地址';
document.body.appendChild(script);
}
方法四:使用render
export default {
components: {
'xxx-js': {
render(createElement) {
return createElement(
'script',
{
attrs: {
type: 'text/javascript',
src: '你的js文件地址',
},
},
);
},
},
},
}
// 使用 <xxx-js></xxx-js> 在頁面中調(diào)用