javascript – 什么时候我应该调用compileComponents,我怎么逃避不这样做?

compileComponents的文档无益地说明了这一点:

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

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

在什么情况下我应该称这种方法为什么?

解决方法:

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

如果您正在使用webpack(如果配置正确),则构建会将templateUrls编译为内联模板,将styleUrls编译为样式.所以没有必要编译组件,因为它就像你正在使用模板和样式,即使你不是.

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

这在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.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...