javascript - 我应该什么时候调用 compileComponents,我怎么能不这样做呢?

标签 javascript angular

compileComponents 的文档无助地只说明这一点:

Compile components with a templateUrl for the test's NgModule. It is necessary to call this function as fetching urls is asynchronous.

然而,这并没有解释在什么情况下“有必要”调用这个函数,也没有解释不这样做的后果。我目前正在开发的应用程序对具有 templateUrl 的组件进行了单元测试,这些测试涉及使用 By.css 查看 DOM,但它们似乎工作正常,即使我们从不在代码库的任何地方调用 compileComponents。与此同时,互联网上还有其他帖子,例如https://github.com/angular/angular-cli/pull/4757。 ,这表明调用 compileComponents 不是必需的。

什么情况下应该调用这个方法,为什么?

最佳答案

In what circumstances should I call this method, and why?

如果您使用的是 webpack(如果配置正确),构建会将 templateUrl 编译成内联 templatestyleUrls样式。所以没有必要 compileComponents 因为它就像你正在使用 templatestyles 即使你不是。

例如,如果您使用的是 SystemJS(或者您没有执行此预编译/转换的其他构建步骤),则不会发生这种情况。 Angular 需要发出 XHR 请求来检索外部模板和外部样式,然后进行编译。在这里,您需要compileComponents

这在 Angular v2 testing docs 中说明

WebPack developers need not call compileComponents because it inlines templates and css as part of the automated build process that precedes running the test.

以及在 Angular v13 的最新文档中与调用 compileComponents 相关:

Ignore this section if you only run tests with the CLI ng test command because the CLI compiles the application before running the tests.

关于javascript - 我应该什么时候调用 compileComponents,我怎么能不这样做呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43608398/

相关文章:

typescript - Angular2 中的动态模板 "transclusion"

Javascript 替换不带特殊字符的精确匹配

javascript - 如何删除 TextField 状态

javascript - 将 API 中的 JSON 数据转换为有用的格式?

javascript - 如何以 Angular 传递 component.ts 文件中复选框的选中和未选中值?

angular - 如何通过单击事件将内容加载到另一个组件中? ( Angular 4)

php - Colorbox 内容平滑淡出

Javascript 获取对象属性和子属性

javascript - 我找到了如何在 typescript 中制作表格,但我不知道如何将其插入到html代码中

Angular 2 - 组件输入问题