我想从网址
http://localhost:4200/courses/5a0eb3d7f6b50c2589c34e57获取ID,所以在我的app.module.ts中我有这样的路由
{path:'courses/:id', component:CourseComponent, canActivate:[AuthGuard]}并且在我的course.component.ts中我有以下代码:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-course',
templateUrl: './course.component.html',
styleUrls: ['./course.component.css']
})
export class CourseComponent implements OnInit {
id: String;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.id = this.route.snapshot.queryParams["id"];
console.log(this.id)
}
}
答案 0 :(得分:2)
你应该使用rxjs。
/path/to/data
优先,因为this.route.params.pluck('id').subscribe(v => console.log(v));
很快就会被贬低:
.params
一切都在文档中...... https://angular.io/guide/router#parammap-api
答案 1 :(得分:0)
这是一个路线参数,你应该使用params属性
this.id = this.route.snapshot.params["id"];