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 ...
随机推荐
- Kernel RBD的QOS配置方案
前言 关于qos的讨论有很多,ceph内部也正在实现着一整套的基于dmclock的qos的方案,这个不是本篇的内容,之前在社区的邮件列表看过有研发在聊qos的相关的实现的,当时一个研发就提出了在使用k ...
- linux中KVM桥接网卡br0
在centos虚拟化当中需要增加一个桥接网卡,然后将虚拟化当中的机器的网卡桥接到桥接网卡,下面将描述设置方法: 查看现有网卡 [root@zb ~]# vim /etc/sysconfig/netwo ...
- tcp黏包问题与udp为什么不黏包
1.先说下subprocess模块的用法,为了举个黏包的例子 # 通过一个例子 来认识网络编程中的一个重要的概念 # 所有的客户端执行server端下发的指令,执行完毕后,客户端将执行结果给返回给服务 ...
- 一些 git 常用的命令
1.本地命令 查看状态 -git status 添加文件 -git add . 提交文件 -git commit -m "(comment)" 查看历史key -git reflo ...
- UML中常见的类关系你了解吗?
最近老大给我设计了一个微信扫码登录的通过工具包流程图,设计过程中使用了模板模式.面向接口编程等设计思路,让我很享受整个过程:下来我就接触了一下Java的设计模式,很是懵懂,听说这也是要靠经验来喂,才能 ...
- Monitor的扩展支持string的超时锁
对Monitor的使用可以防止lock的时间过长并且可以设置其对应的超时时间达到对预期代码的一个控制,合理的使用timeout可以有助于程序的健壮性.但是对于不同的并发程序可能某些时候我们需要的粒度是 ...
- day02-业务服务监控
提供大量第三方工具,可以开发企业级服务监控平台,本章涉及文件与目录差异对比.HTTP质量监控.邮件告警等内容一.文件内容差异比对1.示例1 d = difflib.Differ() diff = d. ...
- Guitar Pro 7教程之打开播放文件的操作技巧
前面的章节我们讲过了很多关于Guitar Pro的相关教程,由于最近{cms_selflink page='index' text='Guitar Pro'} 7中文版刚上新没多久,很多玩吉他的小伙伴 ...
- 【P1588】丢失的牛——区间dp/bfs
(题面来自Luogu) 题目描述 FJ丢失了他的一头牛,他决定追回他的牛.已知FJ和牛在一条直线上,初始位置分别为x和y,假定牛在原地不动.FJ的行走方式很特别:他每一次可以前进一步.后退一步或者直接 ...
- Codeforces Round #667 (Div. 3) B、C、D、E 题解
抱歉B.C题咕了这么久 B. Minimum Product #枚举 #贪心 题目链接 题意 给定四个整数\(a, b, x, y\),其中\(a\geq x, b\geq y\),你可以执行不超过\ ...