简体   繁体   English

我是否必须使用Meteor在每个文件中导入npm软件包并在每个文件中设置选项?

[英]Do I have to import npm packages in every file with Meteor and set options in every file?

I'm trying to get used to using npm packages brought in to Meteor 1.3 alongside regular Atmosphere packages. 我正试图习惯于将Meteor 1.3附带的npm软件包与常规Atmosphere软件包一起使用。 I have been able to use the slug package by using 我已经能够通过使用slug

meteor npm install slug

Then in one of my .js files I import slug with this command 然后在我的.js文件之一中,使用此命令导入slug

import slug from 'slug';

And it seems to work. 而且似乎可行。 But it doesn't work when I try it from another .js file. 但是当我从另一个.js文件尝试时,它不起作用。 Do I have to put the import command at the top of every file I want to use it in? 我必须在要使用导入文件的每个文件的顶部放置导入命令吗? Is this loading it multiple times in memory? 这会在内存中多次加载吗?

I'm also changing the default options using 我也在使用更改默认选项

slug.defaults.mode ='rfc3986';

And I'm wondering if I need to put that at the top of all my files as well. 我想知道是否也需要将其放在所有文件的顶部。 Atmosphere packages were a lot simpler. 大气包装要简单得多。 You simply added them and then you could use them throughout the whole project. 您只需添加它们,然后就可以在整个项目中使用它们。

If you don't want to repeat the options, use this pattern: 如果您不想重复这些选项,请使用以下模式:

Create a /lib/slug.js in your Meteor project with this content: 使用以下内容在您的Meteor项目中创建一个/lib/slug.js

import slug from 'slug';

slug.defaults.mode = 'rfc3986';

export default slug;

Then throughout your project do not import slug from 'slug'; 然后在整个项目中,不要import slug from 'slug'; but rather import slug from '/lib/slug'; 而是import slug from '/lib/slug'; .

Yes, you do have to import the module to another module to make it available there. 是的,您必须将模块导入另一个模块才能在那里使用。 Everything inside the module will not be available to other modules unless you import that module to each module. 除非您将该模块中的所有内容导入每个模块,否则其他模块将无法使用该模块中的所有内容。 Please notice the keyword. 请注意关键字。

Yes you have to import here are some benefits of using imports: 是的,您必须导入,这里有一些使用导入的好处:

  • You can control the load order of files by encoding dependencies through imports. 您可以通过对导入的依赖项进行编码来控制文件的加载顺序。
  • You can create reusable “modules”. 您可以创建可重用的“模块”。

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

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