我正在使用一个简单的TabsProvider隐藏Ionic 2中的某些@Pages(一个Ionic 2装饰器)的标签:
tabs.ts
import { Injectable } from 'angular2/core';
import { BehaviorSubject } from 'rxjs';
@Injectable()
export class TabsProvider {
currentState = new BehaviorSubject<boolean>(true);
public showTabs(){
this.currentState.next(true);
}
public hideTabs(){
this.currentState.next(false);
}
}
tabs组件订阅currentState,TabsProvider注入各种页面,如下所示:
样品page.ts:
import {Page} from 'ionic-angular';
import { TabsProvider } from './tabs';
@Page({
...
})
export class SamplePage {
tabsProvider: TabsProvider;
constructor(tabsProvider: TabsProvider) {
this.tabsProvider = tabsProvider;
}
onPageWillEnter(){
this.tabsProvider.hideTabs();
}
onPageWillLeave(){
this.tabsProvider.showTabs();
}
}
这段代码几乎都是样板文件,如果我可以在装饰器(或注释)中定义这个功能,那将会更加清晰,例如:
import { Page } from 'ionic-angular';
import { hideTabs } from './tabs';
@hideTabs()
@Page({
...
})
export class BuyPage {
}
但我无法确定如何注入TabsProvider并将onPageWillEnter和onPageWillLeave方法添加到SamplePage.
装饰器(或注释)可以以某种方式注入额外的Angular提供者吗?
到目前为止我得到的最远:
在tabs.ts中:
export function hideTabs() {
return function(cls: any) {
cls.prototype.onPageWillEnter = function() {
this.tabsProvider.hideTabs();
};
cls.prototype.onPageWillLeave = function() {
this.tabsProvider.showTabs();
};
return cls;
}
}
这使我们成为我们正在寻找的部分,但仍然需要导入TabsProvider并将其作为特定实例成员注入:
样品page.ts
import {Page, Events} from 'ionic-angular';
import { hideTabs, TabsProvider } from './tabs';
@hideTabs()
@Page({
...
})
export class SamplePage {
constructor(public tabsProvider: TabsProvider) {
}
}
是否可以将其完全抽象为@hideTabs()?
编辑:
选项卡组件的相关部分(对于任何对实现感兴趣的人)pages / tabs / tabs.ts:
import { Page } from 'ionic-angular';
import { TabsProvider } from './tabs';
@Page({
...
})
export class TabsPage {
...
currentState: boolean;
constructor(TabsProvider: TabsProvider) {
TabsProvider.currentState.subscribe((state: boolean) => {
this.currentState = state;
});
}
}
页/标签/ tabs.html:
<div [ngClass]="{'hide-tabs': !currentState}">
<ion-tabs>
...
</ion-tabs>
</div>
页/标签/ tabs.scss:
.hide-tabs ion-tabbar-section {
display: none;
}
最佳答案 我刚遇到同样的问题,这就是我提出的解决方案.一般方法是:
>使用Reflect.getOwnMetadata从类中读取现有依赖项.
>添加到依赖项列表
>创建一个新的构造函数,它包装旧的构造函数并处理这些依赖项.
tabs.ts:
import TabsProvider from "./tabs";
export function hideTabs() {
return function(cls: Function) : any {
// Save existing onPageWillEnter and onPageWillLeave inplementations
let enter = cls.prototype.onPageWillEnter || function () {};
let leave = cls.prototype.onPageWillLeave || function () {};
// Create new constructor for class
const newCls = function (tabsProvider, ...args) {
this.__hideTabs__tabsProvider = tabsProvider;
return cls.apply(this, args);
}
// Copy prototype to new constructor
newCls.prototype = constructor.prototype;
// Copy metadata to new constructor
let metadatakeys = Reflect.getMetadataKeys(cls);
metadatakeys.forEach(function (key) {
Reflect.defineMetadata(key, Reflect.getOwnMetadata(key, cls), newCls)
});
// Read dependencies list from 'cls', add our own dependency, and write list to 'newCls'
let dependencies = Reflect.getOwnMetadata('design:paramtypes', cls);
dependencies = [TabsProvider].concat(dependencies)
Reflect.defineMetadata('design:paramtypes', params, newCls);
// Define new onPageWillEnter and onPageWillLeave implementation which implement tab show/hide functionality
// and also call the old implementation saved above (if they exist)
newCls.prototype.onPageWillEnter = function() {
this.tabsProvider.hideTabs();
enter.apply(this, arguments);
};
newCls.prototype.onPageWillLeave = function() {
this.tabsProvider.showTabs();
leave.apply(this, arguments);
};
return newCls;
}
}
我还建议在@Page装饰器下面移动@hideTabs装饰器