angular 中的重定向功能

Redirect function in angular

在 Angular 项目中我有一个函数。当我运行它的时候,我希望能回家。

在我的函数中我有一个条件,如果为真我想重定向。

 if (condition) {
     location.url('/home');
     // code...
 }

有点混淆你所说的 "redirect" 是什么意思。如果你有确定最终位置的条件,那么你可以只使用一个或另一个。

if(location) {
   $location.url('/home');
} else {
   $location.url('/otherDestination');
}

如果您指的是反映在 window 历史中的真实重定向,请阅读 AngularJS 提供的 $location.replace 功能。向下滚动到标有 'Replace method' 的部分。希望这对您有所帮助!

$location.url('/otherDestination').replace();

你总是可以使用 JS

window.location.href = 'xyz';
constructor(private router: Router) { }

 if (condition) {
      this.router.navigate(['/home']);
 }