如何使用查询参数路由使angular4通用 [英] How to render angular4 universal with query parameter route

查看:97
本文介绍了如何使用查询参数路由使angular4通用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于SEO的目的,我正在尝试将angular 1.4迁移到angular4通用.一切正常.我只有一条路线.

I'm trying to migrate angular 1.4 to angular4 universal for SEO purposes. Everything works well. I have one route.

注册后确认电子邮件 http://localhost/confirmEmail/09393?code = 'xxxxxxxxx'

confirm email after registration http://localhost/confirmEmail/09393?code='xxxxxxxxx'

ngOnInit() {

    let param_id = this.route.snapshot.params['id'];
    let param_code = this.route.snapshot.queryParams['code'];
    this.message = '';

    this.authService.getConfirmation(param_id, param_code)
        .subscribe(
            data => {
              this.router.navigate(['/login']);
            },
            error => {

            }
        );
  }

  1. 看起来像它在服务器上呈现的一样,并调用rest API getConfirmation.
  2. 一旦在客户端上呈现UI,它将再次调用getconfirmation.

有没有办法.我不想调用其余的API服务器端,只需从客户端代码中调用它即可.

Is there a way. I don't wanna call the rest API server side, just call it from the client code.

推荐答案

这对我有用.

检查是否存在全局窗口对象.然后调用rest api进行确认

check to see if global window object is present. then call rest api to get confirmed

ngOnInit(){

ngOnInit() {

  if (typeof window !== 'undefined') {
      let param_id = this.route.snapshot.params['id'];
      let param_code = this.route.snapshot.queryParams['code'];
      this.message = '';
      this.getConfirmation(param_id, param_code);
  }

}

这篇关于如何使用查询参数路由使angular4通用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆