简体   繁体   English

Template vs templateUrl AND样式vs styleUrls-性能

[英]Template vs templateUrl AND styles vs styleUrls - Performance

In Angular 2, we can either use inline or url approach for templates and styles. 在Angular 2中,我们可以对模板和样式使用内联或url方法。 Using url is good for modularity, readability and IDEs also provide code assistance. 使用url有利于模块化,可读性,并且IDE还提供代码帮助。 Using url will result in additional request from browser which might cause some performance drop. 使用url将导致来自浏览器的其他请求,这可能会导致性能下降。 What's the recommended way of using templates and style - either inline or url? 建议使用模板和样式的方法是内联还是url?

Example from Angular 2 cheatsheet Angular 2速查表中的示例

template: 'Hello {{name}}'
templateUrl: 'my-component.html'    

styles: ['.primary {color: red}']
styleUrls: ['my-component.css']

Yes well said 是的好说

Using url is good for modularity, readability and IDEs also provide code assistance. 使用url有利于模块化,可读性,并且IDE还提供代码帮助。 Using url will result in additional request from browser which might cause some performance drop 使用url将导致来自浏览器的其他请求,这可能会导致性能下降

but its depends on us how long our code is according to @mgchev style guide 但是这取决于我们,根据@mgchev样式指南,我们的代码要持续多长时间

Extract the more complex and bigger templates, longer than 15 lines of code, into a separate file and put them next to their controllers' definition. 将超过15行代码的更复杂,更大的模板提取到一个单独的文件中,并将其放在其控制器的定义旁边。

Why?: In case a big and complex template is inlined in the component metadata it may shift the focus from the component's logic defined within the controller. 为什么?:如果在组件元数据中内嵌了一个大型而复杂的模板,则可能会将焦点从控制器内定义的组件逻辑上移开。

but if we look at the officials style guide than it will tell us to use seprate file if coding exceeds more than 3 lines 但是如果我们看一下官方样式指南,它将告诉我们如果编码超过3行,则使用单独的文件

Do extract templates and styles into a separate file, when more than 3 lines. 当超过3行时,请将模板和样式提取到单独的文件中。

also same case apply's in the Css styles, so better is to provide exteranal url instead of inline if we have complex code. Css样式也是如此,因此,如果我们有复杂的代码,最好提供外部url而不是内联。 also see here : 也可以在这里看到:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM