问题描述
compileComponents
仅无奈地声明以下内容:
The docs for compileComponents
unhelpfully state only this:
但是,这并不能解释在什么情况下调用此函数是必要的",也不能解释不这样做的后果.我目前正在使用的应用程序对具有templateUrl
的组件进行了单元测试,这些测试涉及使用By.css
查看DOM,尽管我们从未在代码库中的任何地方调用compileComponents
,但它们似乎运行良好.同时,互联网上还有其他帖子,例如 https://github.com/angular/angular-cli/pull/4757 ,这表明不需要调用compileComponents
.
However, this doesn't explain in what circumstances is it "necessary" to call this function, nor what the consequences are of not doing so. The app I am currently working on has unit tests for components with templateUrl
s and those tests involve looking at the DOM using By.css
, yet they seem to work fine even though we never call compileComponents
anywhere in our codebase. Meanwhile, there are other posts on the internet, like https://github.com/angular/angular-cli/pull/4757, that suggest that calling compileComponents
is not required.
在什么情况下应该调用此方法,为什么?
In what circumstances should I call this method, and why?
推荐答案
如果您使用的是webpack(如果配置正确),则该版本会将templateUrl
s编译为内联template
s,并且将styleUrls
编译为styles
.因此无需compileComponents
,因为即使您没有使用它,也就像您正在使用template
和styles
一样.
If you're using webpack (if configured properly), the build will compile the templateUrl
s into inline template
s and styleUrls
to styles
. So there is no need to compileComponents
because it's just like you're using template
and styles
even though you're not.
例如,如果您使用的是SystemJS(或者您没有执行此预编译/转换的其他构建步骤),则不会发生这种情况. Angular将需要发出XHR请求以检索外部模板和外部样式,然后进行编译.在这里,您需要compileComponents
.
If for example you're using SystemJS (or you don't have some other build step that does this pre-compilation/conversion), this doesn't happen. Angular will need to make an XHR request to retrieve the external templates and external styles, and then compile. Here, you will need to compileComponents
.
这篇关于我什么时候应该调用compileComponents,如何才能不这样做呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!