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

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

相关内容

热门资讯

详情透视!aapoker怎么设... 详情透视!aapoker怎么设置提高好牌几率,aapoker安装包怎么使用,切实真的是有辅助技巧(哔...
普及透视!wepoker游戏下... 普及透视!wepoker游戏下载,wepoker买脚本靠谱吗,一贯一直总是有辅助脚本(哔哩哔哩)透视...
解谜透视!wpk有那种辅助吗,... 解谜透视!wpk有那种辅助吗,wpk德州局透视,好像一直总是有辅助app(哔哩哔哩)1、德州局透视免...
解密透视!hhpoker透视方... 解密透视!hhpoker透视方法,hhpoker软件靠谱吗,本来是真的有辅助软件(哔哩哔哩)1、破解...
推荐透视!aapoker辅助软... 推荐透视!aapoker辅助软件合法吗,aapoker怎么提高中牌率,好像有辅助攻略(哔哩哔哩)1、...
有挂透视!aapoker辅助怎... 有挂透视!aapoker辅助怎么用,aapoker脚本怎么用,都是是真的有辅助技巧(哔哩哔哩)1、脚...
开挂透视!德普之星透视辅助软件... 开挂透视!德普之星透视辅助软件,德普之星辅助器app,本来一直都是有辅助脚本(哔哩哔哩)1、超多福利...
详细透视!wpk有作比弊吗,w... 详细透视!wpk有作比弊吗,wpk是真的还是假的,其实真的是有辅助插件(哔哩哔哩)1、模拟器是什么优...
有挂透视!hhpoker是真的... 有挂透视!hhpoker是真的假的,hhpoker德州牛仔视频,一直一直都是有辅助工具(哔哩哔哩)1...
揭幕透视!aapoker脚本怎... 揭幕透视!aapoker脚本怎么用,aapoker透视插件,总是真的是有辅助攻略(哔哩哔哩)1、揭幕...