Angular 2全局组件 [英] Angular 2 global component

查看:136
本文介绍了Angular 2全局组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的结构如下:

App
    *component, module, template*
    Component 1
        *component, module, template*
        Component 11
            *component, module, template*
    Global Component
        *component, temaplte, BUT NO MODULE*

我的App模块包含组件1和组件11模块.我在app模块中声明了全局组件,以便可以在所有其他组件中使用它.我也将其导出到应用程序模块中.

My App module contains the component 1 and component 11 modules. I declared the global component in the app module, so that I can use it in all my other components. I also exported it in app module.

但是当我在组件11模板中使用选择器时,控制台会显示not a known element.

But when I use the selector in the component 11 template, the console says not a known element.

我尝试将其导入到其他组件中,但是它说它已经被导入了,因此我应该将其导入到更高的模块中.

I tried importing it in my other components but it says it's already imported, and that I should import it in a higher module.

您能告诉我如何声明一个组件,以便可以在子模板中使用它吗?

Could you tell me how to declare a component so that it can be used in child templates ?

推荐答案

我假设在angular 2体系结构中未知全局组件".另请参阅此处==> https://angular.io/docs/ts/Latest/guide/architecture.html

I'd assume "global components" are not known in the angular 2 architecture. See also here ==> https://angular.io/docs/ts/latest/guide/architecture.html

根据经验,我知道如果在app.module级别导入组件(例如管道)可能会是一个问题.例如,使用管道进行此操作时,会出现找不到管道"错误.也许您的组件遇到类似的问题.

From experience I know that it can be a problem if components (e.g. pipes) are imported at the app.module level. When doing this with a pipe for example you get a "pipe not found" error. Maybe you encounter a similiar problem with your component.

因此,我建议您将组件作为模块的一部分,然后将模块导入app.module而不是组件中.

So I'd recommend to make your component part of a module and then import your module in your app.module instead of the component.

这篇关于Angular 2全局组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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