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辅助神器,有需要的用户...
开挂辅助下载"wep... 开挂辅助下载"wepoker可以免费玩吗"开挂(透视)辅助安装(有挂工具);1、这是跨平台的wepo...
我来分享“wpk有没有透视”开... 您好:wpk有没有透视的软件,软件加客服确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特...
开挂辅助工具"wep... 【亲,wepoker透视破解版 这款游戏可以开挂的,确实是有挂的,很多玩家在这款wepoker透视破...
开挂辅助工具"wep... 开挂辅助工具"wepoker免费脚本咨询"开挂(透视)辅助平台(有挂技巧)>>您好:软件加薇1367...
今日公布“wepoker发牌规... 您好:wepoker发牌规律表(最新版)的软件,软件加客服确实是有挂的,很多玩家在这款游戏中打牌都会...
开挂辅助平台"哈糖大... 开挂辅助平台"哈糖大菠萝有挂吗"开挂(透视)辅助插件(真实有挂);打开点击测试直接进入微信(1367...
2分钟细说“wepoker透视... 您好:wepoker透视脚本怎么修改这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发...
开挂辅助脚本"agp... 开挂辅助脚本"agpoker辅助"开挂(透视)辅助神器(有挂方式);无需打开直接搜索薇:136704...
玩家必备科技“微扑克辅助”开挂... 您好:微扑克辅助的软件,软件加客服确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,...