状态管理Vuex

官网:Vuex 是什么? | Vuex (vuejs.org)icon-default.png?t=N7T8https://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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值