简体   繁体   English

Vue - 导入没有导出的 npm package

[英]Vue - Import npm package that has no exports

I am trying to import an npm package into a Vue component.我正在尝试将 npm package 导入 Vue 组件。

The package (JSPrintManager - here ) is just a JavaScript file with no exports . package(JSPrintManager -这里)只是一个没有导出的 JavaScript 文件。 Consequently, I cannot use:因此,我不能使用:

import { JSPM } from "jsprintmanager"

I have also had no success with the following:我在以下方面也没有成功:

import JSPM from "jsprintmanager"

import * as JSPM from "../../node_modules/jsprintmanager/JSPrintManager"

import * as JSPM from "../../node_modules/jsprintmanager/JSPrintManager.js"

Am I barking up the wrong tree?我在吠叫错误的树吗?

If there is no way to import an npm package that is not a module, is there another way to load the relevant JavaScript file (currently residing in my node-modules directory) into my component?如果无法import不是模块的 npm package ,是否有另一种方法可以将相关的 JavaScript 文件(当前驻留在我的 node-modules 目录中)

I am using the Vue CLI我正在使用 Vue CLI

jspm.plugin.js jspm.plugin.js

import * from '../../node_modules/jsprintmanager/JSPrintManager';

export default {
  install(Vue) {
    Vue.prototype.$JSPM = window.JSPM
  }
}

main.js main.js

import Vue from 'vue'
import JSPM from './jspm.plugin';

Vue.use(JSPM);

In any of your components you can now access JSPM as this.$JSPM在您的任何组件中,您现在都可以访问JSPM作为this.$JSPM

If you want to use it outside of your components (say, in store) and you want it to be the same instance as the one Vue uses, export it from Vue, in main.js如果您想在组件之外使用它(例如,在商店中)并且您希望它与 Vue 使用的实例相同,请在main.js中从 Vue 导出它

const Instance = new Vue({
  ...whatever you have here..
}).$mount('#app');

export const { $JSPM } = Instance

Now you can import { $JSPM } from '@/main' anywhere.现在您可以在任何地方import { $JSPM } from '@/main'


That would be the Vue way.那将是 Vue 的方式。 Now, in all fairness, the fact your import is run for the side effect of attaching something to the window object which you then inject into Vue is not very Vue-ish.现在,平心而论,您的导入是为了将某些东西附加到 window object 然后您将其注入 Vue 的副作用而运行的这一事实并不是非常 Vue-ish。 So the quick and dirty way to do it would be, in your component:因此,在您的组件中,快速而肮脏的方法是:

import * from '../../node_modules/jsprintmanager/JSPrintManager';

export default {
  data: () => ({
    JSPM: null
  }),
  mounted() {
    this.JSPM = window.JSPM;
    // this.JSPM is available in any of your methods 
    // after mount, obviously
  }
}

The main point of the above "simpler" method is that you have to make the assignment after the page finished loading and running the JSPM code (and window.JSPM has been populated).上述“更简单”方法的要点是,您必须在页面完成加载并运行 JSPM 代码(并且window.JSPM已填充)之后进行分配。

Obviously, if you disover it sometimes fails (due to size, poor connection or poor hosting), you might want to check window.JSPM for truthiness and, if not there yet, call the assignment function again after in a few seconds until it succeeds or until it reaches the max number of tries you set for it.显然,如果您发现它有时会失败(由于大小、连接不良或托管不良),您可能需要检查window.JSPM的真实性,如果还没有,请在几秒钟后再次调用分配 function 直到成功或者直到它达到您为其设置的最大尝试次数。

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

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