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

相关内容

热门资讯

终于知道!hhpoker脚本(... 终于知道!hhpoker脚本(辅助挂)好像有挂(有挂秘诀辅助脚本)1、打开软件启动之后找到中间准星的...
玩家攻略!哈糖大菠萝挂(辅助挂... 玩家攻略!哈糖大菠萝挂(辅助挂)都是是真的有挂(有挂教程辅助工具)1、用户打开应用后不用登录就可以直...
揭秘攻略!wepoker黑侠辅... 揭秘攻略!wepoker黑侠辅助器(辅助挂)竟然有挂(有挂透视辅助插件)1、许多玩家不知道辅助怎么退...
最新技巧!wepoker透视器... 最新技巧!wepoker透视器免费(辅助挂)原来真的是有挂(有挂头条辅助插件)运辅助工具,进入游戏界...
重大通报!hhpoker怎么破... 重大通报!hhpoker怎么破解(辅助挂)总是有挂(今日头条辅助器)重大通报!hhpoker怎么破解...
玩家必看科普!云扑克有透视吗(... 玩家必看科普!云扑克有透视吗(辅助挂)好像真的是有挂(果真有挂辅助方法)1、用户打开应用后不用登录就...
我来教大家!htx矩阵wepo... 我来教大家!htx矩阵wepoker辅助(辅助挂)其实存在有挂(证实有挂辅助技巧)1. 选牌创建新账...
程序员教你!pokerworl... 程序员教你!pokerworld辅助器(辅助挂)真是存在有挂(有挂攻略辅助教程)1、打开软件启动之后...
攻略讲解!hhpoker脚本(... 攻略讲解!hhpoker脚本(辅助挂)好像真的是有挂(有挂存在辅助方法)1、下载好脚本下载之后点击打...
9分钟了解!cloudpoke... 9分钟了解!cloudpoker外挂(辅助挂)确实真的有挂(有挂解惑辅助插件)辅助器是一种具有地方特...