[英]How to use dependency in Vue component?
I'm mostly a backend dev but I now need to implement something in an existing vue codebase. 我主要是后端开发人员,但现在需要在现有的vue代码库中实现某些功能。 In a file called
myModal.vue
I need to use a this cron-parser js lib . 在名为
myModal.vue
的文件中,我需要使用此cron-parser js lib 。 When I use it in the app.js
file it works perfectly: 当我在
app.js
文件中使用它时,它可以完美运行:
import CronParser from 'cron-parser';
let interval = CronParser.parseExpression('*/2 * * * *');
console.log(interval.next().toString()); // logs a correct datetime
So I now want to pass this CronParser to the myModal.vue
file. 因此,我现在想将此CronParser传递给
myModal.vue
文件。 So following an existing injection of HighCharts I added the third of these 4 lines: 因此,在现有的HighCharts注入之后,我添加了这4行中的第三行:
Vue.prototype.$eventHub = new Vue();
Vue.use(HighchartsVue);
Vue.use(CronParser);
let myModal = require('./components/myModal.vue');
// and some more components
Then in myModal.vue
I use the same code: 然后在
myModal.vue
,使用相同的代码:
let interval = CronParser.parseExpression('*/2 * * * *');
console.log(interval.next().toString());
But I now get 但是我现在得到
"ReferenceError: CronParser is not defined"
I'm kinda lost on where I'm going wrong here though. 不过,我有点迷失在哪里。 Could anybody hint me in the right direction
任何人都可以向我暗示正确的方向
CronParser
is not a vue library, you wouldn't use it as such Vue.use(CronParser);
CronParser
不是vue库,您不会像Vue.use(CronParser);
这样使用它Vue.use(CronParser);
Instead, just make sure you import it in your myModal.vue
. 相反,只需确保将其导入到
myModal.vue
。 It looks like you may be missing import CronParser from 'cron-parser';
看来您可能缺少
import CronParser from 'cron-parser';
in there. 在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.