【问题标题】:Angular 2 (Ionic 2): can't "render" Component inside PageAngular 2(Ionic 2):不能在页面内“渲染”组件
【发布时间】:2016-06-15 05:52:42
【问题描述】:

我构建了一个 Angular 2 组件:

import {Component, View} from 'angular2/core';

@Component({
    selector: 'sidemenu'
})
@View({
  templateUrl: 'build/pages/menu/menu.html',
})
export class Menu {
}

在页面组件中,我不能在模板中使用它:

import {Menu} from '../menu/menu';
@Page({
  templateUrl: 'build/pages/content/content.html'
})
export class PO {

}

content.html:

<ion-content>
<sidemenu></sidemenu>
</ion-content>

不会被 Menu 组件的“html”(build/pages/menu/menu.html)替换。但是,如果我使用 dynamicComponentLoader 从 DOM 加载元素内的组件,它就可以工作(我这样做是为了检查组件是否正常)。 我错过了什么吗?

【问题讨论】:

    标签: javascript angular ionic2 angular2-template


    【解决方案1】:

    指令:[菜单]

    import {Menu} from '../menu/menu';
    @Page({
        templateUrl: 'build/pages/content/content.html',
    directives: [Menu]
    })
    export class PO {
    
    }
    

    【讨论】:

    • 我试过这个,现在我得到这个错误:“例外:类型错误:无法读取未定义的属性'订阅'”如果我尝试在 content.html 中使用 /跨度>
    • 尝试在指令中也导入 IONIC_DIRECTIVES
    【解决方案2】:

    我不知道 Ionic,但通常在 Angular2 中,您需要在 @Component({ ..., directives: [Menu], ...}){ ...} 中列出您在模板中使用的组件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-26
      • 2017-08-25
      • 1970-01-01
      • 2023-03-14
      • 2017-04-05
      • 2017-02-14
      • 1970-01-01
      相关资源
      最近更新 更多