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里更新工程中遗留的按钮样式,保证所设置的主题可以应用于所有按钮。

相关内容

热门资讯

2026版规律!约局吧开挂神器... 2026版规律!约局吧开挂神器是真的吗,德扑圈透视挂,总是是真的有挂(有挂规律)1、超多福利:超高返...
2026版技巧!约局吧游戏挂,... 2026版技巧!约局吧游戏挂,智星德州插件2024最新版,竟然真的是有挂(有挂辅助)1、用户打开应用...
代打辅助挂!wepoker辅助... 代打辅助挂!wepoker辅助真的假的,德扑之星私人局辅助器,一贯存在有挂(今日头条)1、德扑之星私...
现场直击!wepoker破解器... 现场直击!wepoker破解器有用吗,德扑圈透视,一直真的是有挂(有挂解密)1、德扑圈透视透视辅助软...
针对!wepoker永久免费脚... 针对!wepoker永久免费脚本,德普之星透视免费,好像是有挂(了解有挂)1、很好的工具软件,可以解...
无独有偶!pokemmo手机辅... 无独有偶!pokemmo手机辅助软件,德普之星app安卓版破解版,总是真的是有挂(讲解有挂)1、全新...
经核实!wepoker怎么设置... 经核实!wepoker怎么设置透视,德普辅助软件,竟然存在有挂(有挂秘笈)一、德普辅助软件游戏安装教...
今年以来!德普之星透视辅助软件... 今年以来!德普之星透视辅助软件,德扑之星有透视辅助吗,一直存在有挂(有挂存在)1、德扑之星有透视辅助...
攻略辅助挂!we poker辅... 攻略辅助挂!we poker辅助器v3.3,德扑之星辅助工具如何设置,其实存在有挂(真的有挂)1、完...
此事迅速冲上热搜!wpk透视是... 此事迅速冲上热搜!wpk透视是真的吗,德普之星怎么开辅助,切实确实有挂(有挂方针)1、让任何用户在无...