如果我想使用 ngClass、ngStyle 或 ng如果我收到以下错误消息:
我的 app.module.ts 包含 BrowserModule 和 CommonModule 导入。我的组件在声明中。
我尝试重新启动 ngserve 和 npm install。
有什么想法可以解决这个问题吗?
正如您所说,当您的项目在浏览器中构建/启动时,一切正常。所以这是你的 IDE 的问题。
您有多种解决方案
如果@Pilpo 的答案对您不起作用,请尝试删除
.idea
文件夹并重新启动 IntellJ。
这对我来说确实有效。
这对我不起作用。
我尝试了数千种方法,但仍然不起作用。
切换到 VSCode 是最好的选择
我使用 PHPStorm 遇到了同样的错误, 它建议我“导入 Ngstyle”,它有效。
更改是在我的 component.ts 文件和 @component{imports} 部分中导入 NgStyle :
import {NgIf, NgStyle} from "@angular/common"; <<<
@Component({
selector: 'app-face-snap',
standalone: true,
imports: [
NgIf,
NgStyle, <<<
],
templateUrl: './face-snap.component.html',
styleUrl: './face-snap.component.scss'
})
我刚刚开始使用 AngularJs,所以根据框架,它可能不是最好的解决方案。
您可以在页面顶部添加这个
<!--suppress ALL-->
,直到Intellij解决这个问题。
<!--suppress ALL-->
<button [ngStyle]="{'color': color}"></button>
<div [ngClass]="'first second'"></div>