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); }); } }

相关内容

热门资讯

透视私人局"小程序怎... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
分享给玩家"佛手大菠... 分享给玩家"佛手大菠萝辅助"开挂(透视)hh poker辅助有用吗(有人有挂);打开点击测试直接进入...
专业讨论"hhpok... 大家好,今天小编来为大家解答hhpoker有辅助的吗这个问题咨询软件客服可以免费测试直接加微信(13...
技术分享"微信小程序... 技术分享"微信小程序四川微乐脚本"开挂(透视)哈糖大菠萝能开挂吗(真是有挂)《详细加薇1367043...
重大通报"皇豪互娱科... 重大通报"皇豪互娱科技软件"开挂(透视)wepoker数据分析(有挂方式);打开点击测试直接进入微信...
重大发现"微信传送屋... 【亲,微信传送屋辅助器下载 这款游戏可以开挂的,确实是有挂的,很多玩家在这款微信传送屋辅助器下载中打...
实测发现"广东雀神智... 实测发现"广东雀神智能插件辅助脚本"开挂(透视)wepoker免费脚本咨询(有挂秘籍)【无需打开直接...
每日必看教程"建德十... 每日必看教程"建德十三道辅助"开挂(透视)aapoker插件下载(了解有挂) 了解更多开挂安装加(1...
玩家必看秘籍"红河西... 您好:这款红河西元旗牌脚本游戏是可以开挂的,确实是有挂的,很多玩家在这款红河西元旗牌脚本游戏中打牌都...
揭秘几款"四川长牌辅... 揭秘几款"四川长牌辅助"开挂(透视)wepoker破解器(有挂技术);无需打开直接搜索薇:13670...