为什么我不能在我的Meteor源文件中“导入”`anti:fake`? [英] Why don't I have to `import` `anti:fake` into my Meteor source files?

查看:101
本文介绍了为什么我不能在我的Meteor源文件中“导入”`anti:fake`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些类似的问题,但似乎没有一个适用于我的情况。



我正在创建一个Meteor项目并使用Atmosphere软件包'反:假'来生成测试数据。



对于任何其他套餐,我必须 import 它。即使是Meteor本身,我也必须从'meteor / meteor'中 import {Meteor}; 。对于其他类似的Atmosphere软件包,例如 publish-counts 我还必须在'meteor / tmeasday:publish-中编写一行如 import {Counts}计数; 。 ...此外,即使我想导入 anti:fake 我也看不到任何导入名称可以正常工作而不会出错。它们在 packages 文件中以相同的方式列出,并以相同的方式添加到我的项目中,因此,从表面上看,它们看起来都是一样的。



这一切都不重要,因为我可以神奇地使用一个名为 Fake 的对象,但我真的不明白为什么会这样。以及我将来如何知道我需要明确导入什么以及隐式导入的内容。



谢谢!

解决方案

Meteor 1.3引入了ES2015模块,但是为了保持向后兼容性,Meteor仍然保留了导入 anti:fake 包给你,将它绑定到全局变量



出于同样的原因,实际上并不需要从'meteor / meteor'导入{Meteor},但强烈建议将其作为最佳实践(如果将来的版本通过r放弃这种向后兼容性,可能会成为必要如果您寻找针对Meteor 1.3或更新版本的较新教程,您会发现大多数代码都放在中。 / imports 目录,因为Meteor构建工具对此进行了不同的处理,并且其中的所有代码都没有急切加载。必须显式导入此目录中的所有代码,这些代码与模块命名空间一起避免旧文件加载顺序问题,使您更清楚地了解应用程序中正在运行的代码(不那么神奇!)。



Meteor 1.3中还包括对NPM软件包的支持,它们为您提供了使用大气包的替代方案,这种方式包含的软件包也需要明确导入您的应用程序。



您可以考虑使用 https:// www。 npmjs.com/package/faker



但是如果有什么东西适合你,你不觉得必须改变,但希望这有助于你理解为什么它正在工作!



您确定 publish-counts 是否需要import语句?查看 package.js 此包显示这两行

  api.export('Counts'); 
api.export('publishCount','server');

这使得 publishCount 对象可用于导入此包的应用程序(或包)中的服务器,以及客户端和服务器上可用的 Counts 对象。不需要导入语句(因为它们放在全局命名空间中)



有关详细信息,请参阅迁移到Meteor 1.3 doc


I have seen a few similar questions to this but none seem to apply to my case.

I'm creating a Meteor project and using the Atmosphere package 'anti:fake' to generate test data.

For any other package, I have to import it. Even Meteor itself, I have to do import {Meteor} from 'meteor/meteor';. For other similar Atmosphere packages, such as publish-counts I have to also write a line like import {Counts} from 'meteor/tmeasday:publish-counts';. ...furthermore, even if I wanted to import anti:fake I can see no import name that will work without errors. They are listed the same way in the packages file and were added to my project the same way so, on the surface, they all look the same.

None of this really matters as I can just magically use an object called Fake, but I don't really understand why that is the case and how I'd know in the future what I need to import explicitly and what is just implicitly imported.

Thanks!

解决方案

Meteor 1.3 introduced ES2015 Modules, however to maintain backwards compatibility Meteor still retains the "eager loading" behavior that is importing the anti:fake package for you, binding it to the global variable Fake.

import {Meteor} from 'meteor/meteor' is not actually required for the same reason, but highly recommended as best practice (and potentially could become necessary if a future release dropped this backwards compatibility by removing the eager loading).

If you look for newer tutorials that target Meteor 1.3 or newer, you will see that most code is placed in the /imports directory, as this is handled differently by the Meteor build tool, and all code in it is not 'eagerly loaded'. All code in this directory must be imported explicitly, which along with the module namespacing and avoiding the old file load order issues, give you much greater clarity about what code is running in your application (less magic!).

Also included in Meteor 1.3 was support for NPM packages, and they give you an alternative to using atmosphere packages, and packages included this way will also need to be explicitly imported into your application.

You could consider using https://www.npmjs.com/package/faker.

However don't feel like you must change if something is working for you, but hopefully this helps you understand why it is working!

Are you sure publish-counts requires the import statement? Looking at the package.js for this package shows these two lines

  api.export('Counts');
  api.export('publishCount', 'server');

This makes the publishCount object available on the server in an app (or package) that imports this package, and the Counts object available on both client and server. No import statement needed (as they are placed in the global namespace)

For more info, see the Migrating to Meteor 1.3 doc

这篇关于为什么我不能在我的Meteor源文件中“导入”`anti:fake`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆