AngularMaterialv15Update-ThemeNotAppliedtoLegacyButtons
创始人
2024-10-28 11:01:42
0

问题在代码中使用Angular Material提供的CDK Overlay模块。

代码示例:

首先,引入Overlay模块:

import { OverlayModule } from '@angular/cdk/overlay';

然后,创建一个主题工厂:

import { ThemePalette } from '@angular/material/core'; import { MatButton } from '@angular/material/button';

@Injectable() export class MyButtonThemeFactory { constructor(private _palette: ThemePalette) {}

createButtonWithTheme(): typeof MatButton { const MyButtonWithTheme = class extends MatButton { constructor(...args: any[]) { super(...args); this.color = this._palette; } };

return MyButtonWithTheme;

} }

然后,在组件中使用CDK Overlay模块创建一个新的按钮:

import { Overlay } from '@angular/cdk/overlay';

@Component({ template: , }) export class MyComponent { @ViewChild('buttonRef') buttonRef: MatButton;

constructor( private _overlay: Overlay, private _buttonThemeFactory: MyButtonThemeFactory, ) {}

ngAfterViewInit() { const MyButtonWithTheme = this._buttonThemeFactory.createButtonWithTheme();

const positionStrategy = this._overlay
  .position()
  .flexibleConnectedTo(this.buttonRef._elementRef)
  .withPositions([
    {
      originX: 'center',
      originY: 'bottom',
      overlayX: 'center',
      overlayY: 'top',
    },
  ]);

const overlayRef = this._overlay.create({
  positionStrategy,
  scrollStrategy: this._overlay.scrollStrategies.reposition(),
  hasBackdrop: true,
  backdropClass: 'cdk-overlay-transparent-backdrop',
  panelClass: 'my-panel',
});

overlayRef
  .backdropClick()
  .subscribe(() => overlayRef.detach());

const buttonPortal = new ComponentPortal(MyButtonWithTheme);

const buttonRef = overlayRef.attach(buttonPortal);

} }

这样就可以在Angular Material v15里更新工程中遗留的按钮样式,保证所设置的主题可以应用于所有按钮。

相关内容

热门资讯

第4分钟了解!牛财神插件!好像... 第4分钟了解!牛财神插件!好像存在有辅助脚本(有挂神器)-哔哩哔哩1、完成牛财神插件有辅助插件,帮助...
一分钟了解!悠闲川南游戏辅助!... 一分钟了解!悠闲川南游戏辅助!其实是真的有辅助工具(真的有挂)-哔哩哔哩1、一分钟了解!悠闲川南游戏...
第一分钟了解!微信小程序怎么挂... 第一分钟了解!微信小程序怎么挂脚本!果然一直都是有辅助app(有挂技巧)-哔哩哔哩1、微信小程序怎么...
第5分钟了解!开心游戏辅助器!... 第5分钟了解!开心游戏辅助器!竟然一直都是有辅助脚本(有挂方法)-哔哩哔哩开心游戏辅助器能透视中分为...
第六分钟了解!菠萝神器辅助工具... 第六分钟了解!菠萝神器辅助工具!总是是真的有辅助攻略(确实有挂)-哔哩哔哩1、超多福利:超高返利,海...
第6分钟了解!新天道大厅辅助!... 第6分钟了解!新天道大厅辅助!都是一直总是有辅助技巧(有挂技巧)-哔哩哔哩运新天道大厅辅助辅助工具,...
第1分钟了解!老夫子拼十辅助!... 第1分钟了解!老夫子拼十辅助!一直是有辅助神器(有挂技术)-哔哩哔哩老夫子拼十辅助破解侠是真的助透视...
第九分钟了解!三江互娱辅助!真... 第九分钟了解!三江互娱辅助!真是存在有辅助软件(有挂助手)-哔哩哔哩1、三江互娱辅助免费脚本咨询教程...
第6分钟了解!九天大厅挂价格!... 第6分钟了解!九天大厅挂价格!其实是有辅助方法(确实有挂)-哔哩哔哩1、九天大厅挂价格辅助器安装包、...
三分钟了解!手机挂机软件!其实... 三分钟了解!手机挂机软件!其实一直都是有辅助app(讲解有挂)-哔哩哔哩手机挂机软件透视方法中分为三...