Angular问题 - 无法获取 /
创始人
2024-10-30 00:01:00
0

问题描述:

我在使用Angular时遇到了一个问题,我无法获取根路径“/”。我已经尝试了多种方法,但没有成功。以下是我的代码示例:

app.component.ts:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  constructor(private router: Router, private activatedRoute: ActivatedRoute) {}

  ngOnInit() {
    this.router.events.subscribe(event => {
      console.log(event);
      console.log(this.activatedRoute.snapshot.url[0].path);
    });
  }
}

app.component.html:


问题解决方法:

要获取根路径“/”,您可以使用this.router.url而不是this.activatedRoute.snapshot.url[0].path。以下是修改后的代码示例:

app.component.ts:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  constructor(private router: Router) {}

  ngOnInit() {
    this.router.events.subscribe(event => {
      console.log(event);
      console.log(this.router.url);
    });
  }
}

现在,this.router.url将返回当前路由的路径,包括根路径“/”。

希望这可以帮助您解决问题!

相关内容

热门资讯

透视了解!德州最新(WEPOk... WePoke高级策略深度解析‌;透视了解!德州最新(WEPOke)外挂透明挂辅助APP(辅助挂)教你...
透视好牌(aapOKER)外挂... 透视好牌(aapOKER)外挂透明挂辅助APP(透视)透视辅助(2025已更新)(哔哩哔哩)是一款可...
分享一款(微扑克游戏)外挂透明... 分享一款(微扑克游戏)外挂透明挂辅助器(辅助挂)发牌规律(了解有挂)-哔哩哔哩是一款可以让一直输的玩...
程序员教你!pokerrrr开... WePoker透视辅助版本解析‌,程序员教你!pokerrrr开挂(wepoke)外挂透明挂辅助机制...
透视透明(德州之星ai)外挂透... 1、透视透明(德州之星ai)外挂透明挂辅助工具(辅助挂)透视辅助(2021已更新)(哔哩哔哩);该软...
热点推荐(Wepoke代打)外... 热点推荐(Wepoke代打)外挂透明挂辅助软件(透视)透视辅助(有挂分享)-哔哩哔哩;值得一提的是,...
必知教程!微扑克总结(Wepo... 必知教程!微扑克总结(Wepoke)外挂透明挂辅助app(辅助挂)可靠技巧(真实有挂)-哔哩哔哩;微...
透视ai代打(Wepoke自建... 透视ai代打(Wepoke自建房)外挂透明挂辅助机制(辅助挂)软件透明挂(2020已更新)(哔哩哔哩...
技术分享(Wepoke系统)外... 技术分享(Wepoke系统)外挂透明挂辅助软件(辅助挂)发牌规律(有挂方式)-哔哩哔哩1、不需要AI...
每日必备!Wepoke实锤(W... 每日必备!Wepoke实锤(WepOke)外挂透明挂辅助机制(辅助挂)wepoke教程(揭秘有挂)-...