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。

相关内容

热门资讯

玩家必看!wejoker私人辅... 玩家必看!wejoker私人辅助软件,开心网辅助工具,AI教程(必看开挂辅助插件);打开点击测试直接...
透视存在“小程序蜀山四川免费辅... 透视存在“小程序蜀山四川免费辅助器”原生有辅助开挂平台(有挂猫腻);亲,小程序蜀山四川免费辅助器这款...
重大科普!hhpoker辅助,... 暗宝辅助好用吗 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、...
一分钟揭秘“微信小程序多乐辅助... 大家好,今天小编来为大家解答微信小程序多乐辅助器这个问题咨询软件客服可以免费测试直接加微信(1367...
热点推荐!wepoker免费透... 热点推荐!wepoker免费透视脚本,方片十三张辅助,科技教程(原来有开挂辅助软件)>>您好:软件加...
盘点一款“桂麻圈辅助”往昔有辅... >>您好:桂麻圈辅助确实是有挂的,很多玩家在这款桂麻圈辅助游戏中打牌都会发现很多用户的牌特别好,总是...
玩家必看分享!wepoker私... 玩家必看分享!wepoker私人局透视方法,河洛杠次脚本开发,线上教程(了解辅助平台);无需打开直接...
教程攻略!德州透视插件,阿当比... 教程攻略!德州透视插件,阿当比鸡有挂吗,wepoke教程(传授辅助软件)您好:阿当比鸡有挂吗这款游戏...
我来分享“人海大厅辅助插件”原... 您好:这款人海大厅辅助插件游戏是可以开挂的,确实是有挂的,很多玩家在这款人海大厅辅助插件游戏中打牌都...
玩家必备教程“欢乐达人模拟器”... 玩家必备教程“欢乐达人模拟器”起初有辅助下载(确实有挂);无需打开直接搜索打开薇:136704302...