小白都能看懂的vue中各种通信传值方式,附带详细代码
1、路由通信传值
- 路由通信是通过路由跳转用query把参数带过去,也是vue常用的通信手段。
例子:
- 创建并在路由注册一个组件Head
<template>
<div id="head">
<button @click="handleChange">clickMe</button> //给按钮绑定点击事件
</div>
</template>
<script>
export default {
name: 'Head',
data () {
return {
}
},
mounted(){
},
updated(){
},
methods:{
handleChange(){
this.$router.push({ path:"/about" , query:{ text:"我是阿格斯之盾" } }) //路由跳转,并用query带过去参数
}
}
}
</script>
<style scoped>
</style>
- 创建另一个组件About并在路由注册
<template>
<div id="about">
<p>我是关于页:{{ message }}</p><button type="button" @click="handleChange">回到首页</button> //显示接收过来的数据
</div>
</template>
<script>
export default {
name: 'About',
data () {
return {
message: ""
}
},
mounted(){
this.message = this.$route.query.text //在生命周期中接收传过来的数据
},
updated(){
},
methods:{
handleChange(){
this.$router.push({ path: "/" }) //点击返回首页
}
}
}
</script>
<style scoped>
</style>
- 路由注册的简单代码
import Vue from 'vue'
import Router from 'vue-router'
import Head from '@/components/Head'
import About from '@/components/About'
Vue.use(Router)
export default new Router({
mode: "history",
routes: [
{
path: '/',
name: 'Head',
component: Head
},{
path: '/about',
name: 'About',
component: About
}
]
})
2、sessionStorage本地缓存通信
- 还是列举上面的例子,将它们稍微改一改就可以了,路由配置都一样的。sessionStorage的特点就是浏览器关掉缓存就会消失,这是它区别于localStorage的。
例子:
- Heade代码:
<template>
<div id="head">
<button @click="handleChange">clickMe</button>
</div>
</template>
<script>
export default {
name: 'Head',
data () {
return {
}
},
updated(){
},
methods:{
handleChange(){
this.$router.push({ path:"/about"})
},
message(){
var message = "我是阿格斯之盾"
sessionStorage.setItem('text', message) //创建缓存
}
},
mounted(){
this.message();
}
}
</script>
<style scoped>
</style>
- About代码:
<template>
<div id="about">
<p>我是关于页:{{ message }}</p><button type="button" @click="handleChange">回到首页</button>
</div>
</template>
<script>
export default {
name: 'About',
data () {
return {
message: ""
}
},
mounted(){
this.message = sessionStorage.getItem("text") //读取缓存
},
updated(){
},
methods:{
handleChange(){
this.$router.push({ path: "/" })
}
}
}
</script>
<style scoped>
</style>
3、父组件向子组件通信
- 定义父组件Head,还是用上面的例子,父组件传递一句话给子组件,如果传递的参数很多,可使用json数组{}的形式。
例子:
- Head父组件代码
<template>
<div id="head">
<About :text=message></About> //将message参数传给子组件
</div>
</template>
<script>
import About from '@/components/About.vue'
export default {
name: 'Head',
components:{
About
},
data () {
return {
message : "我是阿格斯之盾"
}
},
mounted(){
},
methods:{
}
}
</script>
<style scoped>
</style>
- About子组件代码
<template>
<div id="about">
<p>我是关于页:{{ text }}</p>
</div>
</template>
<script>
export default {
name: 'About',
props:{
'text':[] //子组件接受数据,[]里面可以写传入类型,如果不符合会报错
},
data () {
return {
message: ""
}
},
mounted(){
},
updated(){
},
methods:{
handleChange(){
}
}
}
</script>
<style scoped>
</style>
4、子组件向父组件通信
- 子组件向父组件通信是通过emit事件发送的,话不多说,直接上案例,还是利用上面的案例稍作修改
- About子组件代码:
<template>
<div id="about">
<button @click="handleChange">点击发送消息给父组件</button>
</div>
</template>
<script>
export default {
name: 'About',
props:{
'text':[]
},
data () {
return {
message: ""
}
},
mounted(){
},
updated(){
},
methods:{
handleChange(){
this.$emit( "child-message" , "我是阿格斯之盾" ) //提交信息
}
}
}
</script>
<style scoped>
</style>
- Head父组件代码
<template>
<div id="head">
<About @child-message = "handleText"></About> //这里传过来父组件需要用一个方法接住
<p>来自子组件的消息:{{message}}</p>
</div>
</template>
<script>
import About from '@/components/About.vue'
export default {
name: 'Head',
components:{
About
},
data () {
return {
message : ""
}
},
mounted(){
},
methods:{
handleText(data){ //这里的data就是子组件传过来的内容
this.message = data
}
}
}
</script>
<style scoped>
</style>
5、vuex状态管理
- 状态管理使用起来相对复杂,但是对于大型项目确实非常实用的。
(1)安装vuex,并建立仓库文件
npm install vuex -s
- 安装过后在src文件中创建store文件夹,并建立index.js文件,index.js的代码如下:
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
message: '我是阿格斯之盾'
},
mutations: {
MESSAGE_INFO (state,view) {
state.message = view;
}
}
})
export default store
(2)在min.js中注册store仓库
- 代码如下:
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
(3)状态的读取和提交
- 还是使用上面的案例,我们以子组件About提交改变状态,父组件Head接受状态并显示出来
- 下面是About组件提交状态
<template>
<div id="about">
<button @click="handleChange">点击发送消息给父组件</button>
</div>
</template>
<script>
export default {
name: 'About',
props:{
'text':[]
},
data () {
return {
message: ""
}
},
mounted(){
},
updated(){
},
methods:{
handleChange(){
this.$store.commit("MESSAGE_INFO" , "我是火车王") //提交改变状态
}
}
}
</script>
<style scoped>
</style>
- Head组件接受状态:
<template>
<div id="head">
<About></About>
<p>来自子组件的消息:{{this.$store.state.message}}</p> //直接使用this.$store.state.message接受数据显示
</div>
</template>
<script>
import About from '@/components/About.vue'
export default {
name: 'Head',
components:{
About
},
data () {
return {
message : ""
}
},
mounted(){
},
methods:{
}
}
</script>
<style scoped>
</style>
- 总结:以上就是vue中的通信方式,当然还有一些,比如说eventBus什么的,适用于中小型项目,但是我用的比较少,一般上面的几种在开发中已经够用的,例子很简单,学习是永无止境的,具体更深的东西还得下功夫去研读官网或者其他资料,本文中有不对的地方或者疑惑的地方,还望大家多多指教!谢谢。
小白都能看懂的vue中各种通信传值方式,附带详细代码的更多相关文章
- 搭建分布式事务组件 seata 的Server 端和Client 端详解(小白都能看懂)
一,server 端的存储模式为:Server 端 存 储 模 式 (store-mode) 支 持 三 种 : file: ( 默 认 ) 单 机 模 式 , 全 局 事 务 会 话 信 息 内 存 ...
- 小白都能看懂的tcp三次握手
众所周知,TCP在建立连接时需要经过三次握手.许多初学者经常对这个过程感到混乱:SYN是干什么的,怎么一会儿是1一会儿是0?怎么既有大写的ACK又有小写的ack?为什么ACK在第二次握手才开始出现?初 ...
- 小白都能看懂的Spring源码揭秘之IOC容器源码分析
目录 前言 IOC 只是一个 Map 集合 IOC 三大核心接口 IOC 初始化三大步骤 定位 加载 注册 总结 前言 在 Spring 框架中,大家耳熟能详的无非就是 IOC,DI,Spring M ...
- 小白都能看懂的 Spring 源码揭秘之依赖注入(DI)源码分析
目录 前言 依赖注入的入口方法 依赖注入流程分析 AbstractBeanFactory#getBean AbstractBeanFactory#doGetBean AbstractAutowireC ...
- log4j漏洞的产生原因和解决方案,小白都能看懂!!!!
核弹级bug Log4j,相信很多人都有所耳闻了,这两天很多读者都在问我关于这个bug的原理等一些问题,今天咱们就专门写一篇文章,一起聊一聊这个核弹级别的bug的产生原理以及怎么防止 产生原因 其实这 ...
- Spring Cloud Alibaba分布式事务组件 seata 详解(小白都能看懂)
一,什么是事务(本地事务)? 指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行. 简单的说,事务就是并发控制的单位,是用户定义的一个操作序列. 而一个逻辑工作单元要成 ...
- 小白都能看懂的 Spring 源码揭秘之Spring MVC
目录 前言 Spring MVC 请求流程 Spring MVC 两大阶段 初始化 HttpServletBean#init() FrameworkServlet#initServletBean Fr ...
- gitbook 入门教程之小白都能看懂的 Gitbook 插件开发全流程
什么是插件 Gitbook 插件是扩展 GitBook 功能(电子书和网站)的最佳方式. 只要是 Gitbook 默认没有提供的功能,基于插件机制都可以自行扩展,是插件让 Gitbook 变得更加强大 ...
- Unity 打包发布Android新手教学 (小白都能看懂的教学 ) [转]
版权声明:本文为Aries原创文章,转载请标明出处.如有不足之处欢迎提出意见或建议,联系QQ531193915 扫码关注微信公众号,获取最新资源 最近在Unity的有些交流群里,发现好多Unity开发 ...
随机推荐
- The Open Source Business Model is Under Siege
https://www.influxdata.com/blog/the-open-source-database-business-model-is-under-siege/ A few weeks ...
- application内置对象
application 实现用户间的数据共享,可存放全局变量 setAttribute() getAttribute() getServerInfo(); //获取引擎名和版本号,如:Apache T ...
- 【游记】CSP2019 垫底记
考试时候的我: Day 1 做完 \(T1\) 和 \(T2\),还有 \(2.5 h\),我想阿克 \(Day1\).(\(T3\):不,你不想) 不过一会就想出来给每个点 dfs 贪心选一个点,然 ...
- 项目集成Spring Security
前言 之前写的 涂涂影院管理系统 这个 demo 是基于 shiro 来鉴权的,项目前后端分离后,显然集成 Spring Security 更加方便一些,毕竟,都用 Spring 了,权限管理当然 S ...
- 悲伤的 C++ throw(…)
当在C++语言中引入异常时,引入了相应的throw(…)动态异常说明符,注释了哪些异常可以由函数抛出.比如: // this function might throw an integer or a ...
- mariadb启动不了
提示地址已经被使用,是否有其他的进程正在使用 /var/run/sdata/mysql.sock 查询该文件,发现没有,sdata目录都不存在,应该是上次mysql意外关闭导致这个目录丢失了, 使用r ...
- Maven中使用<version>LATEST</version>自动依赖最新版本引发的问题
今天在打包项目的过程中出现了编译问题,奇怪的是这个项目已经好久没有修改过了,报错如下. 找不到符号 [ERROR] 符号: 方法 intent(java.lang.String) [ERROR] 位置 ...
- 3ds Max学习日记(十一)——如何给模型上贴图
参考链接:https://jingyan.baidu.com/article/e4511cf38a810b2b845eaf1f.html 之前一直都不知道怎么在3dsMax里给模型上材质和贴图,被 ...
- AnonymousPipeStream的使用案例
AnonymousPipeStream的使用具体案例如下: 服务端: using System; using System.Data; using System.Data.SQLite; using ...
- [E2E_L7 51CTO]进一步解析OpenVINO提供的例子并且独立出来(win+vs)
一.例子概览 上图中标红的都是可以运行的例子,在上一个博客中已经提示.其它的是工具等辅助内容. 例子可以简单分为3类,一类是 这个是和OpenCV相关的,可以参考: 一类是 这个是入门的,优先学习 余 ...