Angular11路由-如何在预期的导航标签上着陆?
创始人
2024-10-21 16:30:44
0
  1. 创建一个包含导航标签的组件,并将其添加到导航文件中。例如:

  1. 在每个子组件中,使用以下代码来设置当前活动路由链接的CSS类:

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

@Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.css'] }) export class HomeComponent {

constructor(private router: Router) { }

ngOnInit() { this.router.events.subscribe((event) => { if (event instanceof NavigationEnd) { let currentUrl = this.router.url; if (currentUrl === '/home') { document.getElementById('homeTab').classList.add('active'); } else { document.getElementById('homeTab').classList.remove('active'); } } }); }

}

  1. 在导航标签组件中,使用以下代码来设置活动CSS类:

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

@Component({ selector: 'app-nav', templateUrl: './nav.component.html', styleUrls: ['./nav.component.css'] }) export class NavComponent {

constructor(private router: Router) { }

ngOnInit() { this.router.events.subscribe((event) => { if (event instanceof NavigationEnd) { let currentUrl = this.router.url; if (currentUrl === '/home') { document.getElementById('homeTab').classList.add('active'); } else { document.getElementById('homeTab').classList.remove('active'); } if (currentUrl === '/about') { document.getElementById('aboutTab').classList.add('active'); } else { document.getElementById('aboutTab').classList.remove('active'); } if (currentUrl === '/contact') { document.getElementById('contactTab').classList.add('active'); } else { document.getElementById

相关内容

热门资讯

第六分钟了解!牌乐门安全黑科技... 第六分钟了解!牌乐门安全黑科技是真的吗!原来是有辅助app(有挂攻略)-哔哩哔哩;1、每一步都需要思...
第9分钟了解!土豪联盟辅助!本... 第9分钟了解!土豪联盟辅助!本来有辅助软件(真实有挂)-哔哩哔哩小薇(辅助器软件下载)致您一封信;亲...
七分钟了解!智星德州插件!总是... 七分钟了解!智星德州插件!总是一直都是有辅助方法(真实有挂)-哔哩哔哩1、智星德州插件免费脚本咨询教...
6分钟了解!广丰510k辅助插... 6分钟了解!广丰510k辅助插件!真是有辅助脚本(有挂技巧)-哔哩哔哩1、玩家可以在广丰510k辅助...
九分钟了解!威信茶馆app插件... 九分钟了解!威信茶馆app插件!总是有辅助攻略(有挂技术)-哔哩哔哩运威信茶馆app插件辅助工具,进...
2分钟了解!战神辅助众包!好像... 2分钟了解!战神辅助众包!好像存在有辅助脚本(有挂教学)-哔哩哔哩1、上手简单,内置详细流程视频教学...
第三分钟了解!来趣广西辅助!真... 第三分钟了解!来趣广西辅助!真是真的是有辅助方法(详细教程)-哔哩哔哩1、下载好来趣广西辅助透视辅助...
七分钟了解!拱趴大菠萝辅助工具... 七分钟了解!拱趴大菠萝辅助工具下载!一直存在有辅助教程(有挂细节)-哔哩哔哩该软件可以轻松地帮助玩家...
第3分钟了解!九九山城万州版脚... 第3分钟了解!九九山城万州版脚本!切实真的是有辅助插件(详细教程)-哔哩哔哩1、操作简单,无需九九山...
十分钟了解!wepoker辅助... 十分钟了解!wepoker辅助软件价格!真是存在有辅助app(有挂透视)-哔哩哔哩1、下载好wepo...