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

查看:70
本文介绍了如何在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?

推荐答案

不要将组件放入directives

您在@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天全站免登陆