Angular 4+:手动重定向到路由 [英] Angular 4+ : Manual redirect to route
问题描述
我最近才开始使用angular 4而不是angular.js 1.
I just recently started using angular 4 instead of angular.js 1.
我已经按照英雄指南学习了有关angular 4的基础知识,并且我目前正在使用来自@@ ular/router包中的angular自己的"RouterModule".
I have followed the heroes tutorial to learn about the fundamentals of angular 4 and I am currently using angular's own "RouterModule" from the "@angular/router" package.
为了对我的应用程序实施一些授权,我想知道如何手动重定向到另一条路由,我似乎无法在互联网上找到任何与此有关的有用信息.
In order to implement some authorization for my application I would like to know how to manually redirect to another route, I can not seem to find any useful information about this on the internet.
推荐答案
角度路由:手动导航
首先,您需要导入角度路由器:
Angular routing : Manual navigation
First you need to import the angular router :
import {Router} from "@angular/router"
然后将其注入到您的组件构造函数中:
Then inject it in your component constructor :
constructor(private router: Router) { }
最后,在需要重定向"(redirect)的任何地方调用.navigate
方法. :
And finally call the .navigate
method anywhere you need to "redirect" :
this.router.navigate(['/your-path'])
您还可以在路线上添加一些参数,例如user/5
:
You can also put some parameters on your route, like user/5
:
this.router.navigate(['/user', 5])
文档: Angular官方文档
这篇关于Angular 4+:手动重定向到路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!