You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

按Angular文档引入NgOptimizedImage报错:@angular/common无该导出成员

NgOptimizedImage导入报错解决办法

出现Module "@angular/common" has no exported member 'NgOptimizedImage'报错,根本原因不是这个API被弃用,而是你的Angular版本太低——NgOptimizedImage是Angular 14.2.0及以上版本才新增的特性,低版本框架里自然找不到这个导出成员。

解决步骤:

  • 先检查当前项目的Angular版本:在终端运行ng version,看输出里的@angular/core版本号。如果低于14.2.0,必须升级版本。
  • 升级命令:
    直接升级到对应大版本的最新小版本:ng update @angular/core @angular/cli
    或者指定升级到14.2.x版本:ng update @angular/core@14.2 @angular/cli@14.2
  • 版本升级完成后,确保在你的模块(比如AppModule)里正确导入并配置:
    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { NgOptimizedImage } from '@angular/common';
    
    import { AppComponent } from './app.component';
    
    @NgModule({
      declarations: [AppComponent],
      imports: [BrowserModule, NgOptimizedImage],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

另外提一句:你看到的官方文档是对应高版本Angular的内容,低版本的Angular官方文档里不会有这个API的说明,并不是文档没同步更新。

内容的提问来源于stack exchange,提问作者David J

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 20:55:19