①路由的配置

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:路由的配置、传参以及跳转的更多相关文章

  1. vue路由对不同界面进行传参及跳转的总结

    最近在做一个公众号的商城项目,主要用的VUE+MUI,其实今天这个点对于有过项目经验的前端工作者来说是最基础的,但也是必须要掌握的,今天小编主要是记录下传参和跳转的一些总结(仅供参考). 首先我们先上 ...

  2. Vue Router路由导航及传参方式

    路由导航及传参方式 一.两种导航方式 ①:声明式导航 <router-link :to="..."> ②:编程式导航 router.push(...) 二.编程式导航参 ...

  3. vue路由传参并跳转页面

    在vue项目中参数的传递可以使用本地缓存或者Vuex,那么vue能不能像小程序一样路由传参呢,显然是可以的而且非常简单 方式一:query传参 //传参 go(){ that.$router.push ...

  4. ionic简单路由及页面传参

    1)页面跳转及传参方法 angular.module('app.routes', [])//routes路由模型 .config(function($stateProvider, $urlRouter ...

  5. Tornado学习笔记(二) 路由/post/get传参

    本章我们学习 Tornado 的路由传参等问题 路由 路由的匹配 Tornado的路由匹配采用的是正则匹配 一般情况下不需要多复杂的正则,正则的基本规则如下(站长之家) 举个例子 (r'/sum/(\ ...

  6. vue2.0路由写法、传参和嵌套

    前置知识请戳这里 vue-routerCDN地址:https://unpkg.com/vue-router@3.0.1/dist/vue-router.js vue-router下载地址:https: ...

  7. vue-router路由如何实现传参

    tip: 用params传参,F5强制刷新参数会被清空,用query,由于参数适用路径传参的所以F5强制刷新也不会被清空.(传参强烈建议适用string) 也可以选用sessionstorage/lo ...

  8. vue学习(6)-路由(导入包;创建子组件;创建路由对象)传参,子路由,多个组件

    后端路由:对于普通的网站,所有的超链接都是URL地址,所有的URL地址都对应服务器上对应的资源 前端路由:对于单页面应用程序来说,主要通过URL中的hash(#号)来实现不同页面之间的切换(不会刷新页 ...

  9. ng4 路由多参数传参以及接收

    import { Router } from '@angular/router'; constructor( private router:Router, ) { } goApplicationDet ...

随机推荐

  1. CentOS GRUB损坏修复方法

    前言 博客很久没有更新了,一个原因就是原来存放部署博客的环境坏了,硬盘使用的是SSD,只要读取到某个文件,整个磁盘就直接识别不到了,还好博客环境之前有做备份,最近一直没有把部署环境做下恢复,今天抽空把 ...

  2. 2020年最新ZooKeeper面试题(附答案)

    2020年最新ZooKeeper面试题 1. ZooKeeper 是什么? ZooKeeper 是一个开源的分布式协调服务.它是一个为分布式应用提供一致性服务的软件,分布式应用程序可以基于 Zooke ...

  3. SQL Server数据库Union和Union All查询出数据的区别?

    好久没有更新博客了,可能是最近比较忙,总是忽略了一些事情,今天查了做了一些数据分析的数据,突然感觉对Union和Union all有些不太理解了,可能是自己老了吧,就翻了一些资料,进行回忆和学习,趁着 ...

  4. C#高级编程之特性

    特性定义 MSDN的描述:使用特性,可以有效地将元数据或声明性信息与代码(程序集.类型.方法.属性等)相关联. 将特性与程序实体相关联后,可以在运行时使用反射这项技术查询特性. 参考此处作者的解释 h ...

  5. 每天一个linux命令之stat

    [Linux]Linux下使用stat命令所显示出来的三个时间 转 https://blog.csdn.net/pointer_y/article/details/54347968 在linux系统下 ...

  6. python-网络安全编程第四天(数据库编程&网络编程)

    前言 好几天没更因为寒假放假回家放松了几天 嘿嘿 今天继续开始启动学习模式. python数据库编程 Python DB API访问数据库流程 Python DB API包含的内容 什么是 PyMyS ...

  7. ci爬坑

    1.row_array() 问题描述:没有数据返回NULL,直接foreach,报错

  8. 如何用思维导图软件MindManager制作项目管理图表

    项目管理的官方解释为:运用各种相关技能.方法与工具,为满足或超越项目有关各方对项目的要求与期望,所开展的各种计划.组织.领导.控制等方面的活动. 其实使用MindManager思维导图软件来创建项目管 ...

  9. 两种方式教你搞定在mac中格式化磁盘的问题

    mac怎么格式化u盘?想必这是大部分苹果用户都会关心的一个问题.格式化u盘在我们日常工作中算是一个比较常规的操作了.但是在mac中随着系统版本不一样,格式化的方式也略有差别.今天,小编将以Mac OS ...

  10. Comparator比较器

    Comparator比较器 简介 为什么写? comparator 是javase中的接口,位于java.util包下,该接口抽象度极高,有必要掌握该接口的使用 大多数文章告诉大家comparator ...