In Ionic 2, how do I create a custom directive tha

2019-04-04 17:29发布

Creating a basic directive is simple:

import {Component} from 'angular2/core';

@Component({
    selector: 'my-component',
    template: '<div>Hello!</div>'
})
export class MyComponent {
    constructor() {

    }
}

This works as expected. However, if I want to use Ionic components in my directive things blow up.

import {Component} from 'angular2/core';

@Component({
    selector: 'my-component',
    template: '<ion-list><ion-item>I am an item</ion-item></ion-list>'
})
export class MyComponent {
    constructor() {

    }
}

The directive is rendered, but Ionic components are not transformed, and so wont look/work properly.

I can't find any examples on this. How should I do this?

1条回答
做自己的国王
2楼-- · 2019-04-04 18:28

Found the answer here:

You have to import the Ionic components and register them as 'directives'

So my second example becomes:

import {Component} from 'angular2/core';
import {List, Item} from 'ionic/ionic';

@Component({
    selector: 'my-component',
    directives: [List, Item],
    template: '<ion-list><ion-item>I am an item</ion-item></ion-list>'
})
export class MyComponent {
    constructor() {

    }
}
查看更多
登录 后发表回答