本文介紹了在 Ionic 2 中,如何創建使用 Ionic 組件的自定義指令?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
創建一個基本指令很簡單:
Creating a basic directive is simple:
import {Component} from 'angular2/core';
@Component({
selector: 'my-component',
template: '<div>Hello!</div>'
})
export class MyComponent {
constructor() {
}
}
這按預期工作.但是,如果我想在我的指令中使用 Ionic 組件,事情就會爆炸.
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() {
}
}
指令已渲染,但 Ionic 組件未轉換,因此無法正常查看/工作.
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?
推薦答案
找到答案 這里:
您必須導入 Ionic 組件并將它們注冊為'指令'
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() {
}
}
這篇關于在 Ionic 2 中,如何創建使用 Ionic 組件的自定義指令?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!