简体   繁体   English

如何将组件放在Angular2中的另一个组件中?

[英]How to put a component inside another component in Angular2?

I'm new at Angular and I'm still trying to understand it. 我是Angular的新手,我还在努力理解它。 I've followed the course on the Microsoft Virtual Academy and it was great, but I found a little discrepancy between what they said and how my code behave! 我已经按照微软虚拟学院的课程进行了测试,这很棒,但我发现他们所说的内容与我的代码行为之间存在一些差异! Specifically, I've tried to "put a component inside another component" like this: 具体来说,我试图“将一个组件放在另一个组件中”,如下所示:

@Component({
    selector: 'parent',
    directives: [ChildComponent],
    template: `
            <h1>Parent Component</h1>
            <child></child>
        `
    })
    export class ParentComponent{}


@Component({
    selector: 'child',    
    template: `
            <h4>Child Component</h4>
        `
    })
    export class ChildComponent{}

This is the same example that they make on the course, but in my code doesn't work! 这是他们在课程上做的相同的例子,但在我的代码中不起作用! In particular VisualStudio says to me that the 'directives' property doesn't exist in the component decorator. 特别是VisualStudio告诉我,组件装饰器中不存在'directives'属性。 How can I solve this? 我怎么解决这个问题?

You don't put a component in directives 没有将组件放在directives

You register it in @NgModule declarations: 您在@NgModule声明中注册它:

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App , MyChildComponent ],
  bootstrap: [ App ]
})

and then You just put it in the Parent's Template HTML as : <my-child></my-child> 然后你只需将它放在Parent的模板HTML中: <my-child></my-child>

That's it. 而已。

If you remove directives attribute it should work. 如果删除指令属性,它应该工作。

@Component({
    selector: 'parent',
    template: `
            <h1>Parent Component</h1>
            <child></child>
        `
    })
    export class ParentComponent{}


@Component({
    selector: 'child',    
    template: `
            <h4>Child Component</h4>
        `
    })
    export class ChildComponent{}

Directives are like components but they are used in attributes. 指令与组件类似,但它们用于属性。 They also have a declarator @Directive . 他们还有一个声明者@Directive You can read more about directives Structural Directives and Attribute Directives . 您可以阅读有关指令结构指令属性指令的更多信息。

There are two other kinds of Angular directives, described extensively elsewhere: (1) components and (2) attribute directives. 还有另外两种Angular指令,在别处广泛描述:(1)组件和(2)属性指令。

A component manages a region of HTML in the manner of a native HTML element. 组件以本机HTML元素的方式管理HTML区域。 Technically it's a directive with a template. 从技术上讲,它是一个带模板的指令。


Also if you are open the glossary you can find that components are also directives. 此外,如果您打开词汇表,您会发现组件也是指令。

Directives fall into one of the following categories: 指令属于以下类别之一:

  • Components combine application logic with an HTML template to render application views. 组件将应用程序逻辑与HTML模板相结合以呈现应用程序视图。 Components are usually represented as HTML elements. 组件通常表示为HTML元素。 They are the building blocks of an Angular application. 它们是Angular应用程序的构建块。

  • Attribute directives can listen to and modify the behavior of other HTML elements, attributes, properties, and components. 属性指令可以侦听和修改其他HTML元素,属性,属性和组件的行为。 They are usually represented as HTML attributes, hence the name. 它们通常表示为HTML属性,因此名称。

  • Structural directives are responsible for shaping or reshaping HTML layout, typically by adding, removing, or manipulating elements and their children. 结构指令负责整形或重塑HTML布局,通常通过添加,删除或操作元素及其子元素。


The difference that components have a template. 组件具有模板的区别。 See Angular Architecture overview. 请参阅角度架构概述。

A directive is a class with a @Directive decorator. 指令是一个带有@Directive装饰器的类。 A component is a directive-with-a-template; 组件是带有模板的指令; a @Component decorator is actually a @Directive decorator extended with template-oriented features. @Component装饰器实际上是一个扩展了面向模板功能的@Directive装饰器。


The @Component metadata doesn't have directives attribute. @Component元数据没有directives属性。 See Component decorator . 请参阅组件装饰器

我认为你的Angular-2版本指令在组件装饰器中不受支持,因此你必须在@NgModule中 注册与其他组件相同的指令 ,然后在组件中导入如下,并从装饰器中删除directives: [ChildComponent]

import {myDirective} from './myDirective';

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

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