Angular 4:url用户params捕获问题

时间:2017-12-13 18:26:26

标签: angular url parameters

我正在尝试在用户收到带有令牌的电子邮件后立即在“输入新密码”屏幕中重置用户密码(创建一个新密码)。在我的组件中,我使用ActivateRoute来获取url中的参数(令牌,电子邮件,密码),我需要这些参数将它们发送到API,但到目前为止,我在组件上得到“未定义”。这是ActivateRoute:

export class NewPasswordComponent implements OnInit {

  public loginValues = new User([]);

  // Constructor & init
  constructor(
    private auth: AuthService,
    private router: Router,
    private userService: UserService,
    private snackBar: MatSnackBar,
    private activatedRoute: ActivatedRoute,

  ) {
    this.activatedRoute.params.subscribe(params => {
      console.log(params['email']);
      console.log(params.email); -> undefined
      this.loginValues.email = params['email'];
      this.loginValues.token = params['token'];
    });
  }

与服务通信的组件方法(在HTML中提交新密码时调用)

clientNewPassword() {
    this.userService.newPassword(this.loginValues, passwordSent => {
      this.router.navigateByUrl('/login');
    });
  }

服务:

public newPassword(user: User, callback: (passwordSent: User) => void) {

    return this.restangular
      .all('auth/reset')
      .customPOST(user.toJSONNewPassword())
      .map((res) => res.data)
      .subscribe({
        next: (res: User) => {
          this.user = new User(res);

          callback(this.user);
        },
      });
  }

路线:

//NEW PASSWORD
  {
    path: 'new-password/:email:token',
    component: NewPasswordComponent,
    canActivate: [LoginGuard],
  },

有人能帮助我实现这个目标吗?感谢

编辑:

我试图呼叫的网址:

http://localhost:4200/new-password/email=snapexpress@gmail.com;token=13af5d409878f1dd5ec88049ba82a5549a07deb4

1 个答案:

答案 0 :(得分:0)

根据DaniS的提示,我设法找出解决方案。路线必须是:

//NEW PASSWORD
  {
    path: 'new-password/:email/:token',
    component: NewPasswordComponent,
    canActivate: [LoginGuard],
  },

带有两个参数的网址:

http://localhost:4200/new-password/snapexpress@gmail.com/13af5d409878f1dd5ec88049ba82a5549a07deb4