Angular路由在路由中添加%2F而不是斜杠
创始人
2024-10-28 05:31:01
0

问题描述:在Angular路由中使用斜杠(/)时,会自动转义为%2F。

解决方法: 在Angular路由的配置文件中,可以使用encodeURIComponent()方法将斜杠(/)转义为%2F。

示例代码:

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'about/:param', component: AboutComponent },
];

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

about.component.ts

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

@Component({
  selector: 'app-about',
  templateUrl: './about.component.html',
  styleUrls: ['./about.component.css']
})
export class AboutComponent implements OnInit {
  param: string;

  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.route.params.subscribe(params => {
      this.param = params['param'];
    });
  }

}

about.component.html

Param: {{ param }}

在上述示例代码中,路由配置文件app-routing.module.ts中的about路由使用了参数:param。当在浏览器地址栏中输入/about/test时,Angular会将/test作为参数传递给about组件,并显示在about.component.html中。

如果参数中包含斜杠(/),例如/about/test/path,Angular会将斜杠转义为%2F,即/about/test%2Fpath。在about组件中,可以使用decodeURIComponent()方法将%2F转义回斜杠。

about.component.ts

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

@Component({
  selector: 'app-about',
  templateUrl: './about.component.html',
  styleUrls: ['./about.component.css']
})
export class AboutComponent implements OnInit {
  param: string;

  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.route.params.subscribe(params => {
      this.param = decodeURIComponent(params['param']);
    });
  }

}

这样,当在浏览器地址栏中输入/about/test%2Fpath时,Angular会将/test%2Fpath作为参数传递给about组件,并显示在about.component.html中的param变量中,此时param的值为test/path。

相关内容

热门资讯

德州ai辅助有用!wepoke... 德州ai辅助有用!wepoke显示有(透视)一向有挂(详细辅助软件教程)是一款可以让一直输的玩家,快...
透视玄学!wepokeai代打... 透视玄学!wepokeai代打的胜率,wepoke人有挂吗,详细透视爆料教程1、完成wepokeai...
智星德州菠萝开挂!德州ai智能... 智星德州菠萝开挂!德州ai智能辅助(透视)真是存在有挂(详细辅助普及教程)1、超多福利:超高返利,海...
透视脚本!wepoke确实有挂... 透视脚本!wepoke确实有挂,wepoke中牌率,详细透视透牌教程1、进入游戏-大厅左侧-新手福利...
wepower辅助器!aapo... wepower辅助器!aapoker俱乐部后台可以操作吗(透视)固有真的有挂(详细辅助解说技巧)1、...
wepoke辅助有挂!微扑克w... wepoke辅助有挂!微扑克wpk真的有辅助吗(透视)本然存在有挂(详细辅助必胜教程)是一款可以让一...
透视中牌率!wepoke真的有... 透视中牌率!wepoke真的有挂,wepoke防外挂,详细透视AI教程1、下载好wepoke真的有挂...
德扑数据软件!wpeoke软件... 德扑数据软件!wpeoke软件下载(透视)好像真的是有挂(详细辅助AI教程);人气非常高,ai更新快...
透视辅助!we辅助poker德... 透视辅助!we辅助poker德之星,wopoker真的有挂吗,详细透视可靠教程;1、操作简单,无需注...
微扑克辅助挂!wpk微扑克外挂... 1、微扑克辅助挂!wpk微扑克外挂事件(透视)从来是真的有挂(详细辅助详细教程);代表性(透视辅助软...