我目前正在使用 PrimeNg 17 在 Angular 17 项目中执行切换主题功能。我按照 Primeng 文档中的教程进行操作:https://www.youtube.com/watch?v=5VOuUdDXRsE&embeds_referring_euri=https%3A%2F %2Fprimeng.org%2F&source_ve_path=Mjg2NjY&feature=emb_logo 这是我的
theme.service.ts
import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { SystemService } from './system.service';
@Injectable({
providedIn: 'root',
})
export class ThemeService {
constructor(
@Inject(DOCUMENT) private document: Document,
private systemService: SystemService
) {}
switchTheme(theme: string): void {
let themeLink = this.document.getElementById(
'app-theme'
) as HTMLLinkElement;
if (themeLink) {
themeLink.href = 'assets/themes/' + theme + '/theme.css';
this.systemService.setLocalStorage('theme', theme);
}
}
}
应用程序工作正常,切换主题功能也可以工作,但我在
Decorators are not valid here.ts(1206)
处收到此错误消息@Inject
。
有什么方法可以解决这个问题或禁用错误/警告吗?谢谢。
我尝试在 stackblitz 上复制您的错误,但它对我来说工作正常,您可以尝试以下更改吗?
import { inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { SystemService } from './system.service';
@Injectable({
providedIn: 'root',
})
export class ThemeService {
document: Document = inject(DOCUMENT);
constructor(
private systemService: SystemService
) {}
...
Stackblitz,主题切换器效果非常好!
Stackblitz Demo