ActivatedRoute 当前激活的路由对象
ActivatedRoute,当前激活的路由对象,主要用于保存路由,获取路由传递的参数。
一:传递参数的三种方式,以及ActivatedRoute获取他们的方式:
1.在查询参数中传递数据:
/product?id=1&name=2
/**
获取参数
*/
=>ActivatedRoute.queryParams[id]
2.在路由路径中传递数据:
//路由配置
{path:'/product/:id'}
//路由显示方式Restful
=>/product/1
/**
获取参数
*/
=>ActivatedRoute.params[id]
3.在路由配置中传递数据:
//路由配置
{path:'/product',component:ProductComponent,data:[{isProd:true}]}
/**
获取参数
*/
=>ActivatedRoute.data[0][isProd]
二:ActivatedRoute接收参数类型
1.参数快照(snapshot):
import { Component, OnInit } from '@angular/core';
import{ActivatedRoute,Params} from '@angular/router';
export class ProductComponent implements OnInit {
constructor(private activateInfo:ActivatedRoute) { }
ngOnInit() {
let id:string= this.activateInfo.snapshot.params["id"];
console.log(id);
}
}
2.参数订阅(subscribe):
import { Component, OnInit } from '@angular/core';
import{ActivatedRoute,Params} from '@angular/router';
export class ProductComponent implements OnInit {
constructor(private activateInfo:ActivatedRoute) { }
productId:string;
ngOnInit() {
this.activateInfo.params.subscribe(
(params:Params)=>{
this.productId=params["id"];
console.log(this.productId);
}
)
}
}
这两种方式主要区别是由于nginit在页面第一次加载时会进行初始化,但是第二次不会走nginit的方法,如果同一个页面显示两次,传递不同的参数,快照版式无法实时获取新的参数。
ActivatedRoute 当前激活的路由对象的更多相关文章
- vue常用的路由对象
官网上解释:一个路由对象表示当前激活的路由的状态信息 路由对象,在组件内即this.$route,存着一些与路由相关的信息,当路由切换时,路由对象会被更新 //如果要在刷新页面时候通过路由的信息来操作 ...
- ASP.NET Web API 路由对象介绍
ASP.NET Web API 路由对象介绍 前言 在ASP.NET.ASP.NET MVC和ASP.NET Web API这些框架中都会发现有路由的身影,它们的原理都差不多,只不过在不同的环境下作了 ...
- vue路由对象($route)参数简介
路由对象在使用了 vue-router 的应用中,路由对象会被注入每个组件中,赋值为 this.$route ,并且当路由切换时,路由对象会被更新. so , 路由对象暴露了以下属性: 1.$rout ...
- Vue-Router路由 Vue-CLI脚手架和模块化开发 之 使用路由对象获取参数
使用路由对象$route获取参数: 1.params: 参数获取:使用$route.params获取参数: 参数传递: URL传参:例 <route-linke to : "/food ...
- 路由对象route
路由对象是不可变 (immutable) 的,每次成功的导航后都会产生一个新的对象.不过你可以 watch (监测变化) 它. 通过 this.$route 访问当前路由,还可以通过router.ma ...
- Vue-Router路由Vue-CLI脚手架和模块化开发 之 使用props替代路由对象的方式获取参数
在上一章博文中使用路由对象$route获取参数时,组件和路由对象耦合,在这篇博文中就可以使用props来进行解耦: 1.在组件中使用props选项定义数据,接收参数: 2.在路由中,使用props选项 ...
- vue 路由对象(常用的)
路由对象 在使用了 vue-router 的应用中,路由对象会被注入每个组件中,赋值为 this.$route ,并且当路由切换时,路由对象会被更新. 路由对象暴露了以下属性: $route.path ...
- vue 路由对象
路由对象在使用了 vue-router 的应用中,路由对象会被注入每个组件中,赋值为 this.$route ,并且当路由切换时,路由对象会被更新. so , 路由对象暴露了以下属性: 1.$rout ...
- vue学习(6)-路由(导入包;创建子组件;创建路由对象)传参,子路由,多个组件
后端路由:对于普通的网站,所有的超链接都是URL地址,所有的URL地址都对应服务器上对应的资源 前端路由:对于单页面应用程序来说,主要通过URL中的hash(#号)来实现不同页面之间的切换(不会刷新页 ...
随机推荐
- ubuntu18.04安装谷歌浏览器
sudo wget http://www.linuxidc.com/files/repo/google-chrome.list -P /etc/apt/sources.list.d/ wget -q ...
- Linux-->Mysql:安装,测试
环境准备 mysql下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-linux-glibc2.12-x86_64.tar ...
- Beyond Compare 4的试用期过了怎么办
修改配置文件(C:\Users\gaojs\AppData\Roaming\BCompare\BCompare.ini)中的时间戳. 时间戳在线转换:https://tool.lu/timestamp ...
- June 20th 2017 Week 25th Tuesday
Care and diligence bring luck. 谨慎和勤奋,带来好运气. In my opinion, care and diligence may just gurantee us a ...
- February 21 2017 Week 8 Tuesday
To make each day count. 让每一天都物有所值. We always want to make our life meaningful, however, the acutal f ...
- ZT 类与类之间的四种关系
csdn上一个好贴子:http://bbs.csdn.net/topics/390646332 类与类之间的四种关系1.依赖(Dependency) 类A在类B中作为一个成员函数的参数或者是返回值 ...
- Windows文件批量重命名
选择要命名的文件 按F2,编辑名字 然后按回车就行了 电视剧命名,我认为这样足够了
- MongoDB创建集合、删除集合
创建集合 createCollection() 方法 在 MongoDB 中,创建集合采用 db.createCollection(name, options) 方法. 语法格式 createColl ...
- MATLAB入门学习(一)
开始MATLAB入门啦,,,首先感谢xyy大神的帮助!然后我们开始学习吧!<( ̄︶ ̄)↗[GO!] 工作空间窗口:保存了你定义的常量,变量之类的,可以保存也可以被调用. 保存的话会生成一个mat ...
- Xcode 7提示App Transport Security has blocked a cleartext HTTP (http://) resource load的解决办法
Xcode 7提示App Transport Security has blocked a cleartext HTTP (http://) resource load的解决办法 今天使用Xcod ...