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生命周期钩子中附加门户是很重要的,因为在这个时候模板和视图容器已经准备好了。

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

相关内容

热门资讯

第二分钟辅助!wepokerp... 第二分钟辅助!wepokerplus脚本,hhpoker德州牛仔视频,机巧教程(有挂技术)1、全新机...
四分钟辅助!HH平台挂,pok... 四分钟辅助!HH平台挂,pokemmo修改器手机版,攻略教程(有挂存在)1、pokemmo修改器手机...
第2分钟辅助!wepoker底... 第2分钟辅助!wepoker底牌透视,aapoker怎么开辅助器,演示教程(有挂解惑)1、玩家可以在...
第2分钟辅助!wpk透视怎么安... 第2分钟辅助!wpk透视怎么安装,wepoker手机助手,手段教程(有挂教学)1、wpk透视怎么安装...
七分钟辅助!wepoker免费... 您好,wepoker免费脚本弱密码这款游戏可以开挂的,确实是有挂的,需要了解加去威信【1367043...
第6分钟辅助!wejoker开... 第6分钟辅助!wejoker开挂,aa poker辅助,妙计教程(有挂细节)1、aa poker辅助...
6分钟辅助!德州透视脚本,hh... 6分钟辅助!德州透视脚本,hh poker软件,教程书教程(真的有挂)1、任何德州透视脚本透视是真的...
十分钟辅助!pokemmo手机... 十分钟辅助!pokemmo手机版脚本免费,wepoker祈福有用吗,手段教程(有挂方式)1、poke...
第2分钟辅助!wepoker免... 第2分钟辅助!wepoker免费脚本咨询,wepoker透视器免费,积累教程(有挂总结)wepoke...
第六分钟辅助!aapoker怎... 第六分钟辅助!aapoker怎么设置提高好牌几率,德普之星透视免费,法子教程(有挂猫腻)所有人都在同...