一个组件可以有多个模板吗? [英] Can one component have multiple templates?

查看:37
本文介绍了一个组件可以有多个模板吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular 2 组件有没有办法根据我想放置它的位置使用许多模板文件?

Is there a way for Angular 2 component to use many template files base on where i want to place it?

例如,我有一个 login 组件,我想将它以两种不同的设计在我的网站上放置两次.

For example, I have a login component and i want to place it two times on my website with two different designs.

有没有办法将模板传递给组件?

Is there a way that i can pass template to a component?

推荐答案

不确定 NG2 是否有内置方式来支持这一点.我只是使用了一个基本组件类来包含所有或大部分逻辑和数据,没有 html 模板.然后在派生组件类中,只需要声明调用super(...)的构造函数,并定义相应的html模板即可.

Not sure if NG2 has built-in way to support this. I just used a base component class to contain all or most logic and data, without a html template. Then in derived component classes, just need to declare constructor calling super(...), and define respective html template.

如果您的应用程序很复杂,很可能您将使用模块来实例化类,那么请确保在 Component 属性中声明 moduleId: module.id ,否则 NG2 运行时可能会抱怨无法加载 html 模板.

If your application is complex, likely you will be using module to instantiate classes, then make sure you declare moduleId: module.id in the Component attribute, otherwise the NG2 runtime may complain the html template could not be loaded.

这篇关于一个组件可以有多个模板吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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