Ben*_*ins 6 components reusability angular
当我开始使用Angular 2时,我认为创建组件的主要原因是因为您可以重用它们.例如:
<custom-button id="button1">button 1</custom-button>
<custom-button id="button2">button 2</custom-button>
Run Code Online (Sandbox Code Playgroud)
这是一个很大的理由,在网络应用程序中使用角度2和组件,它可以做到吗?
我没有找到专门回答我关于如何执行此操作的问题的资源.
我想创建一个按钮和一个输入,2个最基本和常用的html元素,并使它们可重用.
我试着制作一个按钮组件并重复使用它.
我试图在像这样的html模板中使用它:
<custom-button>some text</custom-button>
<custom-button>different text</custom-button>
Run Code Online (Sandbox Code Playgroud)
但是文本没有显示在按钮上.可以这样做吗?
我想知道的另一件事是做这个时的唯一html id.我能为每个实例添加一个唯一的html id属性吗?
也许喜欢:
<custom-button id="display-bikes">bikes</custom-button>
<custom-button id="display-helmets">helmets</custom-button>
Run Code Online (Sandbox Code Playgroud)
然后我可以使用元素的唯一ID来做一些特定的CSS?
这就是我想知道的,以及如何做到这一点.
但是,这是我的其余代码涉及:
零件:
import { Component } from '@angular/core';
@Component({
selector: 'custom-button',
templateUrl: 'app/shared/button.component.html',
styleUrls: ['app/shared/button.component.css']
})
export class ButtonComponent { }
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
ABa*_*bin 15
"这可以吗?"
是! 这就是所谓的transclusion或内容投影,您可以在这里详细了解它.
这是如何做:
在button.component.html:
<button>
<ng-content></ng-content>
</button>
Run Code Online (Sandbox Code Playgroud)
然后,每当你将内容放在组件的标签内时,如下所示:<custom-button id="display-bikes">bikes</custom-button>,Angular 2编译器会将它"投影"到ng-content标签之间的组件模板中.所以最终,你会在运行时得到它:
<button>
bikes
</button>
Run Code Online (Sandbox Code Playgroud)
这只是一个简单的例子.您可以使用它获得非常高级的功能,并执行投影其他组件和多个<ng-content>插座等操作.在上面链接的博客上阅读相关内容.
然后我可以使用元素的唯一ID来做一些特定的CSS?
此外,是的...虽然你不会使用标准的id属性.
在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;
//...
}
Run Code Online (Sandbox Code Playgroud)
假设button.component.css有两个名为class-one和的类class-two.
在button.component.html中:
<button [ngClass]="{class-one: styleId === 'applyClassOne', class-two: styleId === 'applyClasstwo'}">
<ng-content></ng-content>
</button>
Run Code Online (Sandbox Code Playgroud)
然后绑定到父类中的Input属性,如下所示:
<custom-button [styleId]="'applyClassOne'"></custom-button>
<custom-button [styleId]="'applyClassTwo'"></custom-button>
Run Code Online (Sandbox Code Playgroud)
还有其他动态应用样式的方法,但这个方法最简单,因为只有两个类可供选择.
你可以通过使用@Input装饰器来实现这一点.你可以在这里阅读更多相关信息.首先,在你的ButtonComponent,添加一个将显示按钮名称的变量,如下所示:
export class ButtonComponent {
@Input() buttonName: string;
}
Run Code Online (Sandbox Code Playgroud)
注意:您需要导入Input装饰器:import { Input } from '@angular/core';
然后,在html中使用双向数据绑定(插值)来显示按钮值:
<button md-raised-button type="button" class="btn text-uppercase flex-sm-middle">
{{buttonName}}
</button>
Run Code Online (Sandbox Code Playgroud)
最后,当您想要显示按钮组件时,您可以为按钮指定一个带有简单属性值的名称:
<custom-button buttonName="First Button"></custom-button>
<custom-button buttonName="Second Button"></custom-button>
<custom-button buttonName="Third Button"></custom-button>
Run Code Online (Sandbox Code Playgroud)
我尽量保持这个简单,如果遇到任何问题,请随时提问.