angular中的路由跳转
一、概述
- 定义:根据不同的
URL
地址,动态的让根组件挂载其它组件来实现一个单页面应用。
二、安装与配置
安装项目时
ng new 项目名
,在询问是否要安装带路由的项目包时,选择Y
。其它条件依据情况选择即可。带路由项目与不带路由项目的区别:
多了一个路由配置文件
app-routing.module.ts
在
app.module.ts
文件中引入了路由配置文件,并在imports
中注入了。import { AppRoutingModule } from './app-routing.module';
imports: [BrowserModule,AppRoutingModule],
在
app.component.html
文件中最后部分多了一个标签。用于放置动态加载的组件。<router-outlet></router-outlet>
app-routing.module.ts
import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router';const routes: Routes = [];@NgModule({imports: [RouterModule.forRoot(routes)],exports: [RouterModule] }) export class AppRoutingModule { }
三、具体实现
1.a
标签进行跳转
创建组件
ng g c home
,ng g c news
,ng g c product
将组件引入到根组件中,并在
declarations
中进行配置。(如果是通过命令生成组件,原则上会配置,但需要检查一下)import { NewsComponent } from './components/news/news.component'; import { HomeComponent } from './components/home/home.component'; import { ProductComponent } from './components/product/product.component';
declarations: [AppComponent,NewsComponent,HomeComponent,ProductComponent],
在路由配置文件中引入我们创建的组件,在
routes
对象中配置路由。import { HomeComponent } from './components/home/home.component'; import { NewsComponent } from './components/news/news.component'; import { ProductComponent } from './components/product/product.component';
const routes: Routes = [{ path: 'home', component: HomeComponent },{ path: 'news', component: NewsComponent },{ path: 'product', component: ProductComponent }, ];
配置无路由匹配时,显示首页组件
const routes: Routes = [{ path: '**', redirectTo: 'home' }, ];
通过
a
标签进行跳转(此时html文件)。[routerLink]
属性是用来动态设置跳转的路由,routerLinkActive
属性是用来设置路由被选中时的样式,需要在scss或者css
文件中声明active
类。<header class="header"><!-- 动态路由 --><a [routerLink]="[ '/home']" routerLinkActive="active">首页</a><!-- 静态路由 --><a routerLink= '/home' routerLinkActive="active">首页</a><a [routerLink]="[ '/news']" routerLinkActive="active">新闻</a><a [routerLink]="[ '/product']" routerLinkActive="active">商品</a> </header> <router-outlet></router-outlet>
1.1get
传值
路由get传值,创建新闻详情组件,并在路由文件app-routing.module.ts
中引入。
import { NewsDetailComponent } from './components/news-detail/news-detail.component';
const routes: Routes = [{ path: 'newsDetail', component: NewsDetailComponent },
];
在新闻页NewsComponent
的html
文件中通过[queryParams]
属性传递值
<ul><li *ngFor="let item of list let key = index"><!-- <a href="">{{key}}----{{item}}</a> --><a [routerLink]="[ '/newsDetail' ]" [queryParams]="{aid:key}">{{key}}----{{item}}</a></li>
</ul>
编写新闻页的ts
文件
export class NewsComponent implements OnInit {public list: any[] = [];constructor() { }ngOnInit(): void {for (var i = 0; i < 10; i++){this.list.push(`这是第${i}条数据`)}}
}
在新闻详情页NewsDetail
组件的ts
文件中通过this.route.queryParams.subscribe
接收
import { ActivatedRoute } from '@angular/router'export class NewsDetailComponent implements OnInit {constructor(public route: ActivatedRoute) {}ngOnInit(): void {// behaviorSubject对象,里面的value是我们传递过来的参数,但是不能直接通过.value获取,需要订阅。console.log(this.route.queryParams);this.route.queryParams.subscribe((data) => {console.log(data);})}}
1.2路由
传值
在app-routing.module.ts
中引入并定义,即配置动态路由。
import { NewsDetailComponent } from './components/news-detail/news-detail.component';
// 易错点:当路由中需要携带参数时,需要在冒号前加上斜杠
const routes: Routes = [{ path: 'newsDetail/:aid', component: NewsDetailComponent },
];
在新闻页NewsComponent
的html
文件中通过routerLink的第二个param来
传递值
<!-- // 易错点:当路由中需要携带参数时,需要在path末尾加上斜杠 -->
<ul><li *ngFor="let item of list ;let key =index"><a [routerLink]="[ '/newsDetail/', item ]">{{key}}----{{item}}</a></li>
</ul>
在新闻详情页NewsDetail
组件的ts
文件中通过this.route.params.subscribe
接收
import { ActivatedRoute } from '@angular/router'export class NewsDetailComponent implements OnInit {constructor(public route: ActivatedRoute) {}ngOnInit(): void {// behaviorSubject对象,里面的value是我们传递过来的参数,但是不能直接通过.value获取,需要订阅。console.log(this.route.params);this.route.params.subscribe(data => console.log(data))}}
2.js
跳转(在业务逻辑上实现跳转)
2.1路由
传值
(比如登录成功后跳转页面。)
在html
页面中定义与跳转相关的函数
<button (click)="goProductDetail()">js跳转路由</button><br><a [routerLink]="[ '/productDetail/', '1234' ]">跳转到商品详情</a><br><button (click)="goHome()">js跳转路由到首页</button><br>
在ts文件中引入router,并在构造函数中注入,然后通过this.router.navigate()进行跳转
import { Component, OnInit } from '@angular/core';
import { Router} from '@angular/router'export class ProductComponent implements OnInit {constructor(public router: Router) { }ngOnInit(): void {}goProductDetail() {// 普通路由和动态路由都适合this.router.navigate(['/productDetail/', '1234'])}goHome() {this.router.navigate(['/home'])}
}
2.1get
传值
在html
文件中
<button (click)="goNews()">get传值跳转路由</button>
在ts
中引入NavigationExtras
,实现跳转
import { Router,NavigationExtras} from '@angular/router';export class ProductComponent implements OnInit {constructor(public router: Router) { }ngOnInit(): void {}goNews() {// 跳转并进行get传值// 引入这个NavigationExtras可以更标准一点let queryParams: NavigationExtras = {queryParams:{'aid':123}}// 注意此时第要传递的参数是放在中括号外面的。this.router.navigate(['/news'], queryParams);}
}
3.嵌套路由
在app-routing.module.ts
中引入,并且给父路由配置子路由。给home路由和product路由配置子路由children
// 引入子路由组件
import { PlistComponent } from './components/product/plist/plist.component';
import { PcateComponent } from './components/product/pcate/pcate.component';
import { WelcomeComponent } from './components/home/welcome/welcome.component';
import { SettingComponent } from './components/home/setting/setting.component';{path: 'home', component: HomeComponent,children: [{ path: 'welcome', component: WelcomeComponent },{ path: 'setting', component: SettingComponent },{ path: '**', redirectTo: 'welcome' },]},{ path: 'news', component: NewsComponent },{path: 'product', component: ProductComponent,children: [{ path: 'list', component: PlistComponent },{ path: 'cate', component: PcateComponent },{ path: '**', redirectTo: 'list' },]},
在home页面的html文件中,通过a标签进行跳转,并且用router-outlet
占位。product页面同。
<div class="content"><div class="left"><a [routerLink]="[ '/home/welcome']" routerLinkActive="active">欢迎你</a><br><a [routerLink]="[ '/home/setting']" routerLinkActive="active">系统设置</a></div><div class="right"><router-outlet></router-outlet></div>
</div>
css
样式文件
.content{width: 100%;height: 500px;display: flex;
}
.left{width: 200px;border-right: 1px solid black;height: 500px;
}
.right{flex: 1;
}
.active{color: red;
}
angular中的路由跳转相关推荐
- Angular中实现路由跳转并通过get方式传递参数
场景 Angular介绍.安装Angular Cli.创建Angular项目入门教程: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/detail ...
- vue中实现路由跳转的三种方式(超详细整理)
vue中实现路由跳转的三种方式 一.使用vue-router vue-router 本质是一个第三方的包 用的时候需要下载 步骤 (7步法 ): 下载vue-router模块到当前工程 yarn ad ...
- Angular中的路由配置、路由重定向、默认选中路由
场景 Angular介绍.安装Angular Cli.创建Angular项目入门教程: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/detail ...
- flutter中的路由跳转
在前面的基本路由和命名路由中,都演示了如何进行路由跳转,并且在路由跳转以后,可以借用系统自带的按钮就行返回上一级,当然了,也可以自定义按钮返回上一级. 返回上一级 在前面的例子中,当从Home.dar ...
- vue 函数 路由跳转_vue中通过路由跳转的三种方式
router-view 实现路由内容的地方,引入组件时写到需要引入的地方 需要注意的是,使用vue-router控制路由则必须router-view作为容器. 通过路由跳转的三种方式 1.router ...
- 写在方法中的路由跳转
场景:login页面点击登陆按钮,登陆成功后,进入home页面:(当然,这里就不能用Link跳转了) 问题:history的报错: 解决:需要引入react-router-dom中的withRoute ...
- vue中的路由跳转和传参
一:注意:在 Vue 实例中,你可以通过 $router 访问路由实例.因此你可以调用 this.$router.push("路由地址")来跳转到目标路由页面: 想要导航到不同的 ...
- vue路由跳转权限_如何在vue中实现路由跳转判断用户权限功能?
实现这类校验有几个步骤. 1.设置路由是否需要校验的阀值. // 路由配置的地方谁知阀值 routes:[ { name:'admin', path:'/admin', component:'..., ...
- Vue3中实现路由跳转的过渡动画(一)
由于VueRouter官方文档写的太过抽象,真的特别不好理解,所以我来总结一下如何去更简单地理解路由跳转时的动画. 首先看一下官方对于这个概念的解释. 在刚开始学习的时候,看到黄色框框里内容直接傻眼, ...
最新文章
- 小程序判断用户在线状态
- 50多种适合机器学习和预测应用的API,你的选择是?(2018年版本)
- 【Web安全】中国蚁剑+DVWA(本地文件上传漏洞Upload)
- GeneXus 图片的创建与删除1
- mPaaS 月度小报 | 3月发生的大事件
- 前端 <table><td><tr><th>
- python合法变量类型_Python 变量类型
- Android 五大布局简析
- 况客:Campisi债券业绩归因模型
- An动画基础之元件的影片剪辑效果
- YOLO系列-yolov3
- 手持两把锟斤拷,口中疾呼烫烫烫。脚踏千朵屯屯屯,笑看万物锘锘锘。
- 媲美celery的分布式调度框架funboost
- something about Android activity
- Linux中kil命令和pkill命令的区别
- nmn是什么公司产品,nmn对男性的影响,很是惊喜
- 你的房间总是一团乱吗?
- AppCompat 22 1 Google暴走,MD全面兼容低版本
- 我是头好马,但要吃次回头草了。
- nginx mysql 网页显示_Win10+Python+Django+Nginx+MySQL开发教程及实例(3)——Nginx运行html网页...
热门文章
- 0基础怎么学习大数据,大数据学习路线及学习资料
- 数仓业务上判断一个表的唯一主键是哪几个字段
- 绝~ 阿里内部“Java进阶必备宝典”,理论到实战,一键通关
- 一个可在微信聊天框中生成短链接的微信公众号
- DIAsource——25OH维生素D抗体丨抗原与结合物研究
- 微型计算机gl703评测,华硕GL703笔记本电脑评测
- php友情链接怎么做,如何让zblogphp程序友情链接仅在网站首页显示?做手赚网必知!...
- C++复习day1:知识点概述(依据C++premier plus)
- linux修改用户名、密码、用户组、设备名教程
- 墨菲定律-第二天之巴纳姆效应