首页 > 解决方案 > 获取路由上的查询参数,Angular 服务器端渲染

问题描述

是否可以获取查询参数服务器端?我有我的 Angular 应用程序的 Lambda Cloudfront 部署版本。

当平台浏览器发生时,一切正常。

当我尝试获取服务器端的查询参数时,我总是得到空/未定义。

我试过这个:

ngOnInit(): void {
  this.queryId = this.route.snapshot.queryParamMap.get('id');
  this.metaPlatform.addTag({ property: this.queryId, content: this.queryId });
}
//I get this meta tag:
<meta property="null" content="null">

并且还尝试了这个:

ngOnInit(): void {
    const snapshot = this.router.routerState.snapshot;
    this.meta.addTag({property: snapshot.url.split('=')[1],content: snapshot.url.split('=')[1]);
}
//I get this meta tag:
<meta property="undefined" content="undefined">

我想根据 queryParams 设置元标记,但我找不到将它们放在服务器端的方法。

标签: angularserver-side-renderingangular-universalquery-parameters

解决方案


看起来像 CloudFront 配置问题。

在 CloudFront 创建的行为中,检查“查询字符串转发和缓存”是否配置为转发特定或所有参数。


推荐阅读