routerLink里面的<mat-tab>有角度的材料

时间:2017-12-25 06:20:55

标签: angular angular4-router

<a routerLink = "/add"></a><mat-tab  label="Add Identity"></mat-tab>
or
<mat-tab  label="Add Identity"> <a routerLink = "/add"></a></mat-tab>.

我是Angular的新手,尝试使用上面Angular材质组件的路由。 但是,当我点击“主页”选项卡时,它不会附加网址。 对此有任何帮助。

4 个答案:

答案 0 :(得分:12)

你实际上可以将它们组合成这样的一个:

<a mat-tab-link [routerLink]="/add">Add Identity</a>

您还需要确保使用的是<nav mat-tab-nav-bar>,而不是<mat-tab-group>

文档:https://material.angular.io/components/tabs/overview#tabs-and-navigation

答案 1 :(得分:3)

这就是我的实现方式。

app.component.html

<nav mat-tab-nav-bar>
  <a mat-tab-link
  *ngFor="let link of navLinks"
  [routerLink]="link.link"
  routerLinkActive #rla="routerLinkActive"
  [active]="rla.isActive">
 {{link.label}}
</a>
</nav>
<router-outlet></router-outlet>

AppComponent

export class AppComponent implements OnInit{
  title = 'app';
  navLinks: any[];
  activeLinkIndex = -1;

  constructor(private router: Router) {
    this.navLinks = [
        {
            label: 'TabTest1',
            link: './tabtest1',
            index: 0
        }, {
            label: 'Tab Test2',
            link: './tabtest2',
            index: 1
        }, {
            label: 'Tab Test3',
            link: './tabtest3',
            index: 2
        }, 
    ];
}
ngOnInit(): void {
  this.router.events.subscribe((res) => {
      this.activeLinkIndex = this.navLinks.indexOf(this.navLinks.find(tab => tab.link === '.' + this.router.url));
  });
}
}

routing.module.ts

const routes: Routes = [
  { path: '', redirectTo: '/tabtest1', pathMatch: 'full' },
  { path: 'tabtest1', component:  TestComponent1},
  { path: 'tabtest2', component:  TestComponent2},

];

export const appRouting = RouterModule.forRoot(routes);



@NgModule({
  imports: [
    RouterModule.forRoot(routes),
    CommonModule
  ],
  exports:[
    RouterModule
  ],

  declarations: []
})
export class AppRoutingModule { }

我希望这对某人有帮助

答案 2 :(得分:0)

如果您需要在routerLink内的<mat-tab>上单击<mat-tab-group>来设置mat-tab-nav,而无需任何变通方法和直接使用(selectedTabChange)="onTabChanged($event)"逻辑事物来更改当前代码,则可以使用mat-tab-群组侦听器<mat-tab-group (selectedTabChange)="onTabChanged($event)" > <mat-tab label="Link to some thing"></mat-tab> <!-- empty stub tab only for link --> <mat-tab label="Some content tab"> ... content there ... </mat-tab> </mat-tab-group>

.html:

onTabChanged(event: MatTabChangeEvent): void {
    switch (event.index) {
      case 0: // index of the tab
        // this is our stub tab for link
        this.router.navigate(['/admin/my-link']);
        break;
      case 1:
        // do stuff with content or do nothing :)
        break;
    }

.ts:

let rMember = message.guild.member(
 message.mentions.users.first() || message.guild.members.cache.get([0])
);

let crewmate = message.guild.roles.cache.get('752639666393710614');

rMember.roles.set([
 crewmate, // add the crewmate role
 ...rMember.roles.cache.filter((role) => role.managed).array(), // and any existing `managed` roles
]);

答案 3 :(得分:0)

<nav mat-tab-nav-bar [backgroundColor]="background">
  <a mat-tab-link *ngFor="let link of links"
     (click)="activeLink = link"
     [active]="activeLink == link"> {{link}} </a>
  <a mat-tab-link disabled>Disabled Link</a>
</nav>

参考:https://material.angular.io/components/tabs/overview#tabs-and-navigation