Angular - 动态路由在域名后面,例如 example.com/:id?
创始人
2024-10-14 15:30:30
0

在Angular中,可以使用动态路由来将参数添加到URL的末尾。下面是一个示例:

首先,定义一个路由配置,将参数添加到URL的末尾:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { YourComponent } from './your-component.component';

const routes: Routes = [
  { path: ':id', component: YourComponent }
];

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

接下来,在组件中获取参数的值:

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

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

  constructor(private route: ActivatedRoute) { }

  ngOnInit(): void {
    this.id = this.route.snapshot.paramMap.get('id');
  }
}

在上面的示例中,我们使用ActivatedRoute服务来获取URL参数的值。在ngOnInit生命周期钩子中,我们使用route.snapshot.paramMap.get('id')方法获取名为'id'的参数的值,并将其赋给组件中的id属性。

最后,在模板中使用参数的值:

The ID is: {{ id }}

这样,当访问像example.com/123这样的URL时,组件中的'id'属性将被设置为'123',并在模板中显示出来。

请注意,以上示例中的组件和路由配置只是示意图,您需要根据实际需求进行调整。

相关内容

热门资讯

两分钟辅助!wpk安卓下载辅助... 两分钟辅助!wpk安卓下载辅助,wepokerplus辅助,妙计教程(有挂详细)1、wpk安卓下载辅...
七分钟辅助!werplan透视... 七分钟辅助!werplan透视挂,sohoopoker辅助,机巧教程(有挂攻略)werplan透视挂...
一分钟辅助!wpk俱乐部怎么作... 一分钟辅助!wpk俱乐部怎么作弊,拱趴大菠萝十三水作弊,指引教程(真是有挂)小薇(辅助器软件下载)致...
第7分钟辅助!wepoker免... 第7分钟辅助!wepoker免费透视脚本,hhpoker外挂靠谱吗,手筋教程(有挂细节)1、超多福利...
一分钟辅助!wpk辅助,约局吧... 一分钟辅助!wpk辅助,约局吧德州可以透视吗,烘培教程(真的有挂)1、实时约局吧德州可以透视吗透视辅...
第9分钟辅助!pokemomo... 第9分钟辅助!pokemomo辅助软件,wepoker轻量版透视方法,大纲教程(有挂攻略)1)wep...
第9分钟辅助!wepokerp... 第9分钟辅助!wepokerplus辅助,pokemmo脚本辅助下载,妙招教程(真实有挂)该软件可以...
7分钟辅助!wepoker究竟... 7分钟辅助!wepoker究竟有没有透视,菠萝德普辅助器免费版在哪里,窍要教程(真实有挂)1、菠萝德...
六分钟辅助!hhpoker辅助... 六分钟辅助!hhpoker辅助软件下载,wpk辅助,妙招教程(有挂秘籍)运hhpoker辅助软件下载...
五分钟辅助!来玩app破解,a... 五分钟辅助!来玩app破解,aapoker ai插件,举措教程(有挂讲解)1、aapoker ai插...