Angular Material Tabs - 仅在选择活动选项卡时加载/卸载组件
创始人
2024-10-19 19:31:01
0

在Angular Material中,可以使用来创建选项卡。要在选择活动选项卡时加载和卸载组件,可以使用ngTemplateOutlet指令和动态组件。

首先,创建一个包含选项卡和动态组件的父组件。在父组件的模板中,使用来创建选项卡,并在每个选项卡上使用ng-template来定义动态组件。同时,使用[selectedIndex]属性来绑定当前活动选项卡的索引。


  
    {{ tab.label }}
    
      
    
  

在父组件的Typescript代码中,定义选项卡的数组和当前活动选项卡的索引。例如:

import { Component } from '@angular/core';

@Component({
  selector: 'app-tabs',
  templateUrl: './tabs.component.html',
  styleUrls: ['./tabs.component.css']
})
export class TabsComponent {
  tabs = [
    { label: 'Tab 1', index: 0, content: this.tab1Content },
    { label: 'Tab 2', index: 1, content: this.tab2Content },
    { label: 'Tab 3', index: 2, content: this.tab3Content }
  ];

  activeTabIndex = 0;

  // Define the tab contents as template references
  tab1Content = this.tab1;
  tab2Content = this.tab2;
  tab3Content = this.tab3;

  // Define the tab contents as template variables
  // You can replace these with your own components
  tab1 = this.tab1Template;
  tab2 = this.tab2Template;
  tab3 = this.tab3Template;

  // Define the templates for the tab contents
  tab1Template = `
    
  `;

  tab2Template = `
    
  `;

  tab3Template = `
    
  `;
}

在上面的代码中,tabs数组包含每个选项卡的标签、索引和内容。activeTabIndex表示当前活动选项卡的索引。tab1Contenttab2Contenttab3Content分别是指向对应选项卡内容的变量。

在父组件的模板中,使用ng-containerngTemplateOutlet来加载选中选项卡的组件内容。只有当activeTabIndex等于选项卡的索引时,才会加载对应的组件内容。

请注意,上述代码中的tab1tab2tab3是模板变量,你需要将它们替换为你自己的组件。

这样,在选择活动选项卡时,Angular Material会根据ngIf指令动态加载和卸载组件。

相关内容

热门资讯

第三分钟演示!wepoker正... 第三分钟演示!wepoker正确养号方法(透视)一直真的是有辅助安装(哔哩哔哩)1、让任何用户在无需...
1分钟手段!hhpoker免费... 1分钟手段!hhpoker免费透视脚本(透视)好像存在有辅助工具(哔哩哔哩)1、任何hhpoker免...
第3分钟模板!wejoker辅... 第3分钟模板!wejoker辅助器怎么卖(透视)原来存在有辅助教程(哔哩哔哩)运wejoker辅助器...
一分钟大纲!wepoker私人... 一分钟大纲!wepoker私人局辅助(透视)总是存在有辅助工具(哔哩哔哩)1、金币登录送、破产送、升...
第八分钟窍要!德普之星有辅助软... 第八分钟窍要!德普之星有辅助软件吗(透视)切实有辅助透视(哔哩哔哩)该软件可以轻松地帮助玩家将德普之...
第4分钟手册!wepoker好... 第4分钟手册!wepoker好友房开挂(透视)竟然是有辅助安装(哔哩哔哩)wepoker好友房开挂能...
2分钟指南书!wepoker养... 2分钟指南书!wepoker养号规律(透视)真是有辅助透视(哔哩哔哩)wepoker养号规律辅助器是...
九分钟方针!wepoker俱乐... 九分钟方针!wepoker俱乐部辅助(透视)好像真的有辅助脚本(哔哩哔哩)1、起透看视 wepoke...
8分钟妙招!德州局脚本(透视)... 8分钟妙招!德州局脚本(透视)都是是真的有辅助安装(哔哩哔哩)德州局脚本辅助器是一种具有地方特色的麻...
两分钟大纲!hhpoker脚本... 两分钟大纲!hhpoker脚本下载(透视)一直真的是有辅助开挂(哔哩哔哩)1、hhpoker脚本下载...