vuex 注册 + 更改值 + 提交载荷 + Action(逻辑层) + 派生属性 getter + module 模块化

news/2024/7/10 2:50:22 标签: vue

vuex_0">安装依赖 npm install --save vuex

  1. 每一个 Vuex 项目的核心就是 store(仓库)。 store 就是一个对象,它包含着你的项目中大部分的状态
    (state)。
  2. state 是 store 对象中的一个选项,是 Vuex 管理的状态对象(共享的数据属性)
    建立vuex文件

vuex 它虽然在切换路由的时候可以获取到状态值, 但是如果刷新了就没有了
可以结合 loacl storage 或 session使用
(seesion 关闭浏览器清空)

loacl storage 除非主动清空 不然不会失效
在这里插入图片描述

注册

在这里插入图片描述

使用全局 和 通过点击更改全局

在这里插入图片描述


提交载荷


在这里插入图片描述

在这里插入图片描述

Action

Action 类似于 mutation,不同在于:
Action 提交的是 mutation,而不是在组件中直接变更状态, 通过它间接更新 state。
在组件中通过 this.$store.dispatch(‘actionName’) 触发状态值间接改变
Action 也支持载荷
Action 可以包含任意异步操作。
不建议直接更改mutation 最好在action中写逻辑之后再mutation改变
相当于是逻辑层
在这里插入图片描述
// 调用方法
在这里插入图片描述

派生属性 getter

有时候我们需要从 store 中的 state 中派生出一些状态。
例如:基于上面代码,增加一个 desc 属性,当 count 值小于 50,则 desc 值为 吃饭 ; 大于等于 50 小于
100,则desc 值为 睡觉 ; 大于100 , 则 desc 值为 打豆豆 。这时我们就需要用到 getter 为我们解决。
getter 其实就类似于计算属性(get)的对象.
组件中读取 $store.getters.xxx

在这里插入图片描述
//获取
在这里插入图片描述

以module模块化项目结构 (便于管理)可以把home抽取出来文件 下面会展示

import Vue from "vue";
import Vuex from "vuex";

// //引入vuex插件
Vue.use(Vuex);

// 以module模块化项目结构
// 可以把home抽取出来文件 
const home = {
    //定义全局变量
    state:{
        count:0
    },
    // 函数
    mutations:{
        //定义更改全局变量函数
        // 载荷参数   n
        increCount(state, n){
            // state.count++
            state.count += n
        },
        subtractCount(state){
            state.count--
        }
    },
    // Action 类似于 mutation,不同在于:
    //     Action 提交的是 mutation,而不是在组件中直接变更状态, 通过它间接更新 state。
    //     在组件中通过 this.$store.dispatch('actionName') 触发状态值间接改变
    //     Action 也支持载荷
    //     Action 可以包含任意异步操作。
    //     不建议直接更改mutation 最好在action中写逻辑之后再mutation改变
    actions:{
        // context 上下文  n 为载荷参数   increCount 调用mutations 里面的函数
        add(context, n){
            context.commit('increCount', n);
        },
        //按需导入   commit 提交函数   state全局变量
        decrement({commit, state}){
            console.log('全局参数', state.count)
            commit('subtractCount')
        }
    },
    //派生属性
    getters:{
        //会获取到上面的state
        remark(state){
            if(state.count < 50){
                return "吃饭";
            }else if(state.count < 100){
                return "睡觉";
            }else{
                return "打豆豆";
            }
        }
    }
}

const goods = {
    state:{},
    mutations:{},
    actions:{},
    getters:{}
}
const store = new Vuex.Store({   // 注意V和S都是大写
    modules:{
        home,
        goods
    }
});
export default store
<template>
  <div class="home">
     <!-- 调取全局变量 -->
     <!-- count : {{$store.state.count}} -->
     <!-- 以module模块化项目结构 (便于管理) 只需要更改获取参数state的这 别的不需要更改 -->
    count : {{$store.state.home.count}}
    <button @click="addCount">增加值</button>
    <button @click="decCount">减少</button>
    <h1>{{$store.getters.remark}}</h1>
  </div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from "@/components/HelloWorld.vue";

export default {
  name: "home",
  components: {
    HelloWorld
  },
  methods:{
    addCount(){
      //通过commotion  调用 mutations 中的 increment 改变状态值
      //多个参数, 叫提交载荷
      // this.$store.commit('increCount', 10);

      // 触发 actions 中的 add 改变状态值
      this.$store.dispatch('add', 10);
    },
    decCount(){
      // this.$store.commit('subtractCount');
      this.$store.dispatch('decrement');

    }
  }
};
</script>


标准项目结构 – 抽取文件标准化

在这里插入图片描述
跟级别的是公共的

在这里插入图片描述
home.js

const state = {
    count:0
}

//派生属性
const getters = {
        //会获取到上面的state
        remark(state){
            if(state.count < 50){
                return "吃饭";
            }else if(state.count < 100){
                return "睡觉";
            }else{
                return "打豆豆";
            }
        }
}

const mutations = {
    //定义更改全局变量函数
    // 载荷参数   n
    increCount(state, n){
        // state.count++
        state.count += n
    },
    subtractCount(state){
        state.count--
    }
}
// Action 类似于 mutation,不同在于:
//     Action 提交的是 mutation,而不是在组件中直接变更状态, 通过它间接更新 state。
//     在组件中通过 this.$store.dispatch('actionName') 触发状态值间接改变
//     Action 也支持载荷
//     Action 可以包含任意异步操作。
//     不建议直接更改mutation 最好在action中写逻辑之后再mutation改变
const actions = {
        // context 上下文  n 为载荷参数   increCount 调用mutations 里面的函数
        add(context, n){
            context.commit('increCount', n);
        },
        //按需导入   commit 提交函数   state全局变量
        decrement({commit, state}){
            console.log('全局参数', state.count)
            commit('subtractCount')
        }
}

export default {
    state,
    getters,
    mutations,
    actions
}

index.js

import Vue from "vue";
import Vuex from "vuex";
import home from "./modules/home.js"
import goods from "./modules/goods.js"
// //引入vuex插件
Vue.use(Vuex);

// 以module模块化项目结构

const store = new Vuex.Store({   // 注意V和S都是大写
    modules:{
        home,
        goods
    }
});
export default store

http://www.niftyadmin.cn/n/623085.html

相关文章

Makefile: Makefile中的变量数据库和隐含规则

/****************************************************************************************************************** 原文地址&#xff1a; 说明&#xff1a;这个是make -p显示make变量数据库和隐含 规则的内容。 *************************************************…

Ubuntu16.04 or ubuntu 20 安装 php7.3 Nginx Mysql8 + Ubuntu安装nginx_php7以及配置index.php启动 + 更换源

转载 安装ubuntu 20 https://blog.csdn.net/iamzhoujunjia/article/details/113826296 https://blog.csdn.net/qq_31953961/article/details/90079814 https://blog.csdn.net/sitebus/article/details/97435428 PHP安装扩展 大部分操作都是根据转载中的操作一样 那么中间又遇到…

python网站开发案例_Python Web框架Flask下网站开发入门实例

{%if islogin 1 %}Welcome ,{{username}}!{%else%}{{username}}!{%endif%}{%for nav in nav_list%}{{nav}}{%endfor%}{{blog[title]}}{{blog[content]}}{%for key,value in blogtag.items()%}{{key}}({{value}}){%endfor%}

Makefile: Makefile中的-I

书上是这样解释的&#xff1a; -I DIR 当包含其他 makefile 文件时&#xff0c;可利用该选项指定搜索目录 读了好多遍都没有懂&#xff0c;结果使我浮想联翩,最后在老师我指导下明白了&#xff1a; 指定目录下&#xff08;如tmp&#xff09;的makefile&#xff08;或者其他名…

ubuntu 部署 simps +mqtt

安装mqtt https://blog.csdn.net/qq_29933439/article/details/91307940 一. 为什么选择在ubuntu下安装服务器的原因 因为考虑后使用wireshark抓取MQTT数据包来进行对MQTT协议分析&#xff0c; mqtt客户端使用的是eclipse.paho.ui.app(安装在windows下&#xff0c;后续将介绍…

python 条形图与线图的图例_Matplotlib极坐标条形图图例

我有一些来自一份问卷的数据&#xff0c;它提供了6个“维度”的答案&#xff08;每个答案代表0-4之间的值&#xff09;。我试着在一个极坐标条形图上画出6个“维度”的平均值。在 这是我的代码&#xff1a;#!/usr/bin/env python3 import numpy as np import matplotlib.pyplot…

文件编程:create函数

例子&#xff1a; #include <stdio.h> #include <stdlib.h>#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>void create_file(char *filename) {if(creat(filename,0755)<0){printf("create file %s failure!\n"…

ubuntu 20 安装hyperf

首先看 hyperf 得安装环境 先安装PHP7.3以上 我安装了7.4 apt install php7.4-fpm php7.4-dev php7.4-mysql sudo apt install redis-server sudo apt install php-redis apt install nginx apt install mysql-server 顺手安装了mysql redis MySQL是8的 之后安装swoole 在码…