出于学习目的,我有一个场景并尝试使用能够动态更改路由器插座生成的组件类的服务。
在此之后,我拥有带有 BehaviorSubject 的服务,该服务的值由函数切换。
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class DynamicClassesService {
private stateRouterOutlet: Subject<string> = new BehaviorSubject<string>('am-topMenu');
stateRouterOutlet$ = this.stateRouterOutlet.asObservable();
toggleStateRouterOutlet() {
let current;
this.stateRouterOutlet.subscribe(res => { current = res });
current = (current === 'am-topMenu' ? 'am-leftMenu' : 'am-topMenu');
this.stateRouterOutlet.next(current);
console.log('Toggled to: ' + current);
}
我需要下一步的帮助。我确实从子组件订阅,并在构造函数中获取值并将其存储在本地(组件的)变量中。(也许是错的)
如果我将它(localStateRouterOutlet)声明为有任何意义@Input()
吗?
localStateRouterOutlet: string;
constructor(private dynamicClasses: DynamicClassesService) {
dynamicClasses.stateRouterOutlet$.subscribe(res => {
this.localStateRouterOutlet = res;
console.log(this.localStateRouterOutlet);
})
}
从服务管理切换功能的线索在哪里对这个变量有影响?
如果我绑定
[ngClass] = "localStateRouterOutlet"
它应该为我的最初目的工作吗?这是完全错误的概念吗?
我非常感谢您提前提供帮助。