①路由的配置

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. 如何避免Cephfs被完全毁掉

    前提 一套系统的最低要求是可恢复,也就是数据不丢失,但是在各种各样的原因下,整套系统都有被毁掉的可能,一直以来有个观点就是存储是需要两套的,一般情况下很难实现,但是如何把故障发生的概率降低到最低,这个 ...

  2. ubuntu配置bonding

    如果节点上有多个网络接口时可以通过bonding将多个网络接口虚拟为一个网络接口,bonding可以提供高可用及负载均衡功能,从而提高节点的网络接口性能及可用性. 配置单bond 一.使用如下命令安装 ...

  3. IGH_Master主站配置驱动伺服电机和变频器总结

    IGH_Master主站配置驱动伺服电机和变频器总结 Ethercat是倍福公司提出的一种工业现场总线协议,具有很好的实时性,IGH是一种开源的Ethercat主站实现协议,本文总结了一下使用IGH_ ...

  4. python+selenium+chromedriver抓取shodan搜索结果

    作用:免积分抓取shodan的搜索结果,并把IP保存为txt 前提: ①shodan会员(ps:黑色星期五打折) ②安装有python27 ③谷歌浏览器(ps:版本一定要跟chromedriver匹配 ...

  5. [代码审计Day1] in_array代码审计

    简介 1 简介  in_array() 函数搜索数组中是否存在指定的值. 语法:in_array(search,array,type) 参数 描述 search 必需.规定要在数组搜索的值. arra ...

  6. 还不懂Java高并发的,建议看看这篇阿里大佬的总结,写的非常详细

    前言 进程是计算机中程序关于某几何数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位.是操作系统结构的基础 线程可以说是轻量级的进程,是程序执行的最小单位,使用多线程而不用多进程去进行并发程 ...

  7. 新鲜出炉!2020年最新java面试题大全,面试突击必备!

    前言 发现网上很多Java面试题都没有答案,所以花了很长时间搜集整理出来了一套Java面试题,希望对大家有帮助哈~ 打算这几天每天更新15~20题.(这样有助于你们阅读和理解!)我们先从简单的开始 1 ...

  8. selenium WebDriver提示Unable to find a matching set of capabilities解决方法

    问题出在:应该将火狐浏览器驱动添加到火狐浏览器安装目录下,并且将火狐浏览器安装目录放在path下面.(出现大意,忘了在火狐浏览器下放其对应的驱动) 亲测以下组合方式可用:   pycharm-comm ...

  9. mac搭建mnmp环境

    brew安装nginx brew install nginx 安装php56 brew tap homebrew/dupes brew tap josegonzalez/homebrew-php br ...

  10. zabbix的搭建及操作(4)实现邮件,钉钉,微信报警

    实现邮件报警 网页版邮箱中开启 POP3/SMTP/IMAP 生成授权码并记录 Server端安装配置邮件服务器 1.Yum安装邮件服务器 yum -y install mailx dos2unix ...