angular快捷键小记以及路由策略

建立路由(module):

ng g m xxx --routing


建立页面(component):

ng g c xxx;


配置路由守卫(guard):

ng g g  auth/auth


配置服务(server):

ng g s auth/auth


懒加载路由;

RouterModule.forRoot() 和 RouterModule.forChild()

RouterModule对象为提供了两个静态的方法:forRoot()和forChild()来配置路由信息。

RouterModule.forRoot()方法用于在主模块中定义主要的路由信息,RouterModule.forChild()与 Router.forRoot()方法类似,但它只能应用在特性模块中。

即根模块中使用forRoot(),子模块中使用forChild()。

应用:

只在根模块 AppRoutingModule 中调用 RouterModule.forRoot(如果在 AppModule 中注册应用的顶级路由,那就在 AppModule 中调用)。 在其它模块中,你就必须调用RouterModule.forChild方法来注册附属路由。

注意:路由配置的顺序很重要。 路由器会接受第一个匹配上导航所要求的路径的那个路由。


路由守卫

Angular路由守卫: 在进入或离开某路由时,用于判断是否可以离开、进入某路由;;; return true 代表可以进入当前路由;return false 代表不可以进入当前路由,但可以进入自定义的路由;


路由守卫与路由的关系

路由守卫只只能应用于路由项上;路由守卫可以应用于多个路由项;每个路由项也可以有多个路由守卫;

路由守卫通过实现如下接口来操作:

canActivate: 控制是否允许进入路由。(通过return true/false决定)

canActivateChild: 等同 canActivate,只不过针对是所有子路由。

canDeactivate: 控制是否允许离开路由。

canLoad: 控制是否允许延迟加载整个模块。


作用

验证身份


RouteReuseStrategy

// Angular路由复用策略RouteReuseStrategy(常用于实现Tab页签切换页面)

参考上一篇文章

猜你喜欢

转载自blog.csdn.net/qq_27751965/article/details/96650972