【发布时间】:2020-01-25 07:05:59
【问题描述】:
想象以下路线:
// student-info/120
{
path: "student-info/:id", component: StudentInfoComponent
}
如果我想获取组件中的id,那就对了
constructor(private route: ActivatedRoute) { }
ngOnInit() {
alert(this.route.snapshot.params['id']);
}
如果我想在服务中获取 id,它会返回 undefined ...
@Injectable({
providedIn: 'root'
})
export class StudentService {
constructor(private route: ActivatedRoute) { }
getParams() {
return this.route.snapshot.params['id'];
}
}
Demo:(点击按钮查看)
【问题讨论】:
-
请在组件元数据中使用StudentService:提供者:[StudentService]