本篇文章带大家了解一下angular中的路由(routing),介绍一下路由的基本用法,看看怎么接收参数,希望对大家有所帮助!
环境:
angular cli: 11.0.6
angular: 11.0.7
node: 12.18.3
npm : 6.14.6
ide: visual studio code
1. 摘要简单来说地址栏中,不同的地址(url)对应不同的页面,这就是路由。同时,点击浏览器的前进和后退按钮,浏览器就会在你的浏览历史中向前或向后导航,这也是基于路由。【相关教程推荐:《angular教程》】
在 angular 里面,router 是一个独立的模块,定义在 @angular/router 模块中,
router 可以配合 ngmodule 进行模块的延迟加载(懒加载)、预加载操作(参考《angular入门到精通系列教程(11)- 模块(ngmodule),延迟加载模块》);router 会管理组件的生命周期,它会负责创建、销毁组件。对于一个新的基于angularcli的项目,初始化时可以通过选项,将approutingmodule默认加入到app.component.ts中。
2. 路由(router)基本用法2.1. 准备
我们首先创建2个页面,用于说明路由的使用:
ng g c page1ng g c page2
使用上面anuglarcli命令,创建page1component, page2component 2个组件。
2.2. 注册路由
//src\app\app-routing.module.tsconst routes: routes = [ { path: 'page1', component: page1component }, { path: 'page2', component: page2component },];@ngmodule({ imports: [routermodule.forroot(routes)], exports: [routermodule],})export class approutingmodule {}
可以看到,简单的路由注册,只需要path和component2个属性,分别定义路由的相对路径,以及这个路由的响应组件。
2.3. html中的用法
<a routerlink="page1">page1</a> |<a routerlink="page2">page2</a>
在html模板中,直接使用routerlink属性,标识为angular的路由。执行代码,可以看到 page1和page2 两个超链接,点击可以看到地址栏地址改为http://localhost:4200/page2或http://localhost:4200/page1, 页面内容在page1和page2中切换
2.4. ts 代码中的用法
有时候,需要根据ts中的业务逻辑,进行跳转。ts中,需要注入router实例,如
constructor(private router: router) {}
跳转代码:
// 跳转到 /page1 this.router.navigate(['/page1']); // 跳转到 /page1/123 this.router.navigate(['/page1', 123]);
3. 接收参数3.1. 路径中的参数
一般来说,我们把参数作为url中的一段,如/users/1, 代表查询id是1的用户,路由定义为"/users/id" 这种风格。
针对我们的简单页面,比如我们的page1页面可以传id参数,那么我们需要修改我们的routing为:
const routes: routes = [ { path: 'page1/:id', //接收id参数 component: page1component, }, { // 实现可选参数的小技巧。 这个routing处理没有参数的url path: 'page1', redirectto: 'page1/', // 跳转到'page1/:id' }, { path: 'page2', component: page2component, },];
ts代码读取参数时, 首先需要注入activatedroute,代码如下:
constructor(private activatedroute: activatedroute) {}ngoninit(): void { this.activatedroute.parammap.subscribe((params) => { console.log('parameter id: ', params.get('id')); // 地址 http://localhost:4200/page1/33 // 控制台输出:query parameter name: 33 // 地址 http://localhost:4200/page1/ // 控制台输出:query parameter name: (实际结果为undefined) });}
3.2. 参数(queryparameter)中的参数
参数还有另外一种写法,如http://localhost:4200/?name=cat, 即url地址后,加一个问号’?’, 之后再加参数名和参数值(‘name=cat’)。这种称为查询参数(queryparameter)。
取这查询参数时,和之前的路由参数类似,只是parammap改为queryparammap,代码如下:
this.activatedroute.queryparammap.subscribe((params) => { console.log('query parameter name: ', params.get('name')); // 地址 http://localhost:4200/page1?name=cat // 控制台输出:query parameter name: cat // 地址 http://localhost:4200/page1/ // 控制台输出:query parameter name: (实际结果为undefined)});
4. url路径显示格式不同于传统的纯静态(html)站点,angular中的url不是对应一个真实的文件(页面),因为anuglar接管的路由(routing)处理,来决定显示那个component给终端用户。为了针对不同的场景,angular的url路径显示格式有2中:
http://localhost:4200/page1/123
http://localhost:4200/#/page1/123
默认是第一种,不加#的。如果需要,可以在app-routing.ts中,加入usehash: true, 如:
// app-routing.ts@ngmodule({ imports: [routermodule.forroot(routes, { usehash: true })], exports: [routermodule],})
5. 部署中遇到的问题同样,因为anuglar接管的路由(routing)处理,所以部署时,部署到iis, nginx等等的服务器,都会有不同的技巧(要求),详细参考:
https://github.com/angular-ui/ui-router/wiki/frequently-asked-questions#how-to-configure-your-server-to-work-with-html5mode
6. 总结angular默认不支持可选路由(e.g. /user/:id?),但是我们可以定义2个路由,指向同一个component来实现这个,达到代码复用;(或者使用redirectto)
可以使用usehash参数,实现augular路径前加一个#;
读取参数时,都需要subscribe订阅一下,不能直接读取。
打包后部署问题,查看官方wifi (https://github.com/angular-ui/ui-router/wiki/frequently-asked-questions#how-to-configure-your-server-to-work-with-html5mode)
更多编程相关知识,请访问:编程视频!!
以上就是angular学习之路由(routing)浅析的详细内容。