Angular 17新项目运行异常求助:NG0203错误排查
解决Angular 17 NG0203错误的实操步骤
清理项目依赖与缓存
- 先关掉正在运行的
ng serve进程 - 执行命令删除旧依赖和锁文件:
# Mac/Linux环境 rm -rf node_modules package-lock.json npm cache clean --force# Windows环境 rmdir /s /q node_modules del package-lock.json npm cache clean --force - 重新安装依赖:
npm install
- 先关掉正在运行的
对齐Angular CLI版本
运行ng version查看本地CLI版本,确保和项目的17.0.8一致。如果版本不匹配,全局安装对应版本:npm install -g @angular/cli@17.0.8确认main.ts代码合规
打开项目根目录的main.ts,确保是Angular 17默认的启动代码,不要在非注入上下文里调用inject():import { bootstrapApplication } from '@angular/platform-browser'; import { appConfig } from './app/app.config'; import { AppComponent } from './app/app.component'; bootstrapApplication(AppComponent, appConfig) .catch((err) => console.error(err));重启服务并清浏览器缓存
- 重新启动
ng serve - 打开浏览器后按
Ctrl+Shift+R(Windows/Linux)或Cmd+Shift+R(Mac)强制刷新,清除浏览器缓存
- 重新启动
内容的提问来源于stack exchange,提问作者Frunobulax
相关产品推荐
相关产品推荐

