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

Angular 17 MatSelect报NG05105错误,导入动画模块仍异常

解决Angular 17中MatSelect的NG05105动画错误

错误原因分析

这个错误源于Angular Material组件(如MatSelect)依赖动画模块,但你在独立组件层级错误配置了动画提供器,导致动画初始化异常。同时代码中DatePipe的导入方式也不符合独立组件的规范。

解决方案步骤

1. 全局配置动画模块

Angular 17独立应用中,动画配置需要在应用根层级(main.ts)提供,而非单个组件内部。

修改main.ts,将provideAnimations()加入bootstrapApplication的providers数组:

import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { provideAnimations } from '@angular/platform-browser/animations';

bootstrapApplication(AppComponent, {
  providers: [
    // 保留原有app配置的providers,新增动画提供器
    provideAnimations()
  ]
}).catch(err => console.error(err));

同时移除组件类中的provideAnimations():

// 从组件的providers数组中删除这一行
// provideAnimations()

2. 修正DatePipe的使用方式

独立组件不能直接将DatePipe添加到imports数组,需改为在组件的providers中声明:

@Component({
  selector: 'app-legend',
  standalone: true,
  imports: [
    MatListModule, 
    MatIconModule, 
    MatDividerModule,
    MatSelectModule, 
    MatTooltipModule, 
    MatFormFieldModule, 
    FormsModule
  ],
  providers: [DatePipe], // 在此处添加DatePipe
  templateUrl: './legend.component.html',
  styleUrl: './legend.component.css',
})

3. 排查重复配置

确保应用中没有同时导入BrowserAnimationsModule和NoopAnimationsModule,也不要在组件和根配置中重复提供动画相关配置。

修改后的完整组件代码

import { Component } from '@angular/core';
import { MatDividerModule } from '@angular/material/divider';
import { MatIconModule } from '@angular/material/icon';
import { DatePipe } from '@angular/common';
import { MatListModule } from '@angular/material/list';
import { MatSelectModule } from '@angular/material/select';
import { MatTooltipModule } from '@angular/material/tooltip';
import { MatFormFieldModule } from '@angular/material/form-field';
import { FormsModule } from '@angular/forms';

export interface legend {
  name: string;
  image: string;
  tooltip: string;
}
export interface maplist {
  name: string;
  legends: Array<legend>
}

@Component({
  selector: 'app-legend',
  standalone: true,
  imports: [
    MatListModule, 
    MatIconModule, 
    MatDividerModule,
    MatSelectModule, 
    MatTooltipModule, 
    MatFormFieldModule, 
    FormsModule
  ],
  providers: [DatePipe],
  templateUrl: './legend.component.html',
  styleUrl: './legend.component.css',
})
export class LegendComponent {
  selectedValue: Array<legend> = [];

  maplistdata: maplist[] = [
    {
      name: "Echocondria",
      legends: [
        {
          name: "House",
          image: "",
          tooltip: "Living quarters for the residents."
        },
        {
          name: "Shop",
          image: "",
          tooltip: "Places to buy and sell goods."
        },
      ]
    },
    {
      name: "Echocondria Sewers",
      legends: [
        {
          name: "House",
          image: "",
          tooltip: "Living quarters for the residents."
        },
        {
          name: "Shop",
          image: "",
          tooltip: "Places to buy and sell goods."
        },
      ]
    },
  ]
}

额外提示

  • 若不需要动画效果,可在main.ts中用provideNoopAnimations()替代provideAnimations()。
  • 确保Angular Material版本与Angular 17兼容,避免版本不匹配引发异常。

内容的提问来源于stack exchange,提问作者Alice Genetrix

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 22:10:58