状态管理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

相关推荐

  1. Vue状态管理Vux

    2024-06-12 17:12:03       37 阅读
  2. vue状态管理

    2024-06-12 17:12:03       20 阅读
  3. Vue状态管理pinia

    2024-06-12 17:12:03       9 阅读
  4. vuex状态管理的使用

    2024-06-12 17:12:03       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-12 17:12:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-12 17:12:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-12 17:12:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-12 17:12:03       18 阅读

热门阅读

  1. Spring Boot 面试热点(一)

    2024-06-12 17:12:03       8 阅读
  2. 初识Docker

    2024-06-12 17:12:03       5 阅读
  3. 记录一个apisix修改后台接口超时时间的方法

    2024-06-12 17:12:03       6 阅读
  4. YOLOv10改进|采用ADown降采样模块有效融合

    2024-06-12 17:12:03       4 阅读
  5. YOLO v5与YOLO v8框图比较

    2024-06-12 17:12:03       9 阅读
  6. 2024年,计算机相关专业还值得选择吗?

    2024-06-12 17:12:03       8 阅读
  7. 22.正则化

    2024-06-12 17:12:03       3 阅读