How to render angular4 universal with query parameter route

倖福魔咒の 提交于 2019-12-22 11:33:30

问题


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

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. Looks like it gets rendered on the server and calls rest API getConfirmation.
  2. once UI is rendered on the client it calls getconfirmation again.

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


回答1:


this worked for me.

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

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);
  }

}



来源:https://stackoverflow.com/questions/45788326/how-to-render-angular4-universal-with-query-parameter-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!