英文:
Attribute Directive is mark as error even on newly created Angular Project
问题
我在 Visual Studio Code 中新建的 Angular 项目遇到了问题,像 [ngClass]、[ngSwitch] 等属性指令在 .html 文件中显示为错误。下面是通过 ng new
在 app.module.ts 中,我按照各种在线建议导入了 CommonModule 如下:
添加了它后,.html 文件中的错误仍然存在。但是当我运行 ng serve 命令时,这个错误并不会导致任何问题,页面可以在浏览器中正常显示。
我尝试在网上查找了一些信息,但找不到类似的问题。
下面是我工作区的 Angular 版本:
由于我对 Angular 还很新,除了尝试在 app.module.ts 中导入 CommonModule 外,没有做太多事情。我几乎花了三天的时间进行了大量的搜索和阅读,但找不到类似的问题。我也尝试了卸载并重新安装 Angular 语言服务,但效果不佳。
非常感谢各位开发者的帮助。提前感谢大家。
英文:
I've encounter problem with newly created Angular Project in Visual Studio Code where attribute directive like [ngClass], [ngSwitch] and etc is shown as error in .html file. Below is the standard app.component.html created as part of ng new <proj name>.
In app.module.ts, I've import CommonModule as suggested in various online site as below: -
After adding it, the error in .html file is still there. However this error does not give any issue when I run ng serve command and the page is able to be viewed in browser and working fine.
I have tried do some read up online but can't find any which is having similar issue.
Below is the Angular version for my workspace: -
Since I'm new to Angular, nothing much has been done apart from trying out some suggestion found online like import CommonModule in app.module.ts. I have spend almost 3 days doing a lot of search and read up but can't find much having similar issue. I did uninstall and reinstall Angular Language Service but to no effect.
Help from fellow developer will be greatly appreciated. Thanks everyone in advance
[updated with code]
partial of app.component.html file
<div class="terminal" [ngSwitch]="selection.value"> <!-- [ngSwitch] giving error -->
<pre *ngSwitchDefault>ng generate component xyz</pre>
<pre *ngSwitchCase="'material'">ng add @angular/material</pre>
<pre *ngSwitchCase="'pwa'">ng add @angular/pwa</pre>
<pre *ngSwitchCase="'dependency'">ng add _____</pre>
<pre *ngSwitchCase="'test'">ng test</pre>
<pre *ngSwitchCase="'build'">ng build</pre>
</div>
app.module.ts file
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
CommonModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Angular CLI Version
Angular CLI: 16.1.4
Node: 18.16.1
Package Manager: npm 9.5.1
OS: win32 x64
Angular: 16.1.6
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1601.4
@angular-devkit/build-angular 16.1.4
@angular-devkit/core 16.1.4
@angular-devkit/schematics 16.1.4
@angular/cli 16.1.4
@schematics/angular 16.1.4
rxjs 7.8.1
typescript 5.1.6
Error message: -
(directive) NgSwitch
Can't bind to 'ngSwitch' since it isn't a known property of 'div'.ngtsc(-998002)
app.component.ts(2, 26): Error occurs in the template of component AppComponent.
[Update - Comparison between my old Angular test project]
Sorry, it's easier to show in image rather than code.
I suspect it's some version compatibility issue since when I remove CommonModule from my older version test project, the error shown is different from the error given in newer version. Please help to enlighten.
Angular version 16.1.6 vs 14.1.1
But obviously I'm not expert in this hence I'm looking for help here. Thanks again
答案1
得分: 0
我已经成功找到了问题所在。这是由于Angular语言服务引起的。
以下是所采取的步骤:
- 更新Visual Studio Code版本(对于我的情况是1.80.1)
- 卸载然后重新安装Angular语言服务扩展
在上述步骤之后,像[ngClass],[ngSwitch]等属性指令在.html文件中不再显示错误。
进一步验证是通过从app.module.ts文件中移除CommonModule并检查错误消息来完成的,现在显示的错误与我在使用Angular版本14.1.1的测试项目中显示的错误相同。
希望这对于遇到类似问题的人有所帮助。
感谢@Rituparna Warwatkar提醒了我关于VS Code的问题,让我尝试在这个方向上做一些工作。
英文:
I have managed to pinpoint the problem. It is due to Angular Language Service.
Below are the step taken: -
- Update Visual Studio Code version (for my case is 1.80.1)
- Uninstall and then re-install Angular Language Service Extension
After the above step, attribute directive like [ngClass], [ngSwitch] and etc no longer show error in .html file.
Further verification is done by removing CommonModule from app.module.ts file and check the error message, now the error shown is the same as error shown in my test project with Angular version 14.1.1
Hope this help whoever who have similar issue.
Thanks and have a great day
Thanks to @Rituparna Warwatkar for highlighting on VS Code which make me try to work on something on this direction.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论