创建严格用于外部重定向的路由的正确方法是什么,还要为其添加一些url参数?
在我的示例中,我想创建一个“/ Register”路由,首先以编程方式附加一些参数,然后重定向到注册/登录网站。
这是流程:
1)用户点击了电子邮件中的链接并点击角网站:“http://my.site.com/Register?token=1234”
2)Angular网站做了一些工作并重定向到“http://login.site.com/Register?token=1234&extrainfo=4567”
我知道如何解决这个问题,但这似乎是一个黑客攻击。那是因为我使用的是我实际上并不需要的组件。宁愿使用某种“服务”......
我正在创建一个实现CanActivate的组件。
{ path: 'Register', component: RegisterRedirectComponent, canActivate: [RegisterRedirectComponent] },
// actually a component, because we need to provide a component to routes
我实现了canactivate,它总是会返回false,因为它会事先进行重定向。
import * as ng from '@angular/core';
import { CanActivate } from '@angular/router';
/*
This is a component trick so that we can implement a redirector using canactivate.
The routing path leads to this component, which is never reached, because canactivate is always false and forces a redirect.
Replace with better solution in future... this is a useless "Component".
*/
@ng.Component({
template: ''
})
export class RegisterRedirectComponent implements CanActivate {
constructor() {}
canActivate() {
// Get current url
// Do some work to get extra parameters
// Append to url
window.location.href = // url
return false;
}
}