Angular 项目创建时,就会问你是否需要创建项目 routing,你可以选择是。如果你创建时没有选择,也可以手动添加一个app-routing.module.ts 文件如下:
在 app.module.ts 中引入并指明 routing 模块
Angular 通过在 html 中加入 <router-outlet></router-outlet> 来实现 view 的占位符,上面路由文件中设置的路由,必须配合 view 的占位符一起使用。
在 app.component.html 中加入占位符
在 app-routing.module.ts 中添加路由。下面的 LoginComponent, ForgetpswComponent,都是我们通过 ng g c [componentname] 方式来创建的各种 component.
那么在browser 中输入: http://localhost:4200/login, 就会看到我们在 LoginComponent 中定义的界面。
根据业务要求,我们有时需要在路由中内嵌路由。
例如:
第一级路由包括:登录页面,密码找回页面,重置密码页面,登录后的主页面
在‘登录后的主页面中’,导航栏和工具栏是不变的,而导航栏之外的各个内容的变换又需要用到子级路由去显示:用户信息列表,产品信息列表,组织列表等。
我们的思路是:
App.component.html 一级路由
App-root.component.html 登陆后主页面,二级路由
路由配置:
绝对路径:
<a routerLink="/app"> </a>
<a routerLink="users"> </a>
2.Js 中跳转
导入router
绝对路径:
this.router.navigate(["login"]); //不带参数
相对路径:
原文:https://www.cnblogs.com/crdanding/p/12097535.html