简体   繁体   English

在Angular 2中重用组件

[英]Reusing Components in Angular 2

When I started using Angular 2 I had the idea that the main reason to create components is because you can reuse them. 当我开始使用Angular 2时,我认为创建组件的主要原因是因为您可以重用它们。 EG: 例如:

<custom-button id="button1">button 1</custom-button>
<custom-button id="button2">button 2</custom-button>

Is it the case that this is a large reason to go with angular 2 and components in a web app, and it can be done? 这是一个很大的理由,在网络应用程序中使用角度2和组件,它可以做到吗?

I haven't found a resource that specifically answers my question about how to do this. 我还没有找到专门回答我关于如何执行此操作的问题的资源。

I'd like to create a button, and an input, the 2 most basic and commonly used html elements, and make them reusable. 我想创建一个按钮和一个输入,2个最基本和常用的html元素,并使它们可重用。

I tried to make a button component and reuse it. 我试着制作一个按钮组件并重复使用它。

I tried to use it in an html template like this: 我试图在像这样的html模板中使用它:

<custom-button>some text</custom-button>
<custom-button>different text</custom-button>

However the text did not show up on the button. 但是文本没有显示在按钮上。 Can this be done? 可以这样做吗?

Another thing I'm wondering about is unique html id's when doing this. 我想知道的另一件事是做这个时的唯一html id。 Am I able to add a unique html id attribute to each instance? 我能为每个实例添加一个唯一的html id属性吗?

Maybe like: 也许喜欢:

<custom-button id="display-bikes">bikes</custom-button>
<custom-button id="display-helmets">helmets</custom-button>

And then I can do some specific css using the unique id of the element? 然后我可以使用元素的唯一ID来做一些特定的CSS?

That's all I want to know, and how to do it. 这就是我想知道的,以及如何做到这一点。

However here is the rest of my code involved: 但是,这是我的其余代码涉及:

component: 零件:

import { Component } from '@angular/core';
@Component({
    selector: 'custom-button',
    templateUrl: 'app/shared/button.component.html',
    styleUrls: ['app/shared/button.component.css']
})
export class ButtonComponent { }

css: CSS:

button {
  font: 200 14px 'Helvetica Neue' , Helvetica , Arial , sans-serif;
  border-radius: 6px;
  height: 60px;
  width: 280px;
  text-decoration: none;
  background-color: $accent;
  padding: 12px;
  color: #FFF;
  cursor: pointer;
}

button:focus {
    outline:0 !important;
}

html: HTML:

<button md-raised-button type="button" class="btn text-uppercase flex-sm-middle">
try now <!-----lets get rid of this and let the client decide what text to display somehow???
</button>

"Can this be done?" “这可以吗?”

Yes! 是! It's called transclusion or content projection and you can read about it in detail here . 这就是所谓的transclusion内容投影 ,您可以在这里详细了解它

Here's how: 这是如何做:

In button.component.html : button.component.html

<button>
    <ng-content></ng-content>
</button>

Then, whenever you put content inside of the tags for the component, like this: <custom-button id="display-bikes">bikes</custom-button> , the Angular 2 compiler will 'project' it into the component's template inbetween the ng-content tags. 然后,无论何时将内容放入组件的标签内,如下所示: <custom-button id="display-bikes">bikes</custom-button> ,Angular 2编译器会将其“投影”到组件的模板中在ng-content标签之间。 So ultimately, you'd get this at runtime: 所以最终,你会在运行时得到它:

<button>
    bikes
</button>

That's just a simple example. 这只是一个简单的例子。 You can get really advanced with it and do things like project other components and have multiple <ng-content> outlets. 您可以获得非常高级的功能,并执行项目其他组件和多个<ng-content>插座等操作。 Read about that at the blog linked above. 在上面链接的博客上阅读相关内容。

And then I can do some specific css using the unique id of the element? 然后我可以使用元素的唯一ID来做一些特定的CSS?

Also, yes... though you wouldn't use the standard id attribute. 此外,是的...虽然你不会使用标准的id属性。

On your ButtonComponent: 在ButtonComponent上:

import { Component, Input } from '@angular/core';
@Component({
  selector: 'custom-button',
  templateUrl: 'app/shared/button.component.html',
  styleUrls: ['app/shared/button.component.css']
})
export class ButtonComponent {
  @Input() styleId: string;

  //...
}

Say that button.component.css has two classes named class-one and class-two . 假设button.component.css有两个名为class-oneclass-two

In button.component.html: 在button.component.html中:

<button [ngClass]="{class-one: styleId === 'applyClassOne', class-two: styleId === 'applyClasstwo'}">
    <ng-content></ng-content>
</button>

Then you bind to the Input property in the parent like so: 然后绑定到父类中的Input属性,如下所示:

<custom-button [styleId]="'applyClassOne'"></custom-button>
<custom-button [styleId]="'applyClassTwo'"></custom-button>

There are other ways of applying styles dynamically, but this one is the simpliest given that there are only two classes to choose from. 还有其他动态应用样式的方法,但这个方法最简单,因为只有两个类可供选择。

You can achieve this by using @Input decorator. 您可以使用@Input装饰器来实现此@Input You can read more about it here . 你可以在这里阅读更多相关信息。 First, in your ButtonComponent , add a variable which will display button name, like this: 首先,在ButtonComponent ,添加一个将显示按钮名称的变量,如下所示:

export class ButtonComponent {

    @Input() buttonName: string;

}

Note : You need to import Input decorator: import { Input } from '@angular/core'; 注意 :您需要导入Input装饰器: import { Input } from '@angular/core';

Then, in html use two-way data binding (interpolation) to display button value: 然后,在html中使用双向数据绑定(插值)来显示按钮值:

<button md-raised-button type="button" class="btn text-uppercase flex-sm-middle">
    {{buttonName}}
</button>

And, finally, when you want to display button component, you can give button a name with a simple attribute value: 最后,当您想要显示按钮组件时,您可以为按钮指定一个带有简单属性值的名称:

<custom-button buttonName="First Button"></custom-button>
<custom-button buttonName="Second Button"></custom-button>
<custom-button buttonName="Third Button"></custom-button>

I tried to keep this as simple as possible, feel free to ask questions if you encounter any problems. 我尽量保持这个简单,如果遇到任何问题,请随时提问。

You really should not bother writing specific CSS that depends on the ID of your component, this is not necessary. 你真的不应该费心去编写依赖于组件ID的特定CSS,这不是必需的。

Each component is encapsulated (by default). 每个组件都被封装(默认情况下)。 The encapsulation uses the Shadow DOM, or an emulation (default). 封装使用Shadow DOM或仿真(默认)。 For each component, you can choose one of the 3 encapsulation modes: 对于每个组件,您可以选择以下3种封装模式之一:

  • ViewEncapsulation.None -- no style encapsulation ViewEncapsulation.None - 没有样式封装
  • ViewEncapsulation.Emulated -- the default, uses attributes to "encapsulate" the style ViewEncapsulation.Emulated - 默认值,使用属性“封装”样式
  • ViewEncapsulation.Native -- Uses Shadow DOM ViewEncapsulation.Native - 使用Shadow DOM

Here is a good blog post about this topic: http://blog.thoughtram.io/angular/2015/06/29/shadow-dom-strategies-in-angular2.html 这是一篇关于这个主题的好博文: http//blog.thoughtram.io/angular/2015/06/29/shadow-dom-strategies-in-angular2.html

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

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