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

相关内容

热门资讯

9分钟了解!微信卡五星辅助器,... 9分钟了解!微信卡五星辅助器,微乐家乡麻将自建房辅助app(开挂自建房)插件1、微乐家乡麻将自建房辅...
重大通报!微乐微信自建房辅助工... 重大通报!微乐微信自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)插件1、完成微乐微信...
重大推荐!微乐小程序麻将自建房... 重大推荐!微乐小程序麻将自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)方法1、微乐小...
记者爆料!微乐自建房辅助工具插... 记者爆料!微乐自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)方法1.微乐家乡麻将自建...
重大来袭!微乐小程序自建房辅助... 重大来袭!微乐小程序自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)工具1、下载好微乐...
重磅来袭!微信微乐自建房辅助工... 重磅来袭!微信微乐自建房辅助工具,微乐小程序免费黑科技(开挂自建房)脚本1)微信微乐自建房辅助工具免...
专业讨论!微乐自建房辅助工具挂... 专业讨论!微乐自建房辅助工具挂,微乐小程序免费黑科技(开挂自建房)器1、让任何用户在无需微乐自建房辅...
终于清楚!微乐自建房辅助工具插... 终于清楚!微乐自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)app1、微乐家乡麻将自...
重大科普!微乐家乡麻将自建房辅... 重大科普!微乐家乡麻将自建房辅助app,微乐小程序免费黑科技(开挂自建房)技巧1、首先打开微乐家乡麻...
玩家必看秘籍!微信卡五星辅助器... 玩家必看秘籍!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)技巧1、任何微信卡五星辅助器透视...