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

Angular13安装@angular/google-maps依赖冲突及导入报错问题

问题根因

@angular/google-maps是Angular官方维护的组件库,版本号和Angular核心框架严格对齐,跨大版本完全不兼容。在Angular 13项目里直接执行无版本号的安装命令时,npm会默认拉取最新的14.x稳定版,该版本要求peer依赖为Angular 14/15,和本地13.2.7的核心包版本冲突,就算用强制参数跳过依赖校验,编译阶段也必然会报导入错误。

解决步骤
  • 先卸载之前强制安装的不兼容版本,执行命令:
npm uninstall @angular/google-maps
  • 安装和Angular 13匹配的13.x版本,执行命令:
npm install @angular/google-maps@13

执行上述命令时npm会自动拉取13大版本下最新的兼容补丁包,和本地的@angular/common、@angular/core等核心依赖版本完全匹配,不会再报依赖树解析错误。

配置验证

安装完成后直接在app.module.ts中正常导入模块即可使用,参考代码:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { GoogleMapsModule } from '@angular/google-maps';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    GoogleMapsModule // 导入地图模块
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

补充说明:所有Angular官方生态包(包括@angular/material、@angular/cdk、@angular/fire、@angular/google-maps等)均和核心框架同版本迭代,使用时必须保证大版本号和项目Angular主版本一致,不要通过--force、--legacy-peer-deps参数强制安装跨大版本的包,后续大概率会出现编译失败、运行时异常等问题。

内容的提问来源于stack exchange,提问作者nandhabalan marimuthu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 11:36:21