Angular 8路由更改无意中查询字符串顺序

时间:2019-07-02 13:14:47

标签: javascript angular angular-routing angular-router angular8

使用Angular 8作为登录页面构建一个应用程序,该应用程序在重定向链接中接受动态查询字符串,该链接包含一些查询字符串键,例如:42=17,例如,当我在浏览器中点击完整链接时,如下所示与:

http://localhost:4200/success?id1=123&42=17&hash=qwertzuiop

结果:它无意间将顺序更改为:

http://localhost:4200/success?42=17&id1=123&hash=qwertzuiop

预期:要保持查询字符串的顺序,就像我需要检查哈希字符串是否与原始url(如果已更改)一样,当然,当序列更改时,哈希函数结果也会随之变化


代码示例

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { SuccessComponent } from './success/success.component';
import { NotFoundComponent } from './not-found/not-found.component';

const routes: Routes = [
  { path: 'success', component: SuccessComponent },
  { path: '**', component: NotFoundComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

success.component.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
  selector: 'app-success',
  templateUrl: './success.component.html',
  styleUrls: ['./success.component.scss']
})

export class SuccessComponent implements OnInit {

  constructor(
    private route: ActivatedRoute,
    private router: Router) { }

  ngOnInit() {
    console.log(this.route.snapshot.queryParams);
    // {42: "17", id1: "123", hash: "qwertzuiop"}

    console.log(this.router.routerState.snapshot.url);
    // /success?42=17&id1=123&hash=qwertzuiop
  }
}

有什么线索可以在匹配角度路线和导航之前拦截原始URL吗?

0 个答案:

没有答案