How to put a component inside another component in Angular2?

后端 未结 3 1462
猫巷女王i
猫巷女王i 2021-01-31 02:18

I\'m new at Angular and I\'m still trying to understand it. I\'ve followed the course on the Microsoft Virtual Academy and it was great, but I found a little discrepancy between

3条回答
  •  日久生厌
    2021-01-31 03:01

    If you remove directives attribute it should work.

    @Component({
        selector: 'parent',
        template: `
                

    Parent Component

    ` }) export class ParentComponent{} @Component({ selector: 'child', template: `

    Child Component

    ` }) export class ChildComponent{}

    Directives are like components but they are used in attributes. They also have a declarator @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.

    A component manages a region of HTML in the manner of a native HTML element. 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. Components are usually represented as HTML elements. They are the building blocks of an Angular application.

    • Attribute directives can listen to and modify the behavior of other HTML elements, attributes, properties, and components. They are usually represented as HTML attributes, hence the name.

    • Structural directives are responsible for shaping or reshaping HTML layout, typically by adding, removing, or manipulating elements and their children.


    The difference that components have a template. See Angular Architecture overview.

    A directive is a class with a @Directive decorator. A component is a directive-with-a-template; a @Component decorator is actually a @Directive decorator extended with template-oriented features.


    The @Component metadata doesn't have directives attribute. See Component decorator.

提交回复
热议问题