任何适用的指令或 div 元素均未提供 ngClass 属性

问题描述 投票:0回答:5

如果我想使用 ngClass、ngStyle 或 ng如果我收到以下错误消息:

我的 app.module.ts 包含 BrowserModule 和 CommonModule 导入。我的组件在声明中。

我尝试重新启动 ngserve 和 npm install。

有什么想法可以解决这个问题吗?

html angular angular-cli
5个回答
6
投票

正如您所说,当您的项目在浏览器中构建/启动时,一切正常。所以这是你的 IDE 的问题。

您有多种解决方案

  • 更新你的 Intellij,如果它已经更新了......
  • ...使您的缓存无效,如果它仍然不起作用
  • 删除节点模块并重新创建它(这解决了您的问题https://youtrack.jetbrains.com/issue/WEB-51326
  • 重新安装/创建新项目
  • 或者...在 Intellij 修复 bug 时切换到 VSCode

1
投票

如果@Pilpo 的答案对您不起作用,请尝试删除

.idea
文件夹并重新启动 IntellJ。 这对我来说确实有效。


0
投票

这对我不起作用。

我尝试了数千种方法,但仍然不起作用。

切换到 VSCode 是最好的选择


0
投票

我使用 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,所以根据框架,它可能不是最好的解决方案。


-2
投票

您可以在页面顶部添加这个

<!--suppress ALL-->
,直到Intellij解决这个问题。

<!--suppress ALL-->
<button [ngStyle]="{'color': color}"></button>
<div [ngClass]="'first second'"></div>
© www.soinside.com 2019 - 2024. All rights reserved.