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

相关内容

热门资讯

玩家必看攻略!约局吧透视功能神... 玩家必看攻略!约局吧透视功能神器,约局吧果然存在有透视神器(有辅助挂辅教程)玩家必看攻略!约局吧透视...
终于清楚!新蓝鲸辅助器功能方法... 终于清楚!新蓝鲸辅助器功能方法,新蓝鲸一直是真的有辅助器方法(有辅助挂规律)1.新蓝鲸 选牌创建新账...
2分钟细说!斗娱斗地主辅助器功... 2分钟细说!斗娱斗地主辅助器功能软件,斗娱斗地主一贯真的是有辅助器软件(有挂辅方法)1、每一步都需要...
玩家必看教程!新超凡透视挂功能... 玩家必看教程!新超凡透视挂功能app,新超凡果然是真的有透视挂app(有辅助挂详细)1)新超凡有没有...
一分钟揭秘!快乐吧开挂功能ap... 一分钟揭秘!快乐吧开挂功能app,快乐吧竟然是有开挂app(有辅助挂辅助);1、每一步都需要思考,不...
2分钟细说!赣牌圈打炸透视功能... 2分钟细说!赣牌圈打炸透视功能技巧,赣牌圈打炸真是是有透视技巧(有辅助挂详情)1、赣牌圈打炸模拟器是...
最新通报!海南骨牌辅助器功能攻... 最新通报!海南骨牌辅助器功能攻略,海南骨牌一直是真的有辅助器攻略(有辅助挂透视)1、首先打开海南骨牌...
实测交流!禅游斗地主辅助器功能... 实测交流!禅游斗地主辅助器功能技巧,禅游斗地主真是存在有辅助器技巧(有辅助挂详情)1、禅游斗地主公共...
揭秘真相!天天开心辅助器功能软... 揭秘真相!天天开心辅助器功能软件,天天开心好像是有辅助器软件(有辅助挂细节)1、天天开心免费辅助多个...
玩家必备科技!福建十三道透视功... 玩家必备科技!福建十三道透视功能技巧,福建十三道确实是有透视技巧(有辅助挂神器)1、在福建十三道插件...