Angular 7 CDK Portal:错误 必须提供一个用于附加的门户
创始人
2024-10-16 17:01:01
0

在Angular 7中,如果你在使用Angular CDK的Portal时遇到了"错误 必须提供一个用于附加的门户",通常是因为你没有提供一个用于附加的门户。

要解决这个问题,你需要确保你在使用Portal时提供了一个正确的门户。

以下是一个示例代码,展示了如何使用Angular CDK Portal并提供一个门户:

  1. 首先,确保你已经安装了Angular CDK。你可以通过以下命令来安装它:
npm install @angular/cdk
  1. 导入所需的Angular CDK模块和组件:
import { Component, ViewChild, TemplateRef, ViewContainerRef } from '@angular/core';
import { CdkPortal, CdkPortalOutlet } from '@angular/cdk/portal';
  1. 在组件中定义一个TemplateRefViewContainerRef,并使用@ViewChild装饰器将其与HTML模板中的相应元素进行关联:
@Component({
  selector: 'app-example',
  template: `
    
      
      

This is the content of the portal

`, }) export class ExampleComponent { @ViewChild('portalContent', { static: true }) portalContent: TemplateRef; @ViewChild('portalContainer', { read: ViewContainerRef, static: true }) portalContainer: ViewContainerRef; }
  1. 在组件类中,使用CdkPortalCdkPortalOutlet来创建并附加一个门户:
export class ExampleComponent {
  @ViewChild('portalContent', { static: true }) portalContent: TemplateRef;
  @ViewChild('portalContainer', { read: ViewContainerRef, static: true })
  portalContainer: ViewContainerRef;

  ngAfterViewInit() {
    const portal = new CdkPortal(this.portalContent);
    this.portalContainer.attach(portal);
  }
}

通过这种方式,你就可以将portalContent模板的内容附加到portalContainer元素中。

请注意,在ngAfterViewInit生命周期钩子中附加门户是很重要的,因为在这个时候模板和视图容器已经准备好了。

希望这个例子能帮助你解决"错误 必须提供一个用于附加的门户"的问题!

相关内容

热门资讯

记者揭秘!德普之星辅助工具如何... 记者揭秘!德普之星辅助工具如何设置,hhpoker德州真的假的,揭秘教程(有挂软件)1、很好的工具软...
盘点一款!pokemmo手机脚... 盘点一款!pokemmo手机脚本辅助器,wepoker有辅助功能吗,解密教程(有挂辅助);建议优先通...
解密关于!wepoker辅助是... 1、解密关于!wepoker辅助是真的吗,wpk辅助器,AI教程(有挂技巧)。2、wepoker辅助...
推荐攻略!we-poker辅助... 推荐攻略!we-poker辅助,hhpoker有后台操作吗,切实教程(有挂辅助)关于hhpoker有...
每日必备!pokemmo手机版... 每日必备!pokemmo手机版透视脚本,wpk辅助器,透明教程(有挂神器);建议优先通过pokemm...
一分钟揭秘!hhpoker是正... 《一分钟揭秘!hhpoker是正规的吗,wepoker怎么挂底牌,玩家教你(有挂透明)》 hhpok...
我来教教大家!wepoker看... 我来教教大家!wepoker看底牌,约局吧德州可以透视吗,揭秘攻略(有挂透视)1、不需要AI权限,帮...
记者揭秘!epoker透视底牌... 记者揭秘!epoker透视底牌,大菠萝辅助器,2025教程(有挂方法);玩家必备必赢加哟《13670...
教程攻略!pokemmo辅助器... 1、教程攻略!pokemmo辅助器手机版下载,智星德州插件2024最新版,玩家教程(有挂技巧)2、进...
玩家交流!hhpoker必备开... 玩家交流!hhpoker必备开挂,wepoker有没有辅助,微扑克教程(有挂攻略);1分钟了解详细教...