Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

angular - Mat-tab not underlying when reload

I am using mat-tab from Material Design, Angular. When I click on a tab then it becomes active and becomes underlined

enter image description here

However, when I reload the page on the browser, the active attribute is still true but the underlying is not appearing

enter image description here

Would anyone knows how to solve this issue ?

HTML

    <nav mat-tab-nav-bar>

      <!-- About tab -->
      <a mat-tab-link 
         routerLink="{{links[0][1]}}" 
         (click)="activeLink = links[0][0]"
         [active]="activeLink == links[0][0]">
        {{links[0][0]}}
      </a>

      <!-- Revenue tab -->
      <a mat-tab-link 
         routerLink="{{links[1][1]}}" 
         (click)="activeLink = links[1][0]"
         [active]="activeLink == links[1][0]">
        {{links[1][0]}}
      </a>

    </nav>

TS

export class ProfileHeaderComponent extends SubscriptionContainerComponent implements OnInit {

  user: UserFirestore;
  links = [
    ['A propos', 'about'],
    ['Revenu', 'revenue'],
  ];
  activeLink = this.links[0][0];

  constructor(
    private route: ActivatedRoute,
    private router: Router,
  ) {
    super();
  }

  ngOnInit(): void {
    // Set user and active link
    this.setUserAndActiveLink();
  }

  setActiveLink(): void {
    const isActiveProfile = this.router.isActive('/home/profile', true);
    const isActiveAbout = this.router.isActive('/home/profile/about', true);
    const isActiveRevenue = this.router.isActive('/home/profile/revenue', true);
    if (isActiveAbout || isActiveProfile) {
      this.activeLink = this.links[0][0];
    } else if (isActiveRevenue) {
      this.activeLink = this.links[1][0];
    } else {
      this.activeLink = undefined;
    }
  }

  setUserAndActiveLink(): void {
    this.subscription.add(
      this.route.data.subscribe((data: { user: UserFirestore }) => {
        // Set user
        this.user = data.user;
        // Set active link
        this.setActiveLink();
      })
    );
  }


question from:https://stackoverflow.com/questions/65947271/mat-tab-not-underlying-when-reload

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Issue solved with

// Enable tab underlying on reload
document.body.style.display = "initial";

in component constructor


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...