首页 文章

指令不适用于离子3

提问于
浏览
0

离子3中的指令示例,以前使用的方法不起作用 . 已经尝试过在离子2中实现指令的方式 .

import {Directive} from 'angular2/core';

@Directive({
   selector: 'test',
   template: './test.html'
)}
export class MyDirective { }

import {MyDirective } from './test'

@Page({
   templateUrl: '../page.html',
   directives: [MyDirective ],
})

1 回答

  • 2

    这可以通过以下方式实现:

    ComponentA,父组件

    acomponent.ts

    import {Component} from '@angular/core';
    
    @Component({
        selector: 'component-a',
        templateUrl: 'acomponent.html'
    })
    export class ComponentA { }
    

    acomponent.html

    <div>
      <h3>Component A</h3>
      <component-b></component-b>
    </div>
    

    bcomponent.ts

    import {Component} from '@angular/core';
    
    @Component({
        selector: 'component-b',
        templateUrl: 'bcomponent.html'
    })
    export class ComponentB { }
    

    bcomponent.html

    <h3>Component B</h3>
    

    app.module.ts

    import { ComponentA } from 'pathto/acomponent';
    import { ComponentB } from 'pathto/bcomponent';
    //other imports here too
    
    @NgModule({
      declarations: [
        ComponentA,
        ComponentB,
        //other declarations
      ],
      imports: [//your imports],
      bootstrap: [IonicApp],
      entryComponents: [
        ComponentA,
        ComponentB,
        //other entryComponents
      ],
      providers: [//your providers]
    })
    export class AppModule { }
    

    当您导航到 ComponentA 时,它会将 ComponentB 注入选择器 <component-b></component-b>

    希望这会有所帮助

相关问题