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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。