简体   繁体   English

使用组件模板与templateUrl的差异

[英]Differences of using Component template vs templateUrl

Angular 2 allows to write multi-line templates by using ` characters to enquote them. Angular 2允许使用`字符来引用它们来编写多行模板。 It is also possible to put multi-line template into .html file and reference it by templateUrl . 也可以将多行模板放入.html文件并通过templateUrl引用它。

It seems comfortable for me to put the template directly into component as then it's all in one place, but is there any drawback of doing so? 将模板直接放入组件中似乎很舒服,因为它只是在一个地方,但这样做有什么缺点吗?

1st approach: 第一种方法:

import {Component} from 'angular2/core';
@Component({
    selector: 'my-app',
    template: `
    <h1>My First Angular 2 multiline template</h1>
    <p>Second line</p> 
    `
})
export class AppComponent { }

vs 2nd approach: vs第二种方法:

import {Component} from 'angular2/core';
@Component({
    selector: 'my-app',
    templateUrl: 'multi-line.html'
})
export class AppComponent { }

together with multi-line.html : multi-line.html一起:

<h1>My First Angular 2 multiline template</h1>
<p>Second line</p> 

In terms of how the final application will perform, there's no real difference between having an embedded template and an external template. 就最终应用程序的执行方式而言,嵌入式模板和外部模板之间没有真正的区别。

For the developer, however, there are a number of differences that you have to consider. 但是,对于开发人员而言,您必须考虑许多差异。

  1. You get better code completion and inline support in your editor/IDE in most cases when the HTML is in a separate .html file. 在大多数情况下,当HTML位于单独的.html文件中时,您可以在编辑器/ IDE中获得更好的代码完成和内联支持。 (IntelliJ IDEA, at least, supports HTML for inline templates and strings) (IntelliJ IDEA,至少支持内联模板和字符串的HTML)
  2. There is a convenience factor to having code and the associated HTML in the same file. 在同一文件中使用代码和关联的HTML有一个方便的因素。 It's easier to see how the two relate to each other. 更容易看出两者如何相互关联。

These two things will be of equal value for many people, so you'd just pick your favorite and move ahead with life if this were all there was to it. 这两件事对很多人来说都是同等价值的,所以你只需选择自己喜欢的东西,然后继续生活,如果这就是它的全部。

But that leads us to the reasons you should keep your templates in your components, in my opinion: 但是,在我看来,这导致了我们应该将模板保留在组件中的原因:

  1. It is difficult to make use of relative filepaths for external templates as it currently stands in Angular 2. 对于外部模板很难使用相对文件路径,因为它目前位于Angular 2中。
  2. Using non-relative paths for external templates makes your components far less portable, since you need to manage all of the /where/is/my/template type references from the root that change depending on how deep your component is. 对外部模板使用非相对路径会使组件的可移植性降低,因为您需要管理根目录中所有/where/is/my/template类型引用,这些引用会根据组件的深度而变化。

That's why I would suggest that you keep your templates inside your components where they are easily found. 这就是为什么我建议您将模板放在组件中,以便轻松找到它们。 Also, if you find that your inline template is getting large and unwieldy, then it is probably a sign that you should be breaking your component down into several smaller components, anyway. 此外,如果您发现您的内联模板变得庞大且笨重,那么这可能表明您应该将组件分解为几个较小的组件,无论如何。

The drawbacks are that the IDE tooling isn't as strong as mentioned above, and putting large chunks of html into your components makes them harder to read. 缺点是IDE工具没有上面提到的那么强大,并且将大量的html放入组件中会使它们难以阅读。

Also, if you are following the angular2 style guide, it recommends that you do extract templates into a separate file when longer than 3 lines. 另外,如果你是以下的angular2风格指南,它建议你模板提取到一个单独的文件比3线较长时。

From the angular2 style guide : 来自angular2风格指南:

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

Why? 为什么? Syntax hints for inline templates in (.js and .ts) code files are not supported by some editors. 某些编辑器不支持(.js和.ts)代码文件中的内联模板的语法提示。

Why? 为什么? A component file's logic is easier to read when not mixed with inline template and styles. 未与内联模板和样式混合时,组件文件的逻辑更易于阅读。

Source 资源

You can pass a string literal to template consisting of your components HTML. 您可以将字符串文字传递给由组件HTML组成的template This way you have the HTML source in the same file as the TypeScript code. 这样,您就可以在与TypeScript代码相同的文件中使用HTML源代码。

With templateUrl you're referencing an external file containing the template HTML. 使用templateUrl您将引用包含模板HTML的外部文件。 This way you have HTML and TypeScript in separate files. 这样,您就可以在单独的文件中使用HTML和TypeScript。

In an external file you usually have better support for auto-completion and syntax check, but can be cumbersome because every component consists of several files instead of one (there are also styles). 在外部文件中,您通常更好地支持自动完成和语法检查,但可能很麻烦,因为每个组件都包含多个文件而不是一个(还有样式)。 External files need to be inlined in a build step, otherwise you'd have lots of server requests for loading all the template files. 外部文件需要在构建步骤中内联,否则您将有大量服务器请求加载所有模板文件。

The Angular2 style guide suggests to not have inline templates with more than 3 lines. Angular2样式指南建议没有超过3行的内联模板。

One of the drawbacks of putting your template HTML into separate file might happen if you're developing Angular2+ library that supposed to be used in other projects. 如果您正在开发应该在其他项目中使用的Angular2 + ,则可能会发生将模板HTML放入单独文件的缺点之一。

In this case the consumers of your library might not be able to perform JIT compilation successfully unless they use template loaders like angular2-template-loader or angular2-rollup or any other depending on the bundler that should be properly configured that sometimes might be tricky. 在这种情况下,库的使用者可能无法成功执行JIT编译,除非他们使用模板加载器,如angular2-template-loaderangular2-rollup或任何其他模板加载器 ,具体取决于应该正确配置的捆绑器,有时可能会很棘手。

To avoid this drawback and still have templates in separate files (which i think is quite convenient) the library developer might end up inlining templates automatically using Gulp plugin like gulp-inline-ng2-template or any other. 为了避免这个缺点并且仍然在单独的文件中使用模板(我认为这非常方便),库开发人员可能最终使用Gulp -inline-ng2-template或任何其他Gulp插件自动内联模板 But this would mean additional level of complexity while building ES2015 version of the library. 但这意味着在构建ES2015版本的库时会增加额外的复杂性。

Most of the answers seam to be around editor support, but, while that argument is true, I would not argue that as a strong point for putting the html and css into .html and .css files. 大多数答案都与编辑器支持有关,但是,虽然这个论点是正确的,但我并不认为这是将html和css放入.html和.css文件的强项。

The main reason that I separate my html and css files is because it follows SRP . 我将html和css文件分开的主要原因是它遵循SRP It allows for easy code portability and reuse as Angular changes versions and syntax of how the typescript should be layed out, your html and css stay for the most part the same. 它允许简单的代码可移植性和重用作为Angular更改版本和如何布置打字稿的语法,你的html和css大部分都保持不变。

Seems to be working now. 似乎现在正在工作。

@Component({
  selector: 'my-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css']
})

Taken from 'tour of heros' https://angular.io/docs/ts/latest/tutorial/toh-pt5.html and adapted to test this concrete issue. 摘自'英雄之旅' https://angular.io/docs/ts/latest/tutorial/toh-pt5.html并进行调整以适应这个具体问题。

angular2@2.0.0-beta.8 angular2@2.0.0-beta.8

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

相关问题 带有动态模板或 templateUrl 的 Angular 2/4 组件 - Angular 2/4 component with dynamic template or templateUrl Angular 2“模板”与“ templateUrl”问题 - Angular 2 “template” vs “templateUrl” issue Template vs templateUrl AND样式vs styleUrls-性能 - Template vs templateUrl AND styles vs styleUrls - Performance 错误:“组件无法同时定义模板和templateUrl” - Error: “component cannot define both template and templateUrl” Typescript组件:模板有效,但templateurl没有 - Typescript component: template works but templateurl does not 在Angular 2(4)中使用templateUrl时,如何告诉模板它属于哪个组件(用于智能/编译错误) - In Angular 2(4) when using templateUrl, how do you tell the template which component it belongs to (for intellisense/compile errors) 使用templateUrl时,Angular编译器返回错误:为component指定的模板不是字符串 - Angular compiler returns error when using templateUrl: The template specified for component is not a string 在angular2的模板中使用templateUrl - Using templateUrl within template in angular2 使用SystemJS在Component中使用templateUrl的相对路径 - Using relative path for templateUrl in Component with SystemJS 在Angular 2中使用template中的node需求和templateUrl之间有什么区别 - Is there any difference between using require of node in template and templateUrl in Angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM