如果在页面加载时检测到cookie,我将尝试调用一个方法。如果检测到,它应该加载我的应用程序黑暗的主题。
我使用的是Angular 7和'ngx-cookie-service‘。
我可以很好地切换主题,但不知道如何在检测到cookie时自动切换主题。
下面是我的主题切换服务的代码:
import { Injectable, OnInit } from '@angular/core';
import { Subject } from 'rxjs';
// Import Services
import { CookieService } from 'ngx-cookie-service';
@Injectable()
export class ThemeSwitchService implements OnInit {
constructor( private cookieService: CookieService ) { }
private _themeDark: Subject<boolean> = new Subject<boolean>();
isThemeDark = this._themeDark.asObservable();
ngOnInit(): void {
const cookieValue: string = this.cookieService.get('DarkTheme');
if (cookieValue === 'True') {
setDarkTheme();
console.log('Dark theme active.')
}
}
setDarkTheme(isThemeDark: boolean) {
this._themeDark.next(isThemeDark);
if (isThemeDark) {
this.cookieService.set('DarkTheme', 'True');
console.log('Dark theme activated.')
} else {
this.cookieService.delete('DarkTheme');
console.log('Dark theme deactivated.');
}
}
}我希望能够在ngOnInit中调用主题切换方法,但我似乎无法让它工作。此错误显示在终端中:ERROR in src/app/core/services/theme-switch.service.ts(20,7): error TS2663: Cannot find name 'setDarkTheme'. Did you mean the instance member 'this.setDarkTheme'?
发布于 2019-03-02 21:04:16
ngOnInit(): void {
let cookieValue: string = this.cookieService.get('DarkTheme');
if (cookieValue === 'True') {
this.setDarkTheme(true);
console.log('Dark theme active.')
}
}请添加此代码,然后重新检查。
https://stackoverflow.com/questions/54958710
复制相似问题