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

相关内容

热门资讯

辅助透视"wepok... 辅助透视"wepokerh5破解"详细辅助必赢教程(辅助工具技巧);人气非常高,ai更新快且高清可以...
透视中牌率"htx矩... 透视中牌率"htx矩阵wepoker辅助"详细辅助透视教程(私人局透视技巧)是一款可以让一直输的玩家...
透视能赢"wepok... 透视能赢"wepoker科技辅助器"详细辅助AA德州教程(辅助器透明);玩家必备必赢加哟《13670...
透视插件"wejok... 透视插件"wejoker辅助软件"详细辅助教你攻略(游戏下载透视)1、很好的工具软件,可以解锁游戏的...
透视智能ai"wep... 这是一款非常优秀的wepoker怎么看底牌 ia辅助检测软件,能够让你了解到wepoker怎么看底牌...
透视最新"hhpok... 透视最新"hhpoker透视工具"详细辅助透明教程(插件功能辅助器方法);小薇(透视辅助)致您一封信...
透视安装"wepok... 1、透视安装"wepoker有辅助功能吗"详细辅助AA德州教程(高级辅助透明)。2、wepoker有...
透视软件"wepok... 透视软件"wepoker手机版辅助"详细辅助AI教程(可以增加运气辅助);支持2-10人实时对战,虚...
透视存在"黑侠破解w... 1、透视存在"黑侠破解wepoker"详细辅助可靠教程(辅助器v3.3教程)2、进入游戏-大厅左侧-...
透视有挂"wepok... 透视有挂"wepoker辅助器安装包"详细辅助可靠教程(辅助器下载神器)关于wepoker辅助器安装...