您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

详解Angular中的Route路由

2024/3/26 19:14:31发布16次查看
本篇文章带大家一起了解angular中的路由(route)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
angular 路由(route)我们可以将路由器理解成控制整个应用状态的视图对象, 每个应用都有一个路由器; 路由器的另一个作用是为每个视图分配唯一的 url, 可以利用这个 url 使应用之间跳转到某一个特定的视图状态。 单页应用其实就是一个视图状态的集合。
相关教程推荐:《angular教程》
单页应用(spa)一个单页应用是主页面只加载一次, 不再重复刷新, 只是改变页面部分内容的应用。 angular 应用就是单页应用, 在 angular 中使用路由器来实现根据用户的操作来改变页面的内容而不重新加载页面。 单页应用可以理解为一个视图状态的集合。
路由对象
routes 路由数组路由器需要先配置才会有路由信息, 并用 routermodule.forroot 方法来配置路由器。 当浏览器的 url 变化时, 路由器会查找对应的 route(路由), 并据此决定该显示哪个组件。
基础配置:
const approutes: routes = [ { path: 'common/a', component: acomponent }, { path: 'common/b/:id', component: bcomponent }, { path: '**', component: notfoundcomponent}, // 定义通配符路由];@ngmodule({ imports: [routermodule.forroot(routes)], exports: [routermodule], ...})
routeroutlet 路由出口routeroutlet 是一个来自路由模块中的指令,它的用法类似于组件。 它扮演一个占位符的角色,用于在模板中标出一个位置,路由器将会把要显示在这个出口处的组件显示在这里。
<h1>组件的内容显示在(router-outlet)下方</h1> <router-outlet></router-outlet>
router 路由器使用 router 对象导航。
constructor(private router: router) {}toacomponent() { this.router.navigate(['/common/a']); // 或 this.router.navigateurl('common/a');}
routerlink 路由器链接路由链接 url 必须以 ‘/’ 开头。
<a [routerlink]="['/']">主页</a><a [routerlink]="['/common/b', id]">b组件</a><router-outlet></router-outlet>
activatedroute 激活的路由当前激活的路由的路径和参数可以通过 activateroute 的路由服务来获取。
常用属性:属性说明
url 路由路径的 observable 对象,是一个由路由路径中的各个部分组成的字符串数组.
data 一个 observable,其中包含提供给路由的 data 对象。也包含由解析守卫(resolve guard)解析而来的值。
parammap 一个 observable,其中包含一个由当前路由的必要参数和可选参数组成的 map 对象。用这个 map 可以获取来自同名参数的单一值或多重值。
queryparammap 一个 observable,其中包含一个对所有路由都有效的查询参数组成的 map 对象。 用这个 map 可以获取来自查询参数的单一值或多重值。
在路由时传递数据在查询参数中传递数据/common/b?id=1&name=2 => activatedroute.queryparammap
在路由路径中传递数据{path: /common/b/:id} => /commo/b/1 => activatedroute.parammap
在路由配置中传递数据{path: /common/b, component: bcomponent, data: {id:“1”, title: “b”}}
示例constructor( private activatedroute: activatedroute) { }ngoninit() { // 从参数中获取 this.activatedroute.queryparammap.subscribe(params => { this.id = params.get('id'); }); // 或 // this.activated.snapshot.queryparammap.get('id'); // 从路径中获取 this.activatedroute.parammap.subscribe(params => { this.id = params.get('id'); }); this.activatedroute.data.subscribe(({id,title}) => { });}
snapshot: 参数快照,是一个路由信息的静态快照,抓取自组件刚刚创建完毕之后,不会监听路由信息的变化。如果确定一个组件不会从自身路由到自身的话,可以使用参数快照。
subscribe: 参数订阅,相当于一个监听器,会监听路由信息的变化。
重定向路由在用户访问一个特定的地址时,将其重定向到另一个指定的地址。
配置重定向路由:
// 当访问根路径时会重定向到 home 路径const approutes: routes = [ { path: '', redirectto: 'home', pathmatch: 'full'}, { path: 'home', component: homecomponent}];
子路由子路由配置语法:
const approutes: routes = [ { path: 'home', component: homecomponent, children: [ { path: '', component: acomponent}, { path: 'b', component: bcomponent} ] },];
辅助路由辅助路由又兄弟路由,配置语法:
// 路由配置{path: 'xxx', component: xxxcomponent, outlet:'xxxlet'},{path: 'yyy', component: xxxcomponent, outlet:'yyylet'}// 使用<router-outlet></router-outlet><router-outlet name="xxxlet"></router-outlet>// 链接<a [routerlink]="['/home',{outlets:{xxxlet: 'xxx'}}]">xxx</a>
当点击xxx链接时,主插座会显示’/home’链接所对应的组件,而xxxlet插座会显示xxx对应的组件。
路由守卫(guard)canactivate/canactivechild:处理导航到某路由的情况当用户不满足这个守卫的要求时就不能到达指定路由。
export class demoguard1 implements canactivate { canactivate(route: activatedroutesnapshot, state: routerstatesnapshot): boolean { ... return true; }}
candeactivate:处理从当前路由离开的情况如果不满足这个守卫的要求就不能离开该路由。
// 泛型中 acomponent 代表要守卫的组件。export class demoguard2 implements candeactivate<acomponent> { candeactivate(component: acomponent): boolean { // 根据 component 的信息进行具体操作 retturn true; }}
resolve:在路由激活之前获取路由数据在进入路由时就可以立刻把数据呈现给用户。
@injectable()export aresolve implements resolve<any> { resolve(route: activatedroutesnapshot, state: routerstatesnapshot) { const id = route.parammap.get('id'); // 可以根据路由中的信息进行相关操作 }}
最后,需要将路由守卫添加到路由配置中:
const approutes: routes = [ { path: 'common/a', component: acomponent, canactivate: [demogurad1], candeactivate: [demoguard2], resolve: {data: aresolve} }, { path: 'common/b/:id', component: bcomponent }, { path: '**', component: notfoundcomponent}, // 定义通配符路由];@ngmodule({ imports: [routermodule.forroot(routes)], exports: [routermodule], ...})
更多编程相关知识,请访问:编程入门!!
以上就是详解angular中的route路由的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product