Angular2 - 你如何console.log当前的路线?

时间:2016-05-22 18:13:14

标签: javascript angular

我正在努力让它发挥作用。

<li [class.active]="router.isRouteActive(router.generate(['/SomeRoute']))">

要对bug进行测试,我想在console.log中查看当前路由以查看实际路由是什么,但我不确定如何在Angular2中执行此操作?

编辑:

{
  "name": "angular2-boilerplate",
  "version": "1.0.0",
  "scripts": {
    "lite": "lite-server",
    "gulp": "gulp",
    "start": "concurrent \"npm run gulp\" \"npm run lite\" "
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.14",
    "es6-shim": "^0.35.0",
    "ng2-pagination": "0.0.1",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "systemjs": "0.19.25",
    "zone.js": "0.6.6"
  },
  "devDependencies": {
    "autoprefixer": "^6.3.5",
    "cssnano": "^3.4.0",
    "concurrently": "^2.0.0",
    "gulp": "^3.9.0",
    "gulp-ext-replace": "^0.2.0",
    "gulp-imagemin": "^2.4.0",
    "gulp-postcss": "^6.0.1",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-typescript": "^2.10.0",
    "gulp-uglify": "^1.5.1",
    "lite-server": "^2.1.0",
    "postcss": "^5.0.13",
    "postcss-scss": "^0.1.3",
    "precss": "^1.3.0"
  }
}

2 个答案:

答案 0 :(得分:0)

您要找的是location service

  

的console.log(location.path())

答案 1 :(得分:0)

您可以使用RouteRegistry查找有关其他组件代码中其他路由的信息:

recognize(url: string, ancestorInstructions: Instruction[]) : Promise<Instruction>
  

给定URL和父组件,返回最具体的组件   将应用程序导航到指定状态的指令   网址

官方文档:https://angular.io/docs/ts/latest/api/router/RouteRegistry-class.html

关于Angular2路由器详细信息的Googd文章:http://julienrenaux.fr/2015/12/25/angular2-series-routing/