既然是使用模块包,那么我们首先要做的就是下载依赖;
Vuex4.x版本:
安装依赖
npm install vuex@next --save
创建store文件:
//解构出来创建store的API
import { createStore } from 'vuex'
//单独写一下store的option
const storeObj = {
state: {
myLoveList: [],
myName: 'zjq',
myRuning: false,
},
mutations: {
addMyLoveList(state, item) {
state.myLoveList.push(item)
},
clearMyLoveList(state) {
state.myLoveList = []
},
changeMyName(state, newName) {
state.myName = newName
},
changeMyRuning(state) {
state.myRuning = !state.myRuning
}
},
actions: {
setTimeoutChangeMyName({ commit }, newName) {
setTimeout(() => { commit('changeMyName', newName) })
}
}
}
//创建store
const store = createStore(storeObj)
export default store
在main中引入饼挂载:
import { createApp } from 'vue'
import App from './App.vue'
// import './index.css'
import 'element-plus/lib/theme-chalk/index.css';
import router from './router'
// import '@css/animate.css'
import 'animate.css' //npm i animate.css
// import { ElButton } from 'element-plus'
let app = createApp(App)
// app.use(ElButton)
import eleP from './assets/js/elementPlus'
eleP(app)
//引入store
import store from './store'
//在app实例上挂载store 链式调用
app.use(router).use(store).mount('#app')
在页面使用:
首先创建一个组件:
<template >
<div class="container">
<ul>
<li>我是vuex中的myLoveList:{
{$store.state.myLoveList}}</li>
<li><el-button type="success" @click="changemyLoveList">改变myLoveList</el-button></li>
<li><el-button type="success" @click="clearmyLoveList">置空myLoveList</el-button></li>
<li>我是vuex中的myName:{
{$store.state.myName}}</li>
<li><el-button type="success" @click="changemyName">改变myName</el-button></li>
<li>我是vuex中的myRuning:{
{$store.state.myRuning}}</li>
<li><el-button type="success" @click="changemyRuning">改变myRuning</el-button></li>
</ul>
</div>
</template>
<script>
//从vuex中解构出来store
import {useStore } from 'vuex'
export default {
setup(props) {
let store = useStore()
let num = 0
const changemyLoveList=()=>{
store.commit('addMyLoveList',num)
num++
}
const clearmyLoveList=()=>{
store.commit('clearMyLoveList')
num = 0
}
let nameList=['周杰伦','刘德华','张学友','陈奕迅','成龙','张学良','秦始皇','周深','王娜']
const changemyName=()=>{
store.dispatch('setTimeoutChangeMyName',nameList[Math.floor(Math.random()*nameList.length)])
}
const changemyRuning=()=>{
store.commit('changeMyRuning')
}
return {changemyLoveList,clearmyLoveList,changemyName,changemyRuning}
}
};
</script>
<style lang="less" scoped>
.container{
ul{
li{
line-height: 60px;
font-size: 30px
}
}
}
</style>
页面调用点击:
同样的mutations中只能是同步方法,actions可以是异步请求,下面我们尝试一下getters这个属性,他是vuex中的计算属性,类似vue组件中的computed:
store/index.js
getters:{
getAll(state){
return String(state.myLoveList) + state.myName +state.myRuning
}
}
vue组件:
<li>{
{$store.getters.getAll}}</li>
页面:
vuex分模块:
store/index.js
import { createStore } from 'vuex'
import home from './home'
const storeObj = {
modules: {
home
}
}
const store = createStore(storeObj)
export default store
store/home.js
export default {
/**
* 定义命名空间,防止多个模块同名共享,使用时需要带上命名空间
*/
namespaced: true,
state: {
myLoveList: [],
myName: 'zjq',
myRuning: false,
},
mutations: {
addMyLoveList(state, item) {
state.myLoveList.push(item)
},
clearMyLoveList(state) {
state.myLoveList = []
},
changeMyName(state, newName) {
state.myName = newName
},
changeMyRuning(state) {
state.myRuning = !state.myRuning
}
},
actions: {
setTimeoutChangeMyName({ commit }, newName) {
setTimeout(() => { commit('changeMyName', newName) })
}
},
getters: {
getAll(state) {
return String(state.myLoveList) + state.myName + state.myRuning
}
}
}
vue页面上使用:
<template >
<div class="container">
<ul>
<li>vuex计算属性 {
{getterAll}}</li>
<li>我是vuex中的myLoveList:{
{$store.state.home.myLoveList}}</li>
<li>
<el-button type="success" @click="changemyLoveList">改变myLoveList</el-button>
</li>
<li>
<el-button type="success" @click="clearmyLoveList">置空myLoveList</el-button>
</li>
<li>我是vuex中的myName:{
{$store.state.home.myName}}</li>
<li>
<el-button type="success" @click="changemyName">改变myName</el-button>
</li>
<li>我是vuex中的myRuning:{
{$store.state.home.myRuning}}</li>
<li>
<el-button type="success" @click="changemyRuning">改变myRuning</el-button>
</li>
<li>{
{$store.getters.getAll}}</li>
</ul>
</div>
</template>
<script>
import { computed } from "vue";
//从vuex中解构出来store
import { useStore } from "vuex";
export default {
setup(props) {
let store = useStore();
let getterAll = computed(() => store.getters["home/getAll"]);
let num = 0;
const changemyLoveList = () => {
store.commit("home/addMyLoveList", num);
num++;
console.log(num);
};
changemyLoveList();
const clearmyLoveList = () => {
store.commit("home/clearMyLoveList");
num = 0;
console.log(store);
};
clearmyLoveList();
let nameList = [
"周杰伦",
"刘德华",
"张学友",
"陈奕迅",
"成龙",
"张学良",
"秦始皇",
"周深",
"王娜"
];
const changemyName = () => {
store.dispatch(
"home/setTimeoutChangeMyName",
nameList[Math.floor(Math.random() * nameList.length)]
);
};
changemyName();
const changemyRuning = () => {
store.commit("home/changeMyRuning");
};
changemyRuning();
return {
changemyLoveList,
clearmyLoveList,
changemyName,
changemyRuning,
getterAll
};
}
};
</script>
<style lang="less" scoped>
.container {
ul {
li {
line-height: 60px;
font-size: 30px;
}
}
}
</style>
页面: