首页 > 其他分享 >Vue学习笔记:Vuex Part03 Action

Vue学习笔记:Vuex Part03 Action

时间:2023-08-17 10:35:26浏览次数:36  
标签:Vue const Part03 state context Action import store

定义Action

Action 类似于 mutation,不同在于:

  • Action 提交的是 mutation,而不是直接变更状态。
  • Action 可以包含任意异步操作。

示例

const store = createStore({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    increment (context) {
      context.commit('increment')
    }
  }
})

在这个示例中可以看到包含了state mutations和action。并且在actions内部使用context对象实现对mutation的commit。这个context是与store实例具有相同方法与属性的对象

分发Action

Action通过store.dispatch方法触发

Action与Mutation的最大区别是,Mutation必须同步执行,而Action可以在内部执行一步操作。

示例

在state中创建deptdata,使用mutation提交数据,在action中引入axios读取数据。

store/index.js

// 1页面有多个需要共享的状态,引入vuex,便于维护(非父子通信)
// 2缓存部分异步数据,减少后端服务的访问,增加体验

import { createStore } from 'vuex'
import axios from 'axios'
import { ssrContextKey } from 'vue'

const store = createStore({
    state() {
        return {
            count: 0,
            datadept: []
        }
    },
    mutations: {
        increment(state) {
            state.count++
        },
        setcount(state, payload) {
            state.count = payload
        },
        setdatadept(state, payload) {
            state.datadept = payload
        }
    },
    actions: {
        async getDataDept(context) {
            if (context.state.datadept.length === 0) {
                console.log("length is 000")
                const res = await axios.get('http://127.0.0.1:5000/api/test/jsontest/Depts')
                console.log(res.data.msg)
                context.commit("setdatadept", res.data.msg)
            }

        }
    }
}
)
export default store

在deptlistView中展示数据

<template>
    <ul>
        <!-- <router-link v-for="d in data" :key="d.id" :to="'/user/detail/'+d.id">
            <li>{{ d.name }}</li>
        </router-link> -->
        <li v-for="d in store.state.datadept" :key="d.id" @click="routeClick(d.id)">{{ d.name }}</li>
    </ul>
</template>
<script setup>
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import { useStore } from 'vuex'

const store = useStore()
const router = useRouter()
const routeClick = (userid) => {
    router.push('detail/' + userid)
}
onMounted(
    () => {
        store.dispatch('getDataDept')
    }
)
</script>

Vue学习笔记:Vuex Part03 Action_action

标签:Vue,const,Part03,state,context,Action,import,store
From: https://blog.51cto.com/quietguoguo/7118801

相关文章

  • vue create is a Vue CLI3 only command and you are using Vue CLI 2.9.6.You may wa
    遇到的问题:vuecreateisVueCLI3onlycommandandyouareusingVueCLI2.9.6.YoumaywanttorunthefollowingtoupgradetoVueCLI3翻译:vuecreate是vueCLI3唯一的命令,您正在使用vueCLI2.9.6。您可能需要运行以下命令来升级到VueCLI3问题原因:安装的版本太低......
  • Vue 框架下提升加载速度的一些实战经验分享
    现在前端的框架有很多,甚至两只手已经数不过来,当然也完全没必要全部都学,还是应该深入的学习一两个被广泛使用的就好。其实我和大部分同学的想法一致,认为最值得我们深究的还是主流的Vue和React。我们通过深入的学习了解这些框架的思维,也让自己使用这些框架能够更加得心应手。......
  • 【校招VIP】前端校招考点之vue底层特性
    考点介绍:大家在面试途中遇到的相对多的问题,也是难点的问题一般都有vue底层原理。对于只会用但是不懂的小白来说真是太痛苦了,仅仅能说出来一些数据劫持,双向数据绑定,虚拟dom树的名词来说远远不够。一、考点题目1、Vue实现数据双向绑定的原理是什么?解答:Vue实现数据双向绑定的原......
  • vue 拖动元素传参
    这里演示拖动左侧列表中单元格到右侧内容区 <divclass="tree_root"><divclass="level_root""><div:draggable="draggable"@dragstart="treeItemDragstart($event,levelItem)"class=&quo......
  • vue3 组合式 api 单文件组件写法
    1Vue3组合式API的基本原理和理念1.1Vue3中的CompositionAPIVue3中的CompositionAPI是一种新的编写组件逻辑的方式,它提供了更好的代码组织、类型推导、测试支持和复用性。相比于Vue2的OptionsAPI,CompositionAPI更加灵活和可扩展。在CompositionAPI中,我们使......
  • vue引入外部字体
    1.下载字体包2.将字体包放在资源目录src/assets/font下3.创建字体css文件夹src/assets/fontcss,创建字体css文件并引入字体包/*注意:font-family:'XXX';将字体名字自定义为XXX,使用时要用这个名字*/@font-face{font-family:'myFont';src:url('../font/Not......
  • Programming abstractions in C阅读笔记: p114-p117
    《ProgrammingAbstractionsinC》学习第48天,p114-p117,​总结如下:一、技术总结主要通过randomnumber介绍了随机数的相关用法,interface​示例(random.h)​,clientprogram示例(craps.c)。#include<stdio.h>#include"genlib.h"#include"random.h"staticboolTryToMakePo......
  • 12 个不可错过的 Vue UI 组件库,请查收!
    Vue.js是一个渐进式javascript框架,用于构建UIS(用户界面)和SPA(单页应用程序)。UI组件库的出现提高了我们的开发效率,增强了应用的整体外观、感觉、交互性和可访问性,下面就来看看有哪些适用于Vue的UI组件库。 1.ElementUIElementUI是一套为开发者、设计师和产品经理......
  • Vue3 常用组件库推荐
    Vue 作为一款深受广大群众喜欢的前端框架,拥有众多优秀的开源UI组件库,这里整理了一下供大家参考。这几套框架主要用于后台管理系统和移动端的制作,方便开发者快速开发。一、PC端UI库1.1ElementPlus官网地址:https://element-plus.org/zh-CN/Github:https://github.com/e......
  • 周二 vue
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><divid="app"><divv-for="addrinaddrs">......