Angular中@Component在模板中使用`require(...)`无法正常工作
创始人
2024-10-31 00:01:12
0

推荐使用import而不是require来加载模块。如果非要使用require,可以使用Angular提供的NgModuleFactoryLoader来动态加载组件。

示例代码:

使用import加载模块:

import { Component } from '@angular/core';
import { MyService } from './my.service';

@Component({
  selector: 'app-my-component',
  template: `
    
Welcome, {{ name }}!
`, }) export class MyComponent { constructor(private myService: MyService) {} name = this.myService.getName(); loadMyOtherComponent() { import('./my-other-component/my-other-component.component').then(module => { // Do something with the module, such as rendering the dynamic component }); } }

使用NgModuleFactoryLoader动态加载组件:

import { Component, NgModuleFactoryLoader, ViewChild, ViewContainerRef } from '@angular/core';

@Component({
  selector: 'app-my-component',
  template: `
    
`, }) export class MyComponent { @ViewChild('target', { read: ViewContainerRef }) target: ViewContainerRef; constructor(private loader: NgModuleFactoryLoader) {} loadMyOtherComponent() { this.loader.load('./my-other-component/my-other-component.module#MyOtherComponentModule').then(factory => { const moduleRef = factory.create(this.target.injector); const componentFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(MyOtherComponent); this.target.clear(); this.target.createComponent(componentFactory); }); } }

相关内容

热门资讯

玩家爆料“WePoKer透视软... WePoKer透视软件挂下载是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用...
盘点十款“wpk辅助kaq66... >>您好:wpk辅助kaq665确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,总...
最新通报“wepoker有透视... 您好,wepoker有透视软件挂吗这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302...
必备教程“wepoker真的能... >>>您好:,软件加微信【添加136704302】确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
专业讨论“wpk德州最新版”开... 无需打开直接搜索加微信客服(136704302)咨询了解wpk德州最新版辅助器目前(微信:13670...
交流学习经验“wepoker有... >>您好:wepoker有没有透视挂软件加扣扣群确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
技巧知识分享“wepoker是... wepoker是不是有辅助是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户...
一分钟揭秘“wepoker辅助... 无需打开直接搜索;操作使用教程:一分钟揭秘“wepoker辅助开挂方法”开挂辅助器(透视)线上教程1...
技术分享“wepoker是不是... >>您好:wepoker是不是有透视软件软件加扣扣群确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
玩家必看攻略“德州wepoke... >>您好:德州wepoker透视挂确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,...