我想跟踪我在多个组件中使用的服务的全局变量。我使用角度材料2作为我的应用程序。
最好的方法是什么? 我能做的任何例子都会有所帮助。 thanx所有你的帮助
我有这个代码,不起作用,主题不是切换。我有错误。
编译失败。
C:/Users/Developer/Documents/GitHub/STAGING/Test-/src/app/admin/pages/products/admin-add-new-product/admin-add-new-product.component.ts (20,3): Type 'Observable<any>' is not assignable to type 'Boolean'.
Types of property 'valueOf' are incompatible.
Type '() => Object' is not assignable to type '() => boolean'.
Type 'Object' is not assignable to type 'boolean'.
import { Injectable } from '@angular/core';
import {Subject} from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class MediatorService {
public isDarkTheme: Observable<any>;
/**
* Returns the selected theme
*/
getTheme () {
return this.isDarkTheme;
}
/**
* Set the selected theme
*/
setTheme (b) {
this.isDarkTheme = b;
console.log(this.isDarkTheme);
}
}
export class AdminAddNewProductComponent implements OnInit {
isDarkTheme: Boolean = this.mediator.getTheme();
}
/**
* Set the selected theme
*/
toggleTheme() {
// this.isDarkTheme = this.isDarkTheme = true ? false : true;
// console.log(this.isDarkTheme);
this.mediator.setTheme(this.isDarkTheme);
}