Angular 12:角度转换Queryparams转换为编码URL

发布于 2025-01-18 02:51:46 字数 1872 浏览 0 评论 0原文

我最近做了ng update然后遇到了问题。所以基本上我有一条路线 (/book) 和一个查询参数:

localhost:4200/book?success=true&successId=1

localhost:4200/book?success=false&errorId=1

Angular 正在将其转换为单个路由,该路由当然不会在 路由模块:

本地主机:4200/book%3Fsuccess%3Dtrue&successId%3D1

我的package.json:

{
"name": "frontend",
"version": "0.0.0",
"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
},
"private": true,
"dependencies": {
    "@angular/animations": "~12.0.1",
    "@angular/cdk": "~12.2.13",
    "@angular/common": "^12.0.5",
    "@angular/compiler": "~12.0.1",
    "@angular/core": "~12.0.1",
    "@angular/forms": "~12.0.1",
    "@angular/material": "~12.2.13",
    "@angular/platform-browser": "~12.0.1",
    "@angular/platform-browser-dynamic": "~12.0.1",
    "@angular/router": "~12.0.1",
    "@js-temporal/polyfill": "^0.4.0",
    "@material/progress-indicator": "^13.0.0",
    "@ngx-translate/core": "^14.0.0",
    "@ngx-translate/http-loader": "^7.0.0",
    "flag-icons": "^6.0.3",
    "install": "^0.13.0",
    "ngx-cookie-service": "^13.1.2",
    "ngx-translate": "0.0.1-security",
    "npm": "^8.3.1",
    "rxjs": "~6.6.0",
    "tslib": "^2.1.0",
    "zone.js": "~0.11.4"
},
"devDependencies": {
    "@angular-devkit/build-angular": "~12.0.1",
    "@angular/cli": "~12.0.1",
    "@angular/compiler-cli": "~12.0.1",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "jasmine-core": "~3.7.0",
    "karma": "~6.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "typescript": "~4.2.3"
}
}

I recently did ng update and went then in problems. So basically I have a route (/book)
and a query param with:

localhost:4200/book?success=true&successId=1

or

localhost:4200/book?success=false&errorId=1

Angular is converting that to a single route, which is of course not registered in the
routing module:

localhost:4200/book%3Fsuccess%3Dtrue&successId%3D1

My package.json:

{
"name": "frontend",
"version": "0.0.0",
"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
},
"private": true,
"dependencies": {
    "@angular/animations": "~12.0.1",
    "@angular/cdk": "~12.2.13",
    "@angular/common": "^12.0.5",
    "@angular/compiler": "~12.0.1",
    "@angular/core": "~12.0.1",
    "@angular/forms": "~12.0.1",
    "@angular/material": "~12.2.13",
    "@angular/platform-browser": "~12.0.1",
    "@angular/platform-browser-dynamic": "~12.0.1",
    "@angular/router": "~12.0.1",
    "@js-temporal/polyfill": "^0.4.0",
    "@material/progress-indicator": "^13.0.0",
    "@ngx-translate/core": "^14.0.0",
    "@ngx-translate/http-loader": "^7.0.0",
    "flag-icons": "^6.0.3",
    "install": "^0.13.0",
    "ngx-cookie-service": "^13.1.2",
    "ngx-translate": "0.0.1-security",
    "npm": "^8.3.1",
    "rxjs": "~6.6.0",
    "tslib": "^2.1.0",
    "zone.js": "~0.11.4"
},
"devDependencies": {
    "@angular-devkit/build-angular": "~12.0.1",
    "@angular/cli": "~12.0.1",
    "@angular/compiler-cli": "~12.0.1",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "jasmine-core": "~3.7.0",
    "karma": "~6.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "typescript": "~4.2.3"
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2025-01-25 02:51:46

这是因为我的app.routing.module:

之前:

 const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'home', component: HomeComponent },
  { path: 'login', component: CreateAccComponent },
  { path: 'book', component: BookComponent },
  { path: 'location', component: LocationComponent },
  { path: 'openings', component: OpeningsComponent },
  { path: 'pictures', component: PicturesComponent },
  { path: 'roomtour', component: RoomtourComponent },
  { path: 'account', component: AccountComponent },
  { path: 'admin', component: AdminPanelComponent },
  { path: 'bookingProcess', component: BookingProcessComponent },
  { path: '**', component: DefaultComponent}
];

之后:

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'home', component: HomeComponent },
  { path: 'login', component: CreateAccComponent },
  { path: 'book', component: BookComponent },
  { path: 'location', component: LocationComponent },
  { path: 'openings', component: OpeningsComponent },
  { path: 'pictures', component: PicturesComponent },
  { path: 'roomtour', component: RoomtourComponent },
  { path: 'account', component: AccountComponent },
  { path: 'admin', component: AdminPanelComponent },
  { path: 'bookingProcess', component: BookingProcessComponent }
];

由于我删除了路径:'**'它的工作原理!

It was because of my app.routing.module:

before:

 const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'home', component: HomeComponent },
  { path: 'login', component: CreateAccComponent },
  { path: 'book', component: BookComponent },
  { path: 'location', component: LocationComponent },
  { path: 'openings', component: OpeningsComponent },
  { path: 'pictures', component: PicturesComponent },
  { path: 'roomtour', component: RoomtourComponent },
  { path: 'account', component: AccountComponent },
  { path: 'admin', component: AdminPanelComponent },
  { path: 'bookingProcess', component: BookingProcessComponent },
  { path: '**', component: DefaultComponent}
];

after:

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'home', component: HomeComponent },
  { path: 'login', component: CreateAccComponent },
  { path: 'book', component: BookComponent },
  { path: 'location', component: LocationComponent },
  { path: 'openings', component: OpeningsComponent },
  { path: 'pictures', component: PicturesComponent },
  { path: 'roomtour', component: RoomtourComponent },
  { path: 'account', component: AccountComponent },
  { path: 'admin', component: AdminPanelComponent },
  { path: 'bookingProcess', component: BookingProcessComponent }
];

since I removed the path: '**' it works just as expected!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文