1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
ts代码读取参数时, 首先需要注入ActivatedRoute,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
3.2. 参数(QueryParameter)中的参数
参数还有另外一种写法,如http://localhost:4200/?name=cat, 即URL地址后,加一个问号’?’, 之后再加参数名和参数值(‘name=cat’)。这种称为查询参数(QueryParameter)。
取这查询参数时,和之前的路由参数类似,只是paramMap改为queryParamMap,代码如下:
1 2 3 4 5 6 7 8 9 |
|
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
, 如:
1 2 3 4 5 |
|
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)的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
浅谈Angular中elem.scope()、elem.isolatescope和$compile(elem)(scope)中scope的区别
了解一下Angular中的@input()和@output()
更多相关阅读请进入《Angular》频道 >>
Vue.js 设计与实现 基于Vue.js 3 深入解析Vue.js 设计细节
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者