如何在Angular 4中跟踪全局变量
我想从我在多个组件中使用的服务中跟踪全局变量。我在我的应用程序中使用角度材质2 最好的方法是什么? 任何我能做到的例子都会有帮助。谢谢你的帮助 我有这个代码,是不是工作,主题是不切换。 我有错误 未能编译如何在Angular 4中跟踪全局变量,angular,typescript,angular-material2,Angular,Typescript,Angular Material2,我想从我在多个组件中使用的服务中跟踪全局变量。我在我的应用程序中使用角度材质2 最好的方法是什么? 任何我能做到的例子都会有帮助。谢谢你的帮助 我有这个代码,是不是工作,主题是不切换。 我有错误 未能编译 C:/Users/Developer/Documents/GitHub/STAGING/Test-/src/app/admin/pages/products/admin-add-new-product/admin-add-new-product.component.ts (20,3): Typ
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'.
该组件将主题变量切换为true或false
navbar.component.ts
问题在于,您将可观测值视为布尔值,而这正是您收到的错误消息。我希望支持angular/TS的编辑器/IDE甚至在编译之前都会向您显示一个错误。请看一看,例如本教程:。它展示了如何使用一个可观察/主体(它以角度2为目标,但这不应该是一个问题)Thnx来帮助您。问题是您将可观察视为一个布尔值,而这正是您收到的错误消息。我希望支持angular/TS的编辑器/IDE甚至在编译之前都会向您显示一个错误。请看一看,例如本教程:。它展示了如何使用一个可观察/主体(它以角度2为目标,但这应该不是问题)Thnx来帮助您
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);
}