zoukankan      html  css  js  c++  java
  • Vue3 Vuex

    一、  安装

    npm install vuex@next --save

    二、 基本使用

    store.js

    import { createStore } from 'vuex'
    
    const store = createStore({
        //用来保存数据
        state() {
            return {
                count: 2
            }
        },
        //用来改变共享的数据
        mutations: {
            increment(state) {
                state.count++
            }
        }
    })
    
    export default store

    注册

    import { createApp } from 'vue'
    import App from './App.vue'
    import router from './router/index.js'
    import store from './store/store'
    
    createApp(App).use(router).use(store).mount('#app')

    使用

    <template>
    home
    {{$store.state.count}}
    <br/>
    <button @click='add'>按钮</button>
    </template>
    
    <script>
    export default {
       methods:{
           add(){
            this.$store.commit('increment')
           }
       }
    }
    </script>

    在改变值的时候要通过mutation里的公共方法,而不是直接给store.state.count赋值。

     使用计算属性

    <script>
    export default {
        computed: {
            count() {
                return this.$store.state.count
            }
        },
    }
    </script>

    mapState 辅助函数

    ??

    Getter

    Mutation

    Action

    Module

     

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • 相关阅读:
    mysql6.0免安装配置
    SQL2005安装时“性能监视器计数器要求(错误)”解决办法
    Centos 下解压和压缩rar文件
    MyEclipse插件介绍与下载
    算法设计之递归法
    Centos5.2 下安装MySQL+Tomcat6 及设置自启动
    科学用电脑
    浅谈Linux的安全设置
    JAVA程序员之路
    CentOS 5.5 挂载Windows NTFS 文件系统
  • 原文地址:https://www.cnblogs.com/buchizaodian/p/14926508.html
Copyright © 2011-2022 走看看