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

相关内容

热门资讯

透视烘培!aa poker辅助... 透视烘培!aa poker辅助,AApoker透视软件总是有工具,八分钟教程(有挂存在)aa pok...
透视总结!wpk辅助哪里买,w... 透视总结!wpk辅助哪里买,wpk控制牌是真的吗(透视)果然有技巧(哔哩哔哩)1、下载好透视辅助下载...
透视策略!德普之星辅助软件,德... 透视策略!德普之星辅助软件,德普之星工具切实存在有插件,第2分钟教程(有挂教学)德普之星辅助软件能透...
一直以来!aapoker透视插... 一直以来!aapoker透视插件,微信小程序微乐破解器2024,揭幕透视(新版有挂)-哔哩哔哩1、首...
透视好友!pokemmo手机脚... 透视好友!pokemmo手机脚本辅助器,微乐贵阳足鸡麻将开挂,科普透视(今日头条)-哔哩哔哩poke...
透视积累!德普之星透视辅助软件... 透视积累!德普之星透视辅助软件下载,德普之星私人局总是是有透视,第3分钟教程(确实有挂)1、玩家可以...
透视手段!wpk辅助哪里买,W... 透视手段!wpk辅助哪里买,WPK有透视一直真的是有工具,7分钟教程(有挂透视)1、玩家可以在wpk...
这一现象值得深思!xpoker... 这一现象值得深思!xpoker辅助器,微乐自建房辅助软件功能,总结脚本(有挂解惑)-哔哩哔哩xpok...
透视绝活!wepoker软件辅... 透视绝活!wepoker软件辅助程序,WePoKer透视果然真的是有工具,8分钟教程(有挂秘诀)1、...
今日!wepoker好友房开挂... 今日!wepoker好友房开挂,微乐春天扑克开挂辅助插件,开挂脚本(有挂存在)-哔哩哔哩1、上手简单...