Angular 2 激活的路由参数在服务中或
import { Component, OnInit } from '@angular/core';import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';@Component({ selector: 'app-example', template: <code> <h1>Example Component</h1> </code>})export class ExampleComponent implements OnInit { constructor(private router: Router, private route: ActivatedRoute) { } ngOnInit() { this.router.events.subscribe(event => { if (event instanceof NavigationEnd) { const params = this.route.snapshot.params; // 在这里可以使用路由参数执行逻辑或传递给服务 console.log(params); } }); }}在上面的代码中,我们订阅了路由事件,并在事件触发时获取当前路由的参数。我们可以将这些参数传递给服务或在Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号