vue 页面间传值
2024-10-21 06:27:11
使用params传参 ,不能使用path 只能使用name
使用params传参,刷新参数会消失
router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import login from '@/components/login/login'
import index from '@/components/index/index'
import HelloWorld from '@/components/HelloWorld'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
redirect: '/login'
},
{
path:'/index',
name : 'index',
component : index
},
{
path:'/login',
name : 'login',
component : login
}
]
})
login页面(params传参)
this.$router.replace({name:'index',params:{
username:successResponse.data.object.username,
phone:successResponse.data.object.phone
}
})
index页面
<template>
<div>
<hr>
<div>
This is username. <span v-text="username"> </span> <br>
This is the phone. <span v-text="phone"> </span> <br>
This is also username {{$route.params.username}}
</div>
<hr>
</div>
</template>
<script>
export default {
name : 'index',
//created 钩子函数 Vue 初始化时执行
created:function() {
this.getParams();
},
watch:{
//监测路由变化,只要变化了就调用路由参数方法将数据存储本组件即可
'$route':'getParams'
},
methods:{
getParams:function() {
//取得路由带过来的参数
let routeUsername = this.$route.params.username
let routePhone = this.$route.params.phone
//将数据放在当前组件的数据内
this.username = routeUsername
this.phone = routePhone
},
}
}
</script>
login页面(query传参)
this.$router.replace({path:'/index',query:{
username:successResponse.data.object.username,
phone:successResponse.data.object.phone
}
})
index页面
<template>
<div>
<hr>
<div>
This is username. <span v-text="username"> </span> <br>
This is the phone. <span v-text="phone"> </span> <br>
This is also username {{$route.query.username}}
</div>
<hr>
</div>
</template>
<script>
export default {
name : 'index',
//created 钩子函数 Vue 初始化时执行
created:function() {
this.getQuerys();
},
watch:{
//监测路由变化,只要变化了就调用路由参数方法将数据存储本组件即可
'$route':'getQuerys',
},
methods:{
getQuerys:function() {
//取得路由带过来的参数
let routeUsername = this.$route.query.username
let routePhone = this.$route.query.phone
//将数据放在当前组件的数据内
this.username = routeUsername
this.phone = routePhone
},
}
}
</script>
最新文章
- java 特性
- 利用fiddler模拟发送json数据的post请求
- [shell]. 点的含义
- Android简易数据存储之SharedPreferences
- Java并发——同步容器与并发容器
- 【Android】listview优化
- 基础算法-查找:线性索引查找(I)
- [原创].NET 业务框架开发实战之十 第一阶段总结,深入浅出,水到渠成(后篇)
- Thrift RPC实战(一).初次体验Thrift
- andorid之摄像头驱动流程--MTK平台
- 搭建Elasticsearch集群常见问题
- MyBatis 框架的搭建和配置
- Scheme来实现八皇后问题(1)
- 2019春第八周作业Compile Summarize
- 利用Python通过频谱分析和KNN完成iphone拨号的语音识别
- java使用POI解析2007以上的Excel表格
- Codeforces 1136E Nastya Hasn&#39;t Written a Legend 线段树
- Solidity字符串和函数
- 邮件服务器fixpost服务(1)
- dedecms添加新模型
热门文章
- Python 自己实现可迭代对象
- View Controller Programming Guid for iOS 笔记
- MySQL主从复制(Centos6.3&;MySQL5.6)
- Scala 中 call by name &; call by value 的区别
- Go语言的反射
- 利其器:无法在 ";.vscode"; 文件夹()内创建 ";launch.json"; 文件。
- POI进行导出时候发现有不可读取的内容
- Windows 对外开放端口号
- UML之九种图
- Codeforces Round #609 (Div. 2) C. Long Beautiful Integer