我什么时候应该调用compileComponents,如何才能不这样做呢? [英] When am I supposed to call compileComponents, and how am I getting away with not doing so?

查看:82
本文介绍了我什么时候应该调用compileComponents,如何才能不这样做呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

compileComponents 仅无奈地声明以下内容:

The docs for compileComponents unhelpfully state only this:

使用templateUrl为测试的NgModule编译组件.由于提取网址是异步的,因此有必要调用此函数.

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.

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 templateUrls 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?

推荐答案

在什么情况下应该调用此方法,为什么?

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

如果您使用的是webpack(如果配置正确),则该版本会将templateUrl s编译为内联template s,并且将styleUrls编译为styles.因此无需compileComponents,因为即使您没有使用它,也就像您正在使用templatestyles一样.

If you're using webpack (if configured properly), the build will compile the templateUrls into inline templates 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.

测试文档

WebPack开发人员无需调用compileComponents,因为它作为运行测试之前的自动构建过程的一部分,内联了模板和CSS.

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

这篇关于我什么时候应该调用compileComponents,如何才能不这样做呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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