【问题标题】:Error getting the correct route in console Angular2在控制台 Angular2 中获取正确路由时出错
【发布时间】:2017-02-26 16:46:52
【问题描述】:

我正在尝试将当前路由打印到控制台日志我尝试使用路由器的 url 属性打印路由。即使原始路线是/profile,它也仅将/ 显示为路线。我尝试将整个路由器对象打印到控制台,在那里我可以看到如下所示的 url 字段:url:(...)。当我单击(...) 时,原始路径显示为url:"\profile"(...) 在悬停时显示“调用属性获取器”。以下是将 url 打印到控制台的代码。我在 angular 2.4.0 上。

  import { Component, AfterViewInit } from '@angular/core';
  import {SignupService} from '../../services/signup.service';
  import {Auth} from '../../services/auth.service';
  import {AppRoutingModule} from '../../routes/app.routing';
  import {Router} from '@angular/router';
  declare var $: any;


  @Component({
    moduleId:module.id,
    selector: 'navbar',
    templateUrl: 'navbar.component.html',
    styleUrls: ['../../assets/css/navbar.css'],
  })
  export class NavbarComponent  {
    constructor(private auth: Auth, private router:Router ){
      console.log(this.router.url);   
    };
    ngAfterViewInit() {
        $(".button-collapse").sideNav();  
      }

  }

【问题讨论】:

  • 请帮忙。谢谢

标签: angular url angular2-routing console.log


【解决方案1】:

您是否尝试过这种解决方案。

import {Location} from '@angular/common';

constructor(locationStrategy: LocationStrategy) {
  console.log(locationStrategy);
}

检查这些示例here(检查浏览器控制台以查看结果)

【讨论】:

  • 据我所知它显示了相对于您的组件的路径
猜你喜欢
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多