文章目录
1.5vuex的核心概念:store、state、getters、mutations、actions
1)加载依赖 npm install vuex -S 下载Vuex最新版本的依赖
小编使用的是vuex3.6版本,下载命令npm i -S vuex@3.6.2
2)导入Vuex的4个核心组件,然后通过index.js加载进来
3)将Vuex对应的index.js 挂载到 main.js 的vue实例中
一、VueX ?
1.1Vuex是什么:
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库),让其在各个页面上实现数据的共享包括状态,并且可操作
1.2Vuex在编码中起到什么作用:
解决了前端组件传参的问题,针对当前项目所有的变量进行统一管理。
相比于总线传参优点在于,能够将整个项目的变量进行统一管理
变量传参的演变形式图:
1.3组件传参的方式目前了解到总共有三种:
1、子传父 $emit 父传子 props
父组件-->子组件,通过子组件的自定义属性:props
子组件-->父组件,通过自定义事件:this.$emit('事件名',参数1,参数2,...);
非父子组件或父子组件:
通过数据总数Bus,this.$root.$emit('事件名',参数1,参数2,...);
更好的方式是在vue中使用vuex
2、总线传参
3、VueX核心组件
VueX官方图解:
个人图解:
1.4Vuex分成五个部分(核心组件):
1.State.js:单一状态树
获取值 this.$store.state.变量值
2.Getters.js:状态获取
获取值 this.$store.getters.变量值的get方法
3.Mutations.js:触发同步事件
获取值 this.$store.commit("同步的方法",{...(载荷)})
4.Actions.js:提交mutation,可以包含异步操作
获取值 this.$store.dispatch("异步的方法",{...(载荷)})
5.Module.js:将vuex进行分模块
在vue根实例中定义变量,这个变量也是vue实例,vuex中默认情况下是不允许使用vue实例,想要请求后台接口,必须将vue实例当成参数从组件那一方传递过来
1.5vuex的核心概念:store、state、getters、mutations、actions
store
每一个Vuex应用的核心就是store(仓库),store基本上就是一个容器,它包含着你的应用中大部分的状态 (state)
const store = new Vuex.Store({
state, // 共同维护的一个状态,state里面可以是很多个全局状态
getters, // 获取数据并渲染
actions, // 数据的异步操作
mutations // 处理数据的唯一途径,state的改变或赋值只能在这里
})
二、VueX版本问题及store.js的使用
2.1Vuex使用步骤
1)加载依赖 npm install vuex -S 下载Vuex最新版本的依赖
小编使用的是vuex3.6版本,下载命令npm i -S vuex@3.6.2
2)导入Vuex的4个核心组件,然后通过index.js加载进来
import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import getters from './getters'
import actions from './actions'
import mutations from './mutations'
Vue.use(Vuex)
const store = new Vuex.Store({
state,
getters,
actions,
mutations
})
export default store
3)将Vuex对应的index.js 挂载到 main.js 的vue实例中
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
//开发环境下才会引入mockjs
/* process.env.MOCK && require('@/mock') */
/* 避免后期打包样式不同,要放在import App from './App'; 之前 */
import ElementUI from 'element-ui' // 新添加 1导入js依赖
import 'element-ui/lib/theme-chalk/index.css' // 新添加 2导入js样式
import App from './App'
import router from './router'
import axios from '@/api/http'//#vue项目对axios的全局配置
import VueAxios from 'vue-axios'
import store from './store'
Vue.use(ElementUI) // 新添加 3 使用elementui
Vue.use(VueAxios,axios);
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
data(){
return{
//在vue根实例中定义一个变量,这个变量就是vue实例,它的总线
//props this.$emit
Bus:new Vue({})
}
},
components: { App },
template: '<App/>'
})
4)测试Vuex的存储变量的功能
<template>
<div>
<p>欢迎来到{{msg}}</p>
</div>
</template>
<script>
export default {
name: 'VuexPage1',
data () {
return {
}
},
computed:{
msg(){
//从vuex的state文件中获取值
return this.$store.state.resName;
}
}
}
</script>
<style>
</style>
在 index.js / src/router中导入一下
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from "../components/HelloWorld";
import AppMain from "../components/AppMain";
import LeftNav from "../components/LeftNav";
import TopNav from "../components/TopNav";
import Login from "../views/Login";
import Reg from "../views/Reg";
import Articles from "../views/sys/Articles";
import VuexPage1 from "../views/sys/VuexPage1";
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'Login',
component: Login
},
{
path: '/Login',
name: 'Login',
component: Login
},
{
path: '/Reg',
name: 'Reg',
component: Reg
},
{
path: '/AppMain',
name: 'AppMain',
component: AppMain,
children:[
{
path: '/LeftNav',
name: 'LeftNav',
component: LeftNav
},
{
path: '/TopNav',
name: 'TopNav',
component: TopNav
},
{
path: '/sys/Articles',
name: 'Articles',
component: Articles
},
{
path: '/sys/VuexPage1',
name: 'VuexPage1',
component: VuexPage1
}
]
}
]
})
启动一下项目,npm run dev
三、Vuex中的设置及获取变量值
export default{
setResName:(state,payload)=>{
//state对象就对应了state.js中的对象
//payload载荷,对应传递的JSON对象参数{name:zs,age:}
state.resName=payload.resName;
}
}
export default {
getResName: (state) => {
return state.resName;
}
}
VuexPage1:
<template>
<div>
<p>页面1:欢迎来到{{msg}}</p>
<button @click="buy">盘它</button>
<button @click="buyAsync">最终的店长</button>
</div>
</template>
<script>
export default {
name: 'VuexPage1',
data() {
return {}
},
methods: {
buy() {
//通过commit方法 会 调用 mutations.js文件中定义好的方法
this.$store.commit("setResName", {
resName: 'KFC'
})
}
},
computed: {
msg() {
// 从vuex的state文件中获取值
// return this.$store.state.resName;——>不推荐,不安全
// 通过getters.js文件获取state.js中定义的变量值
return this.$store.getters.getResName;
}
}
}
</script>
<style scoped>
</style>
VuexPage2:
<template>
<div>
<p>页面2:欢迎来到{{msg}}</p>
</div>
</template>
<script>
export default {
name: 'VuexPage2',
data() {
return {}
},
computed: {
msg() {
// 从vuex的state文件中获取值
// return this.$store.state.resName;——>不推荐,不安全
// 通过getters.js文件获取state.js中定义的变量值
return this.$store.getters.getResName;
}
}
}
</script>
<style scoped>
</style>
在 index.js / src/router中导入一下VuexPage2:
当我们点击盘它,VuexPage2的内容也跟着VuexPage1的内容改变
四、Vuex中的异步同步操作
VuexPage1:
<template>
<div>
<p>页面1:欢迎来到{{msg}}</p>
<button @click="buy">盘它</button>
<button @click="buyAsync">最终的店长</button>
</div>
</template>
<script>
export default {
name: 'VuexPage1',
data() {
return {}
},
methods: {
buy() {
//通过commit方法 会 调用 mutations.js文件中定义好的方法
this.$store.commit("setResName", {
resName: 'KFC'
})
},
buyAsync() {
this.$store.dispatch("setResNameAsync", {
resName: '麦当劳'
})
}
},
computed: {
msg() {
// 从vuex的state文件中获取值
// return this.$store.state.resName;——>不推荐,不安全
// 通过getters.js文件获取state.js中定义的变量值
return this.$store.getters.getResName;
}
}
}
</script>
<style scoped>
</style>
actions.js:
export default {
setResNameAsync:(context,payload)=>{
// 异步修改值 在异步方法中调用了同步方法
// context指的是Vuex的上下文,相当于 this.$store
// 死代码,6秒后执行
setTimeout(function (){
context.commit("setResName",payload);
},6000);
}
}
五、Vuex后台交互
actions.js:
export default {
setResNameAsync:(context,payload)=>{
// 异步修改值 在异步方法中调用了同步方法
// context指的是Vuex的上下文,相当于 this.$store
// 死代码,6秒后执行
setTimeout(function (){
context.commit("setResName",payload);
},6000);
let _this=payload._this;
let url=_this.axios.urls.SYSTEM_MENU_TREE;
_this.axios.post(url,{}).then(r=>{
console.log(r);
}).catch(e=>{
});
}
}
在VuexPage1,添加交互代码
👀👀👀👀👀
后台数据交互成功
总结:
vuex使用步骤:下载 命令:npm install vuex -S(下载最新版本)
四个核心组件:state.js:存放变量的容器
getters.js:获取变量值
mutations.js:同步的方法进行修改
actions.js:异步的方法进行修改
index.js:整合四个组件,作为vuex的入口