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

相关内容

热门资讯

开挂辅助神器"hhp... 开挂辅助神器"hhpoker真的假的"开挂(透视)辅助下载(有挂技巧) 了解更多开挂安装加(1367...
开挂辅助!微信小程序游戏辅助,... 开挂辅助!微信小程序游戏辅助,wepoker辅助器安装包(透视)开挂辅助下载(有挂规律);1.微信小...
开挂辅助下载"pok... 您好:这款pokemmo脚本辅助游戏是可以开挂的,确实是有挂的,很多玩家在这款pokemmo脚本辅助...
开挂辅助!边锋干瞪眼ios辅助... 开挂辅助!边锋干瞪眼ios辅助,德州透视是真的假的(透视)开挂辅助神器(有挂教学);1、不需要AI权...
开挂辅助脚本"wep... 开挂辅助脚本"wepokerplus开挂"开挂(透视)辅助插件(有挂分享)>>您好:软件加薇1367...
开挂辅助!三哥玩辅助,hhpo... 开挂辅助!三哥玩辅助,hhpoker德州透视挂(透视)开挂辅助神器(有挂教学);三哥玩辅助免费下载原...
开挂辅助下载"wep... 大家好,今天小编来为大家解答wepoker底牌透视脚本这个问题咨询软件客服可以免费测试直接加微信(1...
辅助开挂!雀友会广东潮汕麻雀辅... 辅助开挂!雀友会广东潮汕麻雀辅助,wepoker到底有没有透视(透视)开挂辅助插件(今日头条);1、...
开挂辅助安装"wep... 开挂辅助安装"wepoker辅助是真的假的"开挂(透视)辅助安装(确实有挂);无需打开直接搜索薇:1...
辅助开挂!微乐山西小程序破解器... 辅助开挂!微乐山西小程序破解器,hhpoker德州机器人(透视)开挂辅助插件(真的有挂);微乐山西小...