我正面临着构造我的角度应用程序的问题。我需要一个选项卡应用程序而不是路由,即:每当用户单击应用程序中的新链接,而不是将应用程序路由到该组件时,它将:
我最初的计划是:
请你帮我这一点,我需要指南或最佳实践的所有步骤(收集意见)。如果有一些组件或软件包帮助我实现这一点。谢谢
编辑:要清除示例,
假设我有一个从数据库加载的系统中的项目列表,当我想编辑一个项目时,我将能够在它自己的选项卡中打开它。所以我可以同时打开多个项目。当我通过新选项卡添加一个新项时,我也应该能够在它自己的选项卡中打开该记录。它们不仅仅是我想在它们之间路由的静态选项卡的列表。
发布于 2018-11-14 14:59:37
我能够通过以下方式实现我的愿望:
而不是出口
<mat-tab-group [selectedIndex]="selectedTab" backgroundColor="primary" color="primary" (selectedTabChange)="selectedTabChanged($event)" style="height:100%;">
<mat-tab label="Dashboard">
Dashboard Component -- none closable tab, does not depend on routing.
</mat-tab>
<mat-tab *ngFor="let tab of tabsComponents; index as i">
<ng-template mat-tab-label>
{{tab.title | translate}}
<button mat-icon-button class="close-icon"
(click)="closeTab($event,i)">
<mat-icon class="close-icon ">
close
</mat-icon>
</button>
</ng-template>
<ng-container *ngComponentOutlet="tab.component; injector:tab.params" ></ng-container>
</mat-tab>
</mat-tab-group>路由上的事件侦听器:
this.routerEventSub = router.events.pipe(filter(event => event instanceof NavigationEnd))
.subscribe((routeChange: NavigationEnd) => {
this.layout.adjustLayout({ route: routeChange.url });
let child = route.snapshot.firstChild.firstChild;
console.log(child);
if (this.tabsKeys.indexOf(routeChange.url) == -1) {
this.tabsKeys.push(routeChange.url);
let myParams: iTechParam = {
_params: child.params,
_queryParam: child.queryParams,
_path:router.url
}
let params = Injector.create([
{ provide: iTechParam, useValue: myParams }
], this.inj);
this.tabsComponents.push({ title: child.data['title'], component: child.component, params: params, path: router.url });
this.selectedTab = this.tabsKeys.length;
} else {
this.selectedTab = this.tabsKeys.indexOf(routeChange.url) + 1;//+1 is because the dashboard tab, none-closable one
}
});变量:
tabsKeys: string[] = [];//store the keys of the opened tabs
tabsComponents: ITechTab[] = [];// stores information about the component in each tab
selectedTab = 0; // store the index of the selected tab按索引功能关闭选项卡:
closeTabByIndex(i: number) {
this.tabsKeys.splice(i, 1);
let removedtabs = this.tabsComponents.splice(i, 1);
if (this.router.url == removedtabs[0].path) {
if (i == 0 && this.tabsComponents.length == 0) {
this.router.navigate(['/dashboard']);
} else {
this.router.navigate([this.tabsComponents[i - 1].path]);
}
}
}发布于 2018-09-11 15:57:26
还不清楚为什么您认为您必须摆脱路由使用选项卡导航。选项卡导航和路由不是不兼容的。若要在路由中使用MatTabNavBar和MatTabLink,请设置应用程序路由,以便每个“选项卡”都是路由,将路由器选项添加到mat选项卡链接元素中,并使用路由器出口托管选项卡内容。这应该比您计划的要简单得多,因为您不需要担心动态选项卡内容的“创建”和后退按钮的使用。
下面是一个基本示例中的选定代码,完整的演示是在StackBlitz上完成的
模板
<nav mat-tab-nav-bar [backgroundColor]="background">
<a mat-tab-link routerLink="test-one" routerLinkActive #rla1="routerLinkActive" [active]="rla1.isActive">Test One</a>
<a mat-tab-link routerLink="test-two" routerLinkActive #rla2="routerLinkActive" [active]="rla2.isActive">Test Two</a>
</nav>
<router-outlet></router-outlet>组件
import {Component} from '@angular/core';
/**
* @title Basic use of the tab nav bar with routing
*/
@Component({
selector: 'tab-nav-bar-basic-example',
templateUrl: 'tab-nav-bar-basic-example.html',
styleUrls: ['tab-nav-bar-basic-example.css'],
})
export class TabNavBarBasicExample {}
@Component({
selector: 'test-one',
template: '<p>Test One</p>'
})
export class TestOne {}
@Component({
selector: 'test-two',
template: '<p>Test Two</p>'
})
export class TestTwo {}App模块
const appRoutes: Routes = [
{ path: '', redirectTo: 'test-one', pathMatch: 'full' },
{ path: 'test-one', component: TestOne },
{ path: 'test-two', component: TestTwo }
];
@NgModule({
imports: [
RouterModule.forRoot(
appRoutes
),
...
],
entryComponents: [TabNavBarBasicExample],
declarations: [TabNavBarBasicExample, TestOne, TestTwo],
bootstrap: [TabNavBarBasicExample],
providers: []
})
export class AppModule { }https://stackoverflow.com/questions/52278819
复制相似问题