vue项目各页面间的传值
githut地址:https://github.com/liguoyong/vueobj1
一、父子之间主键传值:(主要是在父主件里的子主件传递参数,然后再子主件里用props接收)
例如Father.vue
<template>
<div class="father"> <Son :value="" title="hello" :inputVal="val"
@sendData="testAction">
</Son> <button @click="testAction()">按钮</button> </div>
</template> <script>
import Son from './Son.vue'
export default {
components: {
Son
},
data(){
return {
message: 'hello vue',
val: ''
}
},
methods: {
sendAction(){
this.val = this.$refs.in.value;
}
}
}
</script>
例如son.vue
export default {
//接收组件标签上的属性
//外部属性,只能访问,不能修改
// 单向数据流:保证数据是自顶向下的
// props: ['value', 'title']
props: {
value: Number,
title: String,
inputVal: String
},
//内部属性
data(){
return {
name: this.title
}
},
methods: {
modify(){
this.name = 'world';
},
sendAction(){
let value = this.$refs.in.value;
//调用click事件
// 触发组件标签上的自定义事件
// 参数1:事件名字
// 参数2:传递的值sendData
this.$emit('sendData', value, ,,,,);
}
}
import Vue from 'vue'
import App from './App.vue'
import center from './center' Vue.prototype.$center = center; const vm = new Vue({
el: '#app',
render: h=>h(App)
})
center.js文件: export default {
$on: function(eventName, callback){
// 根据事件名字获得了回调
// 保存所有的回调函数
}, $emit: function(eventName, ...rest){
// 根据事件名字,调用对应的回调函数
// 调出来之前保存的相同事件名字的回调函数,一个一个执行。
},
$off: function(){
}
}
methods: {
//发送事件:(触发事件发送参数)
sendAction(){
console.log(this.value);
//触发事件
this.$center.$emit('send-data', this.value);
}
}
bb.vue文件:(接收参数)
created() {
// 监听事件
this.$center.$on('send-data', this.listener);
},
beforeDestroy(){
console.log('组件销毁前');
//移除监听
this.$center.$off('send-data', this.listener);
}
三.页面跳转通过路由带参数传递数据
// 1.页面中的代码
this.$router.push({
name: 'generalAdminOrderFlowAdd',
params: {
type: 'add',
templateType: this.orderTemplateType
}
})
或 this.$router.push({
name: 'routePage',
query/params: {
routeParams: params
}
})
需要注意的是,实用params去传值的时候,在页面刷新时,参数会消失,用query则不会有这个问题。
这样使用起来很方便,但url会变得很长,而且如果不是使用路由跳转的界面无法使用。
{
path: ':type/:templateType',
name: 'generalAdminOrderFlowAdd',
component: require('@/components/generalAdmin/order/orderFlow')
}
在应用复杂时,推荐使用vue官网推荐的vuex,以下讨论简单SPA中的组件间传值。
import Vue from 'vue'
import Vuex from 'vuex'
import mutations from './mutations'
import actions from './actions'
import getters from './getters' Vue.use(Vuex)
const state = {
order: {} //声明order对象
}
export default new Vuex.Store({
state,
mutations,
actions,
getters
})
export default {
// 声明获取order的方法
getOrder (state) {
return state.order
}
}
export default {
//设置order的值
SET_ORDER (state, order) {
state.order = order
}
this.$store.commit('SET_ORDER', order)// SET_ORDER为order值的设置方法的方法名
// 从vuex中获取order
五.通过$parent,$chlidren等方法调取用层级关系的组件内的数据和方法
通过下面的方法调用: this.$parent.$data.id //获取父元素data中的id this.$children.$data.id //获取父元素data中的id 这样用起来比较灵活,但是容易造成代码耦合性太强,导致维护困难 六、通过eventBus传递数据 使用前可以在全局定义一个eventBus window.eventBus = new Vue(); 在需要传递参数的组件中,定义一个emit发送需要传递的值,键名可以自己定义(可以为对象) eventBus.$emit('eventBusName', id); 在需要接受参数的组件重,用on接受该值(或对象) //val即为传递过来的值 eventBus.$on('eventBusName', function(val) { console.log(val) } ) 最后记住要在beforeDestroy()中关闭这个eventBus eventBus.$off('eventBusName');
vue项目各页面间的传值的更多相关文章
- 使用Block来进行页面间的传值
Block语法 定义Block //定义类型 typedef void (^ReceiveMessageBlock)(NSString *); //申明变量 ReceiveMessageBlock t ...
- HTML5 Web存储 页面间进行传值
在实际使用过程中,经常会遇到需要在页面间进行传值的情况,最初设想一定需要后端才能进行数据的存储和读取,或者在本地使用一个cookie进行保存,直到了解到HTML5 Web存储 使用HTML5的新特性可 ...
- B/S结构中页面间的传值
常见的页面间的传值有session,cookie,application,server.transfer(),queryString,今天主要记录一下server.transfer()的用法. 从A页 ...
- 前端Vue项目——登录页面实现
一.geetest滑动验证 geetest官方文档地址:https://docs.geetest.com/ 产品——极速验证:基于深度学习的人机识别应用.极验「行为验证」是一项可以帮助你的网站与APP ...
- vue项目刷新页面,使数据不丢失(sessionStorage、localStorage、cookie)
vue项目刷新页面时,存储在vuex中的数据会丢失,把他们存到stroage中可以保证不丢失.
- JSP页面间的传值方法总结
JSP 页面间传递参数是项目中经常需要的,这应该算是 web 基本功吧.试着将各种方式总结下来,需要时可以进行权衡利弊选择最合适的方式.下面来一起看看详细的介绍: 1. URL 链接后追加参数 ? 1 ...
- vue程序中组件间的传值方式
vue程序在组件中进行传值有多种方式,这里记录我在项目中使用到的三种: 1. 父组件向子组件传值 2. 子组件向父组件传值 3. 通过路由传参 父组件通过props向子组件传值 在子组件script中 ...
- Vue—非父子组件间的传值(Bus/发布订阅模式/观察者模式/总线)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- UWP开源项目 LLQNotifier 页面间通信利器(移植EventBus)
前言 EventBus是一个Android版本的页面间通信库,这个库让页面间的通信变得十分容易且大幅降低了页面之间的耦合.小弟之前玩Android的时候就用得十分顺手,现在玩uwp就觉得应该在这平台也 ...
随机推荐
- PAT甲级——1101 Quick Sort (快速排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846 1101 Quick Sort (25 分) ...
- POJ1032 Parliament
题目来源:http://poj.org/problem?id=1032 题目大意:给定一个正整数N(5<=N<=1000),将N拆为若干个不同的数使得它们的乘积最大(找到一组互不相等,和为 ...
- 从select机制谈到epoll机制
目录 为什么要用select机制 等待队列 唤醒操作 什么是select机制 关于fd_set select使用 poll函数 为什么select效率较低 什么是epoll epoll机制实现思路 e ...
- 洛谷P3195||bzoj1010 [HNOI2008]玩具装箱TOY
洛谷P3195 bzoj1010 设s数组为C的前缀和 首先$ans_i=min_{j<i}\{ans_j+(i-j-1+s_i-s_j-L)^2\}$ (斜率优化dp)参考(复读)https: ...
- beeline连接hive
beeline -u jdbc:hive2://192.168.1.77:10000 zeppelin default jdbc: jdbc:hive2://nn01.ooccpp.com:2181/ ...
- Java BIO
目录 BIO 字节流 OutputStream InputStream 字符流 Reader Writer 转换流 InputStreamReader OutputStreamWriter BIO I ...
- C#在VS2005开发环境中利用异步模式来对一个方法的执行时间进行超时控制
using System.Threading; using System; namespace ConsoleApplication4 { public class Program { static ...
- mysql数据库的数据变更事件获取以及相关数据
https://medium.com/@Masutangu/udf-trigger%E5%AE%9E%E6%97%B6%E7%9B%91%E6%8E%A7mysql%E6%95%B0%E6%8D%AE ...
- 进程的基础理论、并发(multiprocessing模块)
一.粘包优化方案 之前我们解决粘包的方式是用struct模块来制作一个报头,但是这个解决的方案是有漏洞的,当我们需要传送的文件大于2g时将会报错.所以我们今天将用json来制作报头. from soc ...
- Canny边缘检测学习
Canny边缘检测学习:http://www.open-open.com/lib/view/open1453460512558.html 高斯滤波学习:http://www.cnblogs.com/q ...