我有一个Injectable Class和一个Component类。 我需要做的就是从Injectable类中调用Component Class中的函数。
可注射
Injectable()
export class theInjected
{
constructor(private afAuth: AngularFireAuth, private googlePlus: GooglePlus, public alertCtrl: AlertController){
this.afAuth.authState.subscribe(user =>
{
if(user)
{
this.user = this.afAuth.authState; //this means alert('fire user logged in');
//
// I need to call the function here goTopage();
//
}
else
{
Observable.of(null); //this means alert('fire user logged out');
}
}
);
}
}
组件:
@Component({
selector: 'home',
templateUrl: 'home.html'
})
export class Home
{
constructor(public injected: theInjected){
}
goTopage()
{
this.navCtrl.setRoot(this.loggedInPage);
}
}
我只想要一种从Injectable调用组件类中存在的函数的方法。
谢谢!
答案 0 :(得分:1)
使用observable并在组件中订阅它。
export class theInjected
{
observable = new BehaviorSubject();
someMethod() {
this.observable.next('foo');
}
}
export class Home
{
constructor(public injected: theInjected){
injected.observable.subscribe(val => this.Topage());
}
goTopage()
{
this.navCtrl.setRoot(this.loggedInPage);
}
}