从0开始探究vue-公共变量的管理
背景
在Vue项目中,我们总会遇到一些公共数据的处理,如方法拦截,全局变量等,本文旨在解决这些问题
解决方案
事件总线
所谓事件总线,就是在当前的Vue实例之外,再创建一个Vue实例来专门进行变量传递,事件处理,管理回调事件等
//main.js中
Vue.prototype.$bus = new Vue();
new Vue({...})
//页面一
this.$bus.$on('sayName',(e)=>{
alert('我的名字是',e)
})
//页面二
this.$bus.$emit('sayName','小明');//我的名字是 小明
原型挂载
所谓原型挂载,就是在main.js中将公共变量,事件,都挂在到Vue原型上
//main.js
Vue.prototype.$globalData = {}
Vue.prototype.$sayName = function(e){
console.log('我的名字是',e)
}
new Vue({...})
//组件一
Vue.prototype.$globalData.name='小明';
this.$sayName('小王');//我的名字是小王
//组件二
console.log(this.$sayName.name);//小明
this.$sayName('小王');//我的名字是小王
vuex
Vuex是Vue提供的一种,专门用来管理vue中的公共状态,事件等等,以应用登录为例
//新建store.js
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)
export default new Vuex.Store({
state: {//此处为公共变量
userId:"",//用户Id
loginSession:""//用户登录凭证
},
mutations: {//此处为同步方法
setLoginSession(state,loginSession){//存入state中的用户凭证
state.loginSession = loginSession;
},
setUserId(state,loginSession){//存入state中的用户凭证
state.loginSession = 'user_'+Math.floor(Math.random()*100000000000);
}
},
actions: {//此处为异步方法
getUserId({state,commit},options={}){//从服务器取登录凭证,然后返回是否登录状态
return new Proise((resolve)=>{//返回一个promise对象,来让调用者可以使用.then来进行下一步操作
axios.get('api').then((res)=>{
commit('setLoginSession',res.data.loginSession)
resolve(this.getters.isLogin)
})
}))
}
},
modules: {//此处为混入更多的vuex小模块
},
gatters: {//此处为计算变量
isLogin(){
return (this.userId&&this.loginSession)?true:false
}
}
})
//main.js中注入vuex
import store from './store/store.js'
Vue.prototype.$store = store;
//app.vue中
export default {
data(){
return {}
},
mounted(){
this.$store.commit('setUserId');//设置用户Id
this.$store.dispatch('getUserId').then((result)=>{//查询登录凭证,并返回登录结果
console.log(this.$store.getters.isLogin,result);//true true 此处也可以查看计算属性中的是否登录状态
if(result){
alert('登录了')
}else{
alert('未登录')
}
});
}
}
从0开始探究vue-公共变量的管理的更多相关文章
- 从0开始探究vue-组件化-组件之间传值
理解 Vue中有个非常重要的核心思想,就是组件化,组件化是为了代码复用 什么是组件化 组件化,就像一个电脑主机里的主板,有内存条的插口,有硬盘,光驱等等的插口,我们的项目,就像一个电脑主机,通过各种组 ...
- 如何在vue中全局引入stylus文件的公共变量
新建 一个公共的stylus公共文件添加公共变量,如下: 修改下图圈出的文件: 具体的修改如下: // generate loader string to be used with extract t ...
- 随机跳转页面之使用VBA公共变量
p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...
- C# 引用类型公共变量的影响
public int[] a =new int[2]; private void button1_Click(object sender, EventArgs e) { bing(a); } priv ...
- 从0开始搭建vue+webpack脚手架(二)
接着从0开始搭建vue+webpack脚手架(一) 三.配置webpack-dev-server 1. webpack-dev-server自带一个node的服务器, 项目在服务端运行的同时可以实现热 ...
- m_Orchestrate learning system---三十三、公共变量多弄成全局变量
m_Orchestrate learning system---三十三.公共变量多弄成全局变量 一.总结 一句话总结:比如班级id,小组id,这样省事,而且减少数据库的访问,加快访问速度,而且节约代码 ...
- 最齐全的vue公共函数给你们放出来啦
import Vue from 'vue' /* 配置参数 */Vue.prototype.winH = document.documentElement.clientHeight; Vue.prot ...
- less的引用及公共变量的抽离
一.什么是less? less是什么自然不用多言,乃一个css预编译器,可以扩展css语言,添加功能如如允许变量(variables),混合(mixins),函数(functions) 和许多其他的技 ...
- C#-WebForm-★内置对象简介★Request-获取请求对象、Response相应请求对象、Session全局变量(私有)、Cookie全局变量(私有)、Application全局公共变量、ViewState
内置对象: 1.Request - 获取请求对象 用法:接收传值 protected void Page_Load(object sender, EventArgs e) { TextBox1.Tex ...
随机推荐
- 关于tablayout+viewpager+fragment配合使用的一点记录
最近在写项目的时候遇到要求使用tablayout和fragment,遇到了这里记录一下大致思路. tablayout是头部可以左右切换的头部控制栏控件,配合viewpager使用,fragment是碎 ...
- js中刷新页面的方式总结
1.window.onload / document.onload 2.history.go(num): (1)num为参数,num为正表示前进几个页面,类似于history.forward(): ( ...
- 【Hadoop离线基础总结】linux的shell编程
linux的shell编程 基本了解 概述 Shell是一个用C语言编写的程序,通过shell用户可以访问操作系统内核服务,它类似于DOS下的command和后来的cmd.exe.Shell既是一种命 ...
- 第一行Kotlin系列(三)Intent 向上一页返回数据onActivityResult的使用
1.MainActivity.kt跳转处理 声明全局的按钮对象 private lateinit var button8: Button 实例化按钮对象 button8 = findViewById( ...
- SORM框架01
架构图 Query接口:负责查询(对外提供的核心服务类) QueryFactory类:负责根据配置信息创建Query对象 TypeConvertor接口:类型转换 TableContext类:负责获取 ...
- 设计模式之GOF23代理模式03
动态代理 public class StarHandler implements InvocationHandler{ Star realStar; public StarHandler(Star ...
- JDBC04 PreparedStatement
PreparedStatement类 存在预编译,用占位符去填参数(参数索引从1开始算),可以防止SQL注入 try { Class.forName("com.mysql.cj.jdbc.D ...
- LiteAI四大技术"杀手锏",解锁物联网智能设备AI开发难关
你知道我们生活中常见的物联网智能设备融合AI技术后,会给我们带来什么样的智能交互体验?在我们指尖触碰的那一刹那背后隐藏的代码世界又是怎么样的呢? 今天就来和大家说说IoT智能设备轻松实现AI的奥秘! ...
- select嵌套问题
关于sql语句: SELECT COUNT(ID) FROM dbo.N_Order_BusinessBatch WHERE Mobile='15210235082' And CreateTime=( ...
- HTML学习——day1
HTML是一种用于创建网页的标准标记语 注意:对于中文网页需要使用<meta charset=''utf-8''>声明编码,否则会出现乱码. HTML标签 <标签>内容< ...