文章目录

标签routerLink路由传递参数

url中get传值

// queryParams 传递的是一个对象
<a [routerLink]="[ '/endpage/']" [queryParams]="{name:'huangbiao',age:30}">endpage</a>

定义路由

const routes: Routes = [
{ path: 'endpage', component: EndPageComponent }
];
url的地址是 http://localhost:4200/endpage?name=huangbiao&age=30

获取参数

import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router'; @Component({
selector: 'app-end-page',
templateUrl: './end-page.component.html',
styleUrls: ['./end-page.component.scss']
})
export class EndPageComponent implements OnInit {
constructor(public route:ActivatedRoute) { } ngOnInit() {
console.dir(this.route);
debugger
this.route.params.subscribe((data)=>{
console.log(data); // 打印的是一个对象,key为name和age
});
} }

获取参数要依赖注入 route:ActivatedRoute对象

配置动态路由

// 第一个参数对应路由的name,第二个参数对应路由的age
<a [routerLink]="[ '/endpage/', 'huangbiao','30' ]">endpage</a>

定义路由

const routes: Routes = [
{ path: 'endpage/:name/:age', component: EndPageComponent }
];
url地址是 http://localhost:4200/endpage/huangbiao/30

获取参数

import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router'; @Component({
selector: 'app-end-page',
templateUrl: './end-page.component.html',
styleUrls: ['./end-page.component.scss']
})
export class EndPageComponent implements OnInit {
constructor(public route:ActivatedRoute) { } ngOnInit() {
console.dir(this.route);
debugger
this.route.params.subscribe((data)=>{
console.log(data); // 打印的是一个对象,key为name和age
});
} }

API js路由跳转

配置动态路由

this.router.navigate(['/newscontent/','1243'])
//this.router.navigate(['/home']);

如果是动态路由,则路由后面的/是不能少的

定义路由

const routes: Routes = [
{ path: 'endpage', component: EndPageComponent }
];
url地址是 http://localhost:4200/endpage/huangbiao/30

获取参数

import { Component, OnInit } from '@angular/core';
import { Router} from '@angular/router'; @Component({
selector: 'app-end-page',
templateUrl: './end-page.component.html',
styleUrls: ['./end-page.component.scss']
})
export class EndPageComponent implements OnInit {
constructor(public router:Router) { } ngOnInit() {
console.dir(this.route);
debugger
this.route.params.subscribe((data)=>{
console.log(data); // 打印的是一个对象
});
} }

get传值

this.router.navigate(['/news'],{
queryParams:{
aid:123
}
});

定义路由

const routes: Routes = [
{ path: 'endpage', component: EndPageComponent }
];
url地址是 http://localhost:4200/endpage/huangbiao/30

获取参数

import { Component, OnInit } from '@angular/core';
import { Router} from '@angular/router'; @Component({
selector: 'app-end-page',
templateUrl: './end-page.component.html',
styleUrls: ['./end-page.component.scss']
})
export class EndPageComponent implements OnInit {
constructor(public router:Router) { } ngOnInit() {
console.dir(this.route);
debugger
this.route.params.subscribe((data)=>{
console.log(data); // 打印的是一个对象
});
} }

  

1.以根路由跳转/login

this.router.navigate(['login']);

2.设置relativeTo相对当前路由跳转,route是ActivatedRoute的实例,使用需要导入ActivatedRoute

this.router.navigate(['login', 1],{relativeTo: route});

3.路由中传参数 /login?name=1

this.router.navigate(['login', 1],{ queryParams: { name: 1 } });

4.preserveQueryParams默认值为false,设为true,保留之前路由中的查询参数/login?name=1 to /home?name=1

this.router.navigate(['home'], { preserveQueryParams: true });

5.路由中锚点跳转 /home#top

this.router.navigate(['home'],{ fragment: 'top' });

6.preserveFragment默认为false,设为true,保留之前路由中的锚点/home#top to /role#top

this.router.navigate(['/role'], { preserveFragment: true });

7.skipLocationChange默认为false,设为true,路由跳转时浏览器中的url会保持不变,但是传入的参数依然有效

this.router.navigate(['/home'], { skipLocationChange: true });

8.replaceUrl默认为true,设为false,路由不会进行跳转

this.router.navigate(['/home'], { replaceUrl: true });

angular路由学习笔记的更多相关文章

  1. Angular快速学习笔记(2) -- 架构

    0. angular 与angular js angular 1.0 google改名为Angular js 新版本的,2.0以上的,继续叫angular,但是除了名字还叫angular,已经是一个全 ...

  2. Angular 快速学习笔记(1) -- 官方示例要点

    创建组件 ng generate component heroes {{ hero.name }} {{}}语法绑定数据 管道pipe 格式化数据 <h2>{{ hero.name | u ...

  3. Angular【学习笔记】

    1.angular入门网站 感谢@菜鸟教程:http://www.runoob.com/angularjs/angularjs-tutorial.html 学习笔记:

  4. Angular 4 学习笔记 从入门到实战 打造在线竞拍网站 基础知识 快速入门 个人感悟

    最近搞到手了一部Angular4的视频教程,这几天正好有时间变学了一下,可以用来做一些前后端分离的网站,也可以直接去打包web app. 环境&版本信息声明 运行ng -v @angular/ ...

  5. angular.js学习笔记--概念总结

    好久没更新了,现在开始学习学习angularjs,并且把学习到的一些知识总结记录一下,方便以后查找以及希望能给初学者一些帮助!(由于本人也是初学ng所以各位慎重理解!) 废话不多说,开始! $root ...

  6. Angular2之路由学习笔记

    目前工作中项目的主要技术栈是Angular2 在这里简单记录一下遇到的问题以及解决方案. 这篇笔记主要记录Angular2 的路由. 官方文档链接:https://angular.cn/docs/ts ...

  7. Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)

    刚学没多久,作了一个小项目APP,微信企业号开发与微信服务号的开发,使用的是AngularJS开发,目前项目1.0版本已经完结,但是项目纯粹为了赶工,并没有发挥AngularJS的最大作用,这几天项目 ...

  8. Angular.js学习笔记(三)

    一.过滤器 1.uppercase,lowercase 大小写转换{{ "lower cap string" | uppercase }} // 结果:LOWER CAP STRI ...

  9. angular.js学习笔记(二)

    1.安装core ,shared模块 ng g m core  ng g m shared 2.在shared中导入core模块   core模块只加载一次所以将公共组件放到core中 3.创建公共组 ...

随机推荐

  1. Jenkins+Jmeter+Ant自动化持续集成环境搭建

    [前言] 今天让我们先把准备工作做好 >> 搭建环境! [正文] JDK+Jmeter安装教程: 参考博客:https://www.cnblogs.com/baoziluo/p/79056 ...

  2. 前后分离调用API跨域

    前后分离调用API接口跨域问题 什么是跨域?  跨域是指一个域下的文档或脚本试图去请求另一个域下的资源,这里跨域是广义的. 广义的跨域: 资源跳转:A链接.重定向.表单提交. 资源嵌入: <li ...

  3. string类型的方法

    var str1 = ' this is string1 '; var str2 = 'this is string2'; str1.indexOf('s'); //args:string retur ...

  4. C# AD 验证登陆

    using System.DirectoryServices; using System.DirectoryServices.AccountManagement; using (DirectoryEn ...

  5. Django-- CRM1客户建表与装饰器

    一.CRM项目(1) 引入三个表:用户表,客户表,校区表,班级表,梳理逻辑关系并迁移数据库,生成表. 使用admin插入数据,admin是Django提供的web形式的后台数据管理页面,它是和用户认证 ...

  6. CAS操作

    CAS操作: Compare and Swap,比较并操作,CPU指令,在大多数处理器架构,包括IA32.Space中采用的都是CAS指令,CAS的语义是“我认为V的值应该为A,如果是,那么将V的值更 ...

  7. Linux Shell命令系列(4)

    16. cat命令 “cat”代表了连结(Concatenation),连接两个或者更多文本文件或者以标准输出形式打印文件的内容. 17. cp 命令 “copy”就是复制.它会从一个地方复制一个文件 ...

  8. (转)认识 Linux 文件系统

    7.1 认识 Linux 文件系统 原文:https://wizardforcel.gitbooks.io/vbird-linux-basic-4e/content/59.html Linux 最传统 ...

  9. The bytes/str dichotomy in Python 3 [transport]

    reference and transporting from: http://eli.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-p ...

  10. Maven的学习资料收集--(二)安装m2eclipse插件

    在Eclipse中可以安装Maven插件,可以更方便的使用: 官网地址:http://www.eclipse.org/m2e/ 可以在线安装或者离线下载,之前在线安装总是失败,可能是网速的原因,找到了 ...