【问题标题】:VUE NPM import package to all other filesVUE NPM 导入包到所有其他文件
【发布时间】:2021-03-28 22:13:54
【问题描述】:
拥有 vue 3 npm 应用程序。我需要使用 jquery。于是我npm安装了jquery打包。
现在,如果我需要使用它,我就这样做:
import $ from "jquery";
这可行,但我必须在每个文件中都这样做!没有办法只在最父脚本(即 main.js 或 App.vue)中导入 jquery,而不是像那样在每个文件中导入它吗?我在每个文件中都使用 jquery。
【问题讨论】:
标签:
javascript
node.js
vue.js
npm
【解决方案1】:
将此添加到main.js 以允许使用this.$ 从任何地方访问它
import Vue from "vue";
import App from "./App.vue";
import jQuery from "jquery";
Vue.prototype.$= jQuery;
new Vue({
render: (h) => h(App)
}).$mount("#app");
【解决方案2】:
我已将此代码添加到我的 main.js 中
import jQuery from "jquery";
window.jQuery = window.$ = jQuery;
不确定这对我来说是不是最好的解决方案,一切正常。