简体   繁体   English

选择器在 angular 2 中究竟做了什么?

[英]What exactly does a selector do in angular 2?

What does the selector do in this case?选择器在这种情况下做什么?

import { Component } from '@angular/core';
import { HighlightDirective } from './highlight.directive';

@Component({
  selector: 'my-app',
  templateUrl: 'app/app.component.html',
  directives: [HighlightDirective]
})
export class AppComponent { }

And what does it do in this case?在这种情况下它会做什么?

@Directive({
  selector: '[myHighlight]',
  host: {
    '(mouseenter)': 'onMouseEnter()',
    '(mouseleave)': 'onMouseLeave()'
  }
})

The component is applied to the <my-app></my-app> tag in your index.html .该组件应用于index.html<my-app></my-app>标签。 If your index.html doesn't have that tag Angular will fail at startup.如果您的index.html没有该标签,Angular 将在启动时失败。 You can control where you Angular application will be played.您可以控制 Angular 应用程序的播放位置。

This is special for the Angular component that is created using bootstrap(AppComponent)这对于使用bootstrap(AppComponent)创建的 Angular 组件是特别的

The directive selector [myHighlight] will create a MyHighlight directive instance for all elements that have a myHighlight attribute like <xxx myHighlight> and where MyHighLight is listed in directives like指令选择器[myHighlight]将为所有具有myHighlight属性(如<xxx myHighlight>以及MyHighLight列在指令中的元素创建一个MyHighlight指令实例,如

@Component({
  selector: '...', 
  directives: [MyHighlight], ...
})
export class Xxx

Like the directive selector for other components (that are not the root component like AppComponent usually is), it works the same like the selector for the directive.与其他组件的指令选择器(它们不是 AppComponent 通常是的根组件)一样,它的工作方式与指令的选择器相同。 The component has to be listed in the directives array.该组件必须列在directives数组中。 Then all tags that match the selector are upgraded to Angular components.然后所有匹配选择器的标签都升级为 Angular 组件。

Selectors are like CSS selectors.选择器就像 CSS 选择器。 They can be attribute selectors, tag selectors, class selectors, id selectors and combinations of these.它们可以是属性选择器、标签选择器、类选择器、id 选择器以及它们的组合。 Also :not(...) is supported.还支持:not(...)

What is not supported are selectors that need to match parent and child like with combinators like ab or a > b or a + b where b is a sibling, child, descandant, ... of another component.不支持的选择器需要像aba > ba + b类的组合子匹配 parent 和 child,其中 b 是另一个组件的兄弟、孩子、descandant...。 A directive or component selector can always only refer to a single element.指令或组件选择器始终只能引用单个元素。

If we say in simple term selector is name which is used in our view like html tag.如果我们用简单的术语说选择器是在我们的视图中使用的名称,如 html 标签。 as we all know angular2 is component based.众所周知,angular2 是基于组件的。 so selector is just provide the name of the component which is being called by its className in the directives list and called by selector name in the view of the another component like this:-所以选择器只是提供组件的名称,该名称由指令列表中的 className 调用,并在另一个组件的视图中由选择器名称调用,如下所示:-

//suppose this is our component //假设这是我们的组件

@Component({
 selector : 'mycomponent'
 ....
})
export class Mycomponent{ }

now we can use this component in another component like this -现在我们可以在这样的另一个组件中使用这个组件 -

@Component({
 selector : 'secondcomponent',
 directives: [Mycomponent],  //here we use class name instead of selector name
 template: '<mycomponent></mycomponent>'  //here we used selector name
 ....
})
export class Mycomponent{ }

Also we can say selector is a complete functionality name used as html tag in the view.我们也可以说选择器是一个完整的功能名称,用作视图中的 html 标签。

In the second component, we export second component instead of My component.在第二个组件中,我们导出第二个组件而不是 My 组件。 Correct me if I was wrong.如果我错了,请纠正我。

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

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