浅谈vuex的基本用法和mapaction传值问题

作者:请不要让我脱发 时间:2024-06-05 09:18:21 

vuex的理论知识就不多提了,官网上已经有明确的讲解。

用一个简单的例子来描述一下基本的用法:

第一步:npm install vuex –save-dev

第二步:在目录中创建store目录配置管理状态


//store/index.js
/**
* Created by zhaohuan on 2017/7/13.
*/
import Vue from 'vue'
import Vuex from 'vuex';

Vue.use(Vuex);

const store = new Vuex.Store({
state: {
 msg: '原始数据'
},
actions: {
 fun: function ({commit},date) {
  commit('saveAdminInfo', {list: date});
 },
},
mutations: {
 saveAdminInfo(state, adminInfo){
  state.msg = adminInfo.list;
 }
}
});

export default store;

第三步:在main.js中引入store


new Vue({

el: '#app',
router,
store,
template: '<App/>',
components: { App }
});

第四部:编写路由页面


//test1.vue
<template>
<div>
msg:{{msg}}

<input type="text" v-model="checkedNames" style="border: 1px solid red">
 <button @click="fun">提交</button>
</div>

</template>

<script>
import {mapState,mapActions} from 'vuex';
export default{
 data(){
   return{
    checkedNames:''
   }
 } ,
 computed: {...mapState(['msg'])},
 methods: {
//    ...mapActions(['fun']);
//如果需要向actions里面传值就手动调用
fun(){
    this.$store.dispatch('fun',this.checkedNames);
  }
//...mapActions(['fun'])==   this.$store.dispatch('fun');
 }
}
</script>

test2.vue


<template>
<div>
 msg:{{msg}}
</div>
</template>

<script>
import {mapState} from 'vuex';
export default{
 computed: {...mapState(['msg'])}, //对应getters.技术中的msg
//  methods: {...mapActions(['fun'])}
}
</script>

修改之前:

test1

浅谈vuex的基本用法和mapaction传值问题

test2

浅谈vuex的基本用法和mapaction传值问题

来源:https://blog.csdn.net/yangyiboshigou/article/details/75062739

标签:vuex,mapaction,传值
0
投稿

猜你喜欢

  • 超简单的微信小程序轮播图

    2024-04-25 10:36:05
  • asp如何显示SQL数据库所有表的名称?

    2010-06-08 09:30:00
  • Oracle 异构服务实践

    2007-08-17 10:00:00
  • python读取xml文件方法解析

    2021-04-25 03:53:45
  • Node.js模块全局安装路径配置方法

    2024-05-13 09:28:11
  • 简单谈谈GET和POST有什么区别

    2022-06-06 04:53:41
  • 如何利用Golang解析读取Mysql备份文件

    2024-01-28 20:51:19
  • python 时间 T 去掉 带上ms 毫秒 时间格式的操作

    2021-12-16 23:24:45
  • Flyway的简单介绍及使用详解

    2022-05-05 07:35:34
  • opencv3/C++图像像素操作详解

    2021-04-23 08:29:26
  • Python混合使用同步和异步函数的方法

    2021-07-19 05:01:05
  • js实现简单的联动菜单效果

    2024-04-19 09:54:45
  • 网页iframe元素应用浅析

    2009-04-11 18:11:00
  • python列表和字符串的三种逆序遍历操作

    2022-09-09 03:00:56
  • python中 @的含义以及基本使用方法

    2022-12-24 04:58:48
  • Python利用re模块实现简易分词(tokenization)

    2021-06-12 20:07:59
  • ASP中双引号单引号和&连接符使用技巧

    2007-10-01 18:20:00
  • pandas读取excel时获取读取进度的实现

    2022-03-24 09:57:26
  • python网络爬虫基于selenium爬取斗鱼直播信息

    2023-03-05 09:33:19
  • MySQL数据备份之mysqldump的使用详解

    2024-01-18 20:46:57
  • asp之家 网络编程 m.aspxhome.com