How to conditionally prevent user from navigating to other tab in mat-tab-group

前端 未结 2 961
别那么骄傲
别那么骄傲 2021-01-03 11:15

I have an angular component which uses an angular material tab group.


   

        
相关标签:
2条回答
  • 2021-01-03 11:37

    If there is no solution today then i can offer you some trick based on monkey patching:

    template.html

    <mat-tab-group #tabs>
      ...
    </mat-tab-group> 
    

    component.ts

    import { MatTabGroup, MatTabHeader, MatTab } from '@angular/material';
    ...
    @Component({...})
    export class AppComponent implement OnInit {
      @ViewChild('tabs') tabs: MatTabGroup;
    
      ngOnInit() {
        this.tabs._handleClick = this.interceptTabChange.bind(this);
      }
    
      interceptTabChange(tab: MatTab, tabHeader: MatTabHeader, idx: number) {
        const result = confirm(`Do you really want to leave the tab ${idx}?`);
    
        return result && MatTabGroup.prototype._handleClick.apply(this.tabs, arguments);
      }
    }
    

    Ng-run Example

    0 讨论(0)
  • The accepted answer has stopped working for me in the latest vue version.

    Another option is to set the other tabs to disabled when the active tab is in a state that is incomplete.

    <mat-tab-group>
      <mat-tab label="Form">Form Here</mat-tab>
      <mat-tab label="Result" >Result Here</mat-tab>
    </mat-tab-group>
    
    0 讨论(0)
提交回复
热议问题