Angular:路由的配置、传参以及跳转
①路由的配置
1、首先用脚手架新建一个项目,在路由配置时选择yes

2、用ng g component创建组件

3、在src/app/app-routing.module.ts中配置路由
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { FirstComponent } from './components/first/first.component';
import { TwoComponent } from './components/two/two.component';
import { FirstChildrenComponent } from './components/first-children/first-children.component';
import { TwoChildrenComponent } from './components/two-children/two-children.component';
import { ThreeComponent } from './components/three/three.component';
const routes: Routes = [
// { path: '', component: FirstComponent }, //表示匹配到'/'路径,显示FirstComponent组件
{
path: 'first', component: FirstComponent,
children: [ //设置子路由
{ path: 'firstC/:hxId', component: FirstChildrenComponent } //设置动态路由
]
},
{
path: 'two', component: TwoComponent,
children: [
{ path: 'twoC', component: TwoChildrenComponent }
]
},
{
path: 'three', component: ThreeComponent
},
// { path: '**', component: FirstComponent }, //**表示匹配任意路径,显示FirstComponent组件
{ path: '', redirectTo: '/first', pathMatch: 'full' }, //表示匹配到'/'路径,重定向到'/first'路径
// { path: '**', redirectTo: '/two', pathMatch: 'full' } //**表示匹配任意路径,重定向到'/first'路径
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
②路由传参
1、使用动态路由传参
在first.component.html中,进行路由跳转,传参
<p>我用动态路由进行传参</p>
<ul>
<!-- 使用动态路由 -->
<li><a [routerLink]="[ '/first/firstC/',1]">我是商品1的详情</a></li>
</ul>
<router-outlet></router-outlet>
<button (click)="tiaoZhuan()">js跳转路由</button>
在first-children.component.ts子组件中引入ActivatedRoute模块,接收参数
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-first-children',
templateUrl: './first-children.component.html',
styleUrls: ['./first-children.component.less']
})
export class FirstChildrenComponent implements OnInit {
constructor(public route: ActivatedRoute) { }
ngOnInit(): void {
console.log(this.route);
this.route.params.subscribe({
next(res): any {
console.log(res);
}
});
}
}
2、使用get传参
在two.component.html中,进行路由跳转,传参
<p>我用get进行传参</p>
<ul>
<!-- 使用get传参 -->
<li><a [routerLink]="['/two/twoC']" [queryParams]="{hxId:1}">我是商品1的详情</a></li>
</ul>
<router-outlet></router-outlet>
在two-children.component.ts子组件中引入ActivatedRoute模块,接收参数
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-two-children',
templateUrl: './two-children.component.html',
styleUrls: ['./two-children.component.less']
})
export class TwoChildrenComponent implements OnInit {
constructor(public route: ActivatedRoute) { }
ngOnInit(): void {
console.log(this.route);
this.route.queryParams.subscribe({
next(res): any {
console.log(res);
}
});
}
}
③js路由跳转
在first.component.html中,进行路由跳转
<p>我用动态路由进行传参</p> <!-- js跳转路由 -->
<button (click)="tiaoZhuan()">js跳转路由</button>
在first.component.ts中实现,引入Router模块
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-first',
templateUrl: './first.component.html',
styleUrls: ['./first.component.less']
})
export class FirstComponent implements OnInit {
constructor(public router: Router) { }
ngOnInit(): void {
}
tiaoZhuan(): any {
console.log(this.router);
this.router.navigate(['/three']); //实现路由跳转,也可以用动态路由或者get方式传参
}
}
Angular:路由的配置、传参以及跳转的更多相关文章
- vue路由对不同界面进行传参及跳转的总结
最近在做一个公众号的商城项目,主要用的VUE+MUI,其实今天这个点对于有过项目经验的前端工作者来说是最基础的,但也是必须要掌握的,今天小编主要是记录下传参和跳转的一些总结(仅供参考). 首先我们先上 ...
- Vue Router路由导航及传参方式
路由导航及传参方式 一.两种导航方式 ①:声明式导航 <router-link :to="..."> ②:编程式导航 router.push(...) 二.编程式导航参 ...
- vue路由传参并跳转页面
在vue项目中参数的传递可以使用本地缓存或者Vuex,那么vue能不能像小程序一样路由传参呢,显然是可以的而且非常简单 方式一:query传参 //传参 go(){ that.$router.push ...
- ionic简单路由及页面传参
1)页面跳转及传参方法 angular.module('app.routes', [])//routes路由模型 .config(function($stateProvider, $urlRouter ...
- Tornado学习笔记(二) 路由/post/get传参
本章我们学习 Tornado 的路由传参等问题 路由 路由的匹配 Tornado的路由匹配采用的是正则匹配 一般情况下不需要多复杂的正则,正则的基本规则如下(站长之家) 举个例子 (r'/sum/(\ ...
- vue2.0路由写法、传参和嵌套
前置知识请戳这里 vue-routerCDN地址:https://unpkg.com/vue-router@3.0.1/dist/vue-router.js vue-router下载地址:https: ...
- vue-router路由如何实现传参
tip: 用params传参,F5强制刷新参数会被清空,用query,由于参数适用路径传参的所以F5强制刷新也不会被清空.(传参强烈建议适用string) 也可以选用sessionstorage/lo ...
- vue学习(6)-路由(导入包;创建子组件;创建路由对象)传参,子路由,多个组件
后端路由:对于普通的网站,所有的超链接都是URL地址,所有的URL地址都对应服务器上对应的资源 前端路由:对于单页面应用程序来说,主要通过URL中的hash(#号)来实现不同页面之间的切换(不会刷新页 ...
- ng4 路由多参数传参以及接收
import { Router } from '@angular/router'; constructor( private router:Router, ) { } goApplicationDet ...
随机推荐
- 01 Servlet技术
Servlet 1.Servlet技术 1.1.什么是Servlet Servlet是JavaEE规范之一.规范就是接口 Servlet就JavaWeb三大组件之一.三大组件分别是:Servlet程序 ...
- 支持jewel版本的calamari
之前测试了下,发现calamari不支持jewel版本的,是因为接口了有了一些变化,在提出这个问题后,作者给出了回答,说肯定会支持的,并且做了一点小的改动,就可以支持了,这个作者merge了到了git ...
- HDU100题简要题解(2080~2089)
//2089之前忘做了,周二C语言课上做,至于2086,写题解的时候突然发现之前的做法是错的,新的解法交上去CE,等周二再弄吧,其余题目暂时可以放心 HDU2080 夹角有多大II 题目链接 Prob ...
- Hbase启动报java异常
在conf文件夹下的hbase-env.sh文件中的j添加ava_home的环境变量, ******************************************************** ...
- Camtasia中对录制视频进行编辑——旁白
相信很多人都遇见过想要录制视频,但是不知道在电脑上用哪一款软件比较好,害怕自己录的视频导出来之后会有水印,或者在录制的过程中遇到麻烦,更或者下载一款带有病毒的软件.那么今天我便给大家推荐一款专业录制屏 ...
- Java8常用的内置函数式接口(一)Predicate、Consumer、Supplier、Function
Java8常用的内置函数式接口(一) 简介 JDK 1.8 API中包含了很多内置的函数式接口.有些是在以前版本的Java中大家耳熟能详的,例如Comparator接口,或者Runnable接口.对这 ...
- iOS程序内实现版本更新
最近这段时间刚把手头里面的两个项目交付出去,很想写点东西但又不想随随便便的写些抒情的文字,其实生活中的很多事情.成长的路上遇到的很多问题,并非简简单单的抱怨.埋怨,用一种激情悲昂的情绪去逃避.去发泄所 ...
- Java数组作业
1 //输入一组数(10个),找出最大值,并给出最大值的位置 2 package test; 3 4 public class test1_1 { 5 public static double max ...
- python安装第三方库aiohtpp,sanio失败,pip install multidict 失败问题
1.python的第三库安装地址:http://www.lfd.uci.edu/~gohlke/pythonlibs 2. 3.pip安装.whl文件指定该文件的位置
- otter搭建
转载: https://blog.csdn.net/inthat/article/details/93595156 https://www.cnblogs.com/Inspire-Yi/p/80943 ...