官网:Vuex 是什么? | Vuex (vuejs.org)https://v3.vuex.vuejs.org/zh/
创建一个vue2的新项目名为vuex-demo,安装命令 npm install vuex@3
新建index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment (state) {
state.count++
}
}
})
export default store
修改main.js,注入
import Vue from 'vue'
import App from './App.vue'
import store from './store'
Vue.config.productionTip = false
new Vue({
render: h => h(App),
store: store
}).$mount('#app')
HelloWorld.vue修改
<template>
<div class="hello">
{{ this.$store.state.count }}
<button @click="add">+1</button>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
methods: {
add() {
this.$store.commit('increment') // commit mutation
}
}
}
</script>
<style scoped>
</style>
App.vue修改
<template>
<div id="app">
<HelloWorld/>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'App',
components: {
HelloWorld
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
运行
修改HelloWorld.vue
<template>
<div class="hello">
<!-- {{ this.$store.state.count }} -->
{{ count }}
<button @click="add">+1</button>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
computed: {
count() {
return this.$store.state.count // access state
}
},
methods: {
add() {
this.$store.commit('increment') // commit mutation
}
}
}
</script>
<style scoped>
</style>
刷新,效果不变
修改HelloWorld.vue,效果还是不变
<template>
<div class="hello">
<!-- {{ this.$store.state.count }} -->
{{ count }}
<button @click="add">+1</button>
</div>
</template>
<script>
import { mapState } from 'vuex';
export default {
name: 'HelloWorld',
computed:mapState(['count']),
// computed: {
// count() {
// return this.$store.state.count // access state
// }
// },
methods: {
add() {
this.$store.commit('increment') // commit mutation
}
}
}
</script>
<style scoped>
</style>
修改index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0,
todos: [
{ id: 1, text: '吃饭', done: true },
{ id: 2, text: '睡觉', done: false }
]
},
mutations: {
increment (state) {
state.count++
}
}
})
export default store
修改HelloWorld.vue
<template>
<div class="hello">
<!-- {{ this.$store.state.count }} -->
{{ count }}
<button @click="add">+1</button>
<ul>
<li v-for="todo in todos" :key="todo.id">{{ todo.text }}</li>
</ul>
</div>
</template>
<script>
import { mapState } from 'vuex';
export default {
name: 'HelloWorld',
computed:mapState(['count','todos']),
methods: {
add() {
this.$store.commit('increment') // commit mutation
}
}
}
</script>
<style scoped>
</style>
修改
修改index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0,
todos: [
{ id: 1, text: '吃饭', done: true },
{ id: 2, text: '睡觉', done: false }
]
},
mutations: {
increment (state) {
state.count++
}
},
getters: {
doneTodos: state => {
return state.todos.filter(todo => todo.done)
}
}
})
export default store
修改HelloWorld.vue
<template>
<div class="hello">
<!-- {{ this.$store.state.count }} -->
{{ count }}
<button @click="add">+1</button>
<ul>
<li v-for="todo in doneTodos" :key="todo.id">{{ todo.text }}</li>
</ul>
</div>
</template>
<script>
import { mapState,mapGetters} from 'vuex';
export default {
name: 'HelloWorld',
computed:{
...mapState(['count','todos']),
...mapGetters(['doneTodos'])
methods: {
add() {
this.$store.commit('increment') // commit mutation
}
}
}
</script>
<style scoped>
</style>
刷新
修改index.js,每次加2
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0,
todos: [
{ id: 1, text: '吃饭', done: true },
{ id: 2, text: '睡觉', done: false }
]
},
mutations: {
increment (state,n) {
state.count += n
}
},
getters: {
doneTodos: state => {
return state.todos.filter(todo => todo.done)
}
}
})
export default store
修改HelloWorld.vue
<template>
<div class="hello">
<!-- {{ this.$store.state.count }} -->
{{ count }}
<button @click="add">+1</button>
<ul>
<li v-for="todo in doneTodos" :key="todo.id">{{ todo.text }}</li>
</ul>
</div>
</template>
<script>
import { mapState,mapGetters} from 'vuex';
export default {
name: 'HelloWorld',
// computed:mapState(['count','todos']),
computed:{
...mapState(['count','todos']),
...mapGetters(['doneTodos'])
},
// computed: {
// count() {
// return this.$store.state.count // access state
// }
// },
methods: {
add() {
this.$store.commit('increment',2) // commit mutation
}
}
}
</script>
<style scoped>
</style>
刷新,每次加2