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能... 玩家必看教程“wepoker能开挂吗”开挂辅助工具(透视)分享教程是一款可以让一直输的玩家,快速成为...
开挂辅助插件"wpk... wpk真吗 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、软件...
玩家爆料“wepoker透视脚... 无需打开直接搜索加微信客服(136704302)咨询了解wepoker透视脚本连接辅助器目前(微信:...
开挂辅助神器"德普之... 开挂辅助神器"德普之星透视软件免费入口官网"开挂(透视)辅助平台(有挂方法) 了解更多开挂安装加(1...
盘点几款“wepoker辅助i... 盘点几款“wepoker辅助ios”开挂辅助挂(透视)新版2026教程是一款可以让一直输的玩家,快速...
开挂辅助脚本"pok... 开挂辅助脚本"pokerrrr2辅助"开挂(透视)辅助下载(有挂秘笈) 【无需打开直接搜索加薇136...
技术分享“微扑克发牌规律”开挂... 微扑克发牌规律是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加我微信...
开挂辅助软件"hhp... hhpoker德州有挂吗是一款专注玩家量身打造的游戏记牌类型软件,在hhpoker德州有挂吗这款游戏...
盘点一款“wpk辅助器”开挂辅... >>您好:wpk辅助器确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,总是好牌,而...