在Angular中,可以使用Router来进行路由导航。以下是一些基础的代码示例:
import { Router } from '@angular/router';
constructor(private router: Router) {}
this.router.navigate(['/path']);
其中,/path
是要导航到的路径。可以使用相对路径或绝对路径。
this.router.navigate(['/path'], { queryParams: { id: 123 } });
在导航时,可以传递一些参数。在上面的示例中,id
参数的值为123
。
import { ActivatedRoute } from '@angular/router';
constructor(private route: ActivatedRoute) {}
ngOnInit() {
const id = this.route.snapshot.queryParams['id'];
}
通过注入ActivatedRoute服务,可以获取当前路由的参数。在上面的示例中,获取了id
参数的值。
下一篇:Angular中的路由导航