Angular 15中使用@在URL中进行路由定位

huangapple go评论84阅读模式
英文:

Angular 15 Routing with @ in URL

问题

"Quick Question: Is it possible to route something like domain.com/@Username? The @ is not part of the username itself.

I already tried { path: "@:username", component: UserComponent } but this doesn't seem to work.

{ path: "@/:username", component: UserComponent } would work but looks pretty 💩"

英文:

Quick Question: Is it possible to route something like domain.com/@Username? The @ is not part of the username itself.

I already tried { path: "@:username", component: UserComponent } but this doesn't seem to work.

{ path: "@/:username", component: UserComponent } would work but looks pretty 💩

答案1

得分: 1

你可以在URL中将“@”符号编码为“%40”。因此,要实现所需的路由模式,例如domain.com/@Username,您可以使用以下路由配置:

{ path: "%40:username", component: UserComponent }

编辑:

确保在您的路由模块中正确定义了路由:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { UserComponent } from './user.component';

const routes: Routes = [
  { path: '%40:username', component: UserComponent },
];

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

确保您在模板文件中使用了正确的路由出口(router outlet)。例如,如果您想要在主要的路由出口中渲染UserComponent,您的模板应该包含一个router-outlet元素。

双重检查您是否以正确的URL格式导航。例如,如果您在模板中使用routerLink导航到用户组件,请确保它被正确编码:

<a [routerLink]="['%40', 'Username']">前往用户</a>
英文:

You can encode the "@" symbol as "%40" in the URL. So, to achieve the desired routing pattern of domain.com/@Username, you can use the following route configuration:

{ path: &quot;%40:username&quot;, component: UserComponent }

Edit:

Ensure that you have defined the route correctly in your routing module.

import { NgModule } from &#39;@angular/core&#39;;
import { RouterModule, Routes } from &#39;@angular/router&#39;;
import { UserComponent } from &#39;./user.component&#39;;

const routes: Routes = [
  { path: &#39;%40:username&#39;, component: UserComponent },
];

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

Make sure you are using the correct router outlet in your template file. For example, if you want to render the UserComponent in the main router outlet, your template should have a router-outlet element.

Double-check that you are navigating to the correct URL format. For instance, if you're using routerLink in your template to navigate to the user component, make sure it is encoded properly:

&lt;a [routerLink]=&quot;[&#39;%40&#39;, &#39;Username&#39;]&quot;&gt;Go to User&lt;/a&gt;

答案2

得分: 0

我刚在 angular文档中找到了关于使用url匹配器进行路由 的答案。

const routes: Routes = [
    {
        matcher: (url) => {
            if (url.length === 1 && url[0].path.match(/^@.+$/gm)) {
                return {
                    consumed: url,
                    posParams: {
                        username: new UrlSegment(url[0].path.slice(1), {}),
                    },
                };
            }

            return null;
        },
        component: UserComponent,
    },
];

<details>
<summary>英文:</summary>

I just found an answer in the angular docs for [routing with url matcher][1].

	const routes: Routes = [
		{
			matcher: (url) =&gt; {
			  if (url.length === 1 &amp;&amp; url[0].path.match(/^@.+$/gm)) {	
				return {
				  consumed: url
				  posParams: {
					username: new UrlSegment(url[0].path.slice(1), {}),
				  },
				};
			  }

			  return null;
			},
			component: UserComponent,
		},
    ];


  [1]: https://angular.io/guide/routing-with-urlmatcher#configure-your-routes-for-your-application

</details>



huangapple
  • 本文由 发表于 2023年7月3日 15:54:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定