动态分量加载角度2

时间:2016-05-26 12:22:25

标签: typescript angular viewchild

我使用动态组件加载器从一个服务填充的数组中读取组件名称,然后在页面上加载组件。

我使用的是API的loadIntoLocation方法,但因为它没有出现在最新版本的angular中,所以我需要将它替换为最新版本的@ViewChild。

这是我在其他一些使用resolveComponent动态添加组件的线程上找到的一个有用的plunker示例。

(请参阅以下评论中的plunker链接。由于我在移动设备上输入此内容,我无法插入代码而无需代码我无法插入plunker链接)

但是,我想稍微改变一下实施方式。现在,app.ts传递要加载到Tabs组件的组件数组。在这里,我使用@ViewChild加载页面上的所有组件。

我想做的是我想要从app.ts传递@ViewChild引用,以便我可以定义: <div #target></div> (应该在其中呈现组件)在App.ts文件中。现在它出现在Tabs.ts中,但我不想在Tabs组件中定义它。我想将Tabs.ts作为其他组件可以用来动态加载其组件的基类。

因此,实际上,每个组件都有自己的模板,如:

//Component 1
  Template: <div #left></div>
            <div #right></div>

  @ViewChild('left') child1
  @ViewChild('right') child2
  (Now pass these ViewChild references child1 and child2 to the component builder Tabs component so that I can control from component1 where to render these children)

  //Do this for multiple components

0 个答案:

没有答案
相关问题