AngularMaterials在动态创建的独立组件中无法正常工作在Angular14-15中。
创始人
2024-10-28 10:34:26
0

在Angular 14-15中,使用Angular Materials在动态创建的独立组件中可能会出现问题。最好的解决方法是确保在“NgModule”中将“CommonModule”和“MatButtonModule”导入到需要使用Angular Materials的组件中。

在下面的示例代码中,我们可以看到如何导入这些模块,并在独立组件中使用Button。组件的样式和生成在DOM中的方式不会对这个问题的解决方法产生影响。

app.module.ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { MyDynamicComponent } from './my-dynamic.component';
import { MatButtonModule } from '@angular/material/button';
import { CommonModule } from '@angular/common';

@NgModule({
  imports: [BrowserModule, CommonModule, MatButtonModule],
  declarations: [AppComponent, MyDynamicComponent],
  bootstrap: [AppComponent],
  entryComponents: [MyDynamicComponent],
})
export class AppModule { }

app.component.ts:

import { Component } from '@angular/core';
import { MyDynamicComponent } from './my-dynamic.component';
import { ViewContainerRef, ViewChild, AfterViewInit, ComponentFactoryResolver } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
    
    
`, }) export class AppComponent implements AfterViewInit { @ViewChild('container', { read: ViewContainerRef }) container: ViewContainerRef; constructor(private resolver: ComponentFactoryResolver) { } ngAfterViewInit() { this.createComponent(); } createComponent() { this.container.clear(); const factory = this.resolver.resolveComponentFactory(MyDynamicComponent); const componentRef = this.container.createComponent(factory); } }

my-dynamic.component.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'my-dynamic-component',
  template: '',
})

相关内容

热门资讯

透视规律“大当家辅助脚本下载”... 透视规律“大当家辅助脚本下载”一向有辅助软件(发现有挂)您好:大当家辅助脚本下载这款游戏可以开挂,确...
专业讨论“黑侠破解约局吧”原先... 专业讨论“黑侠破解约局吧”原先有辅助平台(有挂方略)您好:黑侠破解约局吧这款游戏可以开挂,确实是有挂...
盘点几款“wepoker模拟器... 盘点几款“wepoker模拟器哪个”本然有辅助开挂下载(有挂细节)1、下载安装好wepoker模拟器...
透视肯定“吉祥填大坑游戏攻略”... 透视肯定“吉祥填大坑游戏攻略”原本有开挂辅助器(有挂总结);无需打开直接搜索加薇136704302(...
安装程序教程“小程序家乡大二辅... 安装程序教程“小程序家乡大二辅助工具”从来有辅助插件(详细教程);无需打开直接搜索打开薇:13670...
一起来讨论“八张透视辅助”原本... 大家好,今天小编来为大家解答八张透视辅助这个问题咨询软件客服可以免费测试直接加微信(13670430...
透视好友“莆田情怀脚本辅助器”... 透视好友“莆田情怀脚本辅助器”往昔有辅助工具(确实有挂);无需打开直接搜索打开薇:136704302...
教学盘点“浙江游戏温州熟客辅助... 教学盘点“浙江游戏温州熟客辅助”起初有开挂辅助插件(竟然有挂);亲,浙江游戏温州熟客辅助这款游戏原来...
透视脚本“游戏大厅辅助器”原生... 透视脚本“游戏大厅辅助器”原生有开挂辅助脚本(今日头条);无需打开直接搜索打开薇:136704302...
一分钟揭秘“汇友游戏辅助”本来... 【亲,汇友游戏辅助 这款游戏可以开挂的,确实是有挂的,很多玩家在这款汇友游戏辅助中打牌都会发现很多用...