如何将组件放在 Angular2 中的另一个组件中? [英] How to put a component inside another component in Angular2?

查看:26
本文介绍了如何将组件放在 Angular2 中的另一个组件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Angular 的新手,我仍在努力理解它.我已经学习了 Microsoft Virtual Academy 上的课程,这很棒,但我发现他们所说的内容与我的代码行为方式之间存在一些差异!具体来说,我试图将一个组件放在另一个组件中",如下所示:

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 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{}

这是他们在课程中制作的相同示例,但在我的代码中不起作用!特别是 VisualStudio 对我说组件装饰器中不存在指令"属性.我该如何解决这个问题?

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. How can I solve this?

推荐答案

不要把一个组件放在指令

您在@NgModule 声明中注册:

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

然后你只需将它放在父模板 HTML 中:<my-child></my-child>

and then You just put it in the Parent's Template HTML as : <my-child></my-child>

就是这样.

这篇关于如何将组件放在 Angular2 中的另一个组件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆