1.  安装webpack的问题: webpack坑系列--安装webpack-cli

2.  vue-cli(vue脚手架)超详细教程

3.  在命令行中使用 touch 执行新建文件;

4.  关于Vue.use()详解

5. vuex 最简单的介绍

  目录如左侧所示,主要是标红的三个文件。

5.1 store文件,编写vuex的各个功能,包括:

import Vue from 'vue'
import Vuex from 'vuex' Vue.use(Vuex) const state = { // 定义状态数据
count: 2
} const mutations = { // 定义方法,操作数据
increment (state) {
state.count++
},
decrement (state) {
state.count--
}
} const actions = {
add: ({commit}) => { // 触发上述的方法,对外提供的方法接口,可以在这里提供异步操作
commit('increment')
},
reduce: ({commit}) => {
commit('decrement')
}
} export default new Vuex.Store({state, mutations, actions})

5.2 然后在main文件中,引入store 注意这里是小写的store

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
Vue.config.productionTip = false /* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})

5.3 最后在vue文件中调用:

<template>
<div>
<h1>{{ $store.state.count }}</h1>
<button type="button" @click="add">增加</button>
<button type="button" @click="reduce">删减</button>
</div>
</template>
<script>
import { mapActions } from 'vuex'
export default {
methods: mapActions([
'add',
'reduce'
])
}
</script>

注意:在5.2中挂在元素时,加入的store,才能在接下来的文件中,引用到 $store

6.  mapState 的使用方法   (文章标题:   state,mapState,...mapState对象展开符详解 )

注意的是: 用data接收的 $store 不能及时响应更新,用computed就可以.

7. 多个组件使用不同的vuex:

主要涉及上面三个地方,其中

1、  main.js 主要用来引入注册 store,

2、 store文件夹下定义不同页面或组件对应的 vuex数据,并且有index来进行统一的对外输入,

3、 componments文件夹喜爱定义的a和b等组件是html范畴,调用vuex

具体如下所示:

(1)main文件引入 store的index文件如下所示:

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/index.js'//注意这里引入的是store文件夹下的index文件
Vue.config.productionTip = false new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})

(2) index文件 引入a和b文件

import Vue from 'vue';
import Vuex from 'vuex';
import moneys from './modules/a'
import counts from './modules/b' Vue.use(Vuex)//安装注册vuex export default new Vuex.Store({//实例话vuex,并引入两个vuex定义的函数
modules:{
moneys,
counts
}
})

(3)store文件夹下的a.js

const state = {//定义state
money: 10
} const mutations = {//定义操作数据方法
add(state,parmas) {//接受传参的方法
state.money+=parmas;
},
reduce(state) {
state.money--
}
} const actions = {//定义外部接口,包括异步处理
add: ({commit},parmas) => {//注意这里的传参方式
commit('add',parmas)
},
reduce: ({commit}) => {
commit('reduce')
}
} export default {//抛出定义的函数和开启命名空间
namespaced:true, //用于在全局引用此文里的方法时标识这一个的文件名
state, mutations, actions }

对应的b与其类似。对外的index文件如下:

import Vue from 'vue';
import Vuex from 'vuex';
import moneys from './modules/a'
import counts from './modules/b' Vue.use(Vuex)//安装注册vuex export default new Vuex.Store({//实例话vuex,并引入两个vuex定义的函数
modules:{
moneys,
counts
}
})

(4)对应的vue调用vuex

<template>
<div class="box">
<div>{{$store.state.moneys.money}}</div><!-- 注意这里调用到的state值,需要带上命名空间moneys-->
<button @click="add(2)">增加</button>
<button @click="reduce">减少</button>
</div> </template>
<script>
import {mapActions} from 'vuex'
export default {
methods: mapActions('moneys',['add','reduce'])//注意这里的命名空间 moneys
  //this.$store.dispatch('delTask', {task, index});  传递多个参数的时候 写成一个对象
}
</script>

以下两个方式一样:

changeTab(index){
this.$store.dispatch('leftNav/changeTab',index) //left是命名空间,index是传递的参数
},
//...mapActions('leftNav',['changeTab'])

分发事件和常规事件混合方法:

methods: {
...mapActions('a',['changeData']), //注意这里要写上...符号
triger:()=>{
alert(1);
}
}

附:   超简单入门Vuex小示例

vue全家桶+Koa2开发笔记(1)--vuex的更多相关文章

  1. vue全家桶+Koa2开发笔记(5)--nuxt

    1. nuxt项目初始化报错 下面是使用 koa 模板方法初始化一个项目,使用该方法需要将 nuxt 的版本降至1.4.2: 官方 https://zh.nuxtjs.org/guide/instal ...

  2. vue全家桶+Koa2开发笔记(7)--登陆注册功能

    1 文件结构:pages中放置页面代码:server 分为 dbs 和interface两个文件夹: dbs设置有关数据库的代码:interface设置接口信息: 2.2 先看dbs的,在dbs的配置 ...

  3. vue全家桶+Koa2开发笔记(6)--app开发

    1.环境配置 详见文章<Nuxt 开发 - 项目初始化> 1.1  使用nuxt脚手架  https://zh.nuxtjs.org/guide/installation 1.2 在nod ...

  4. vue全家桶+Koa2开发笔记(2)--koa2

    1. 安装koa脚手架的时候 执行命令 koa2 -e koa-learn 注意要使用-e的方式,才会生成ejs的模板 2. async await的使用方法:存在的意义:提高promise的可读性 ...

  5. vue全家桶+Koa2开发笔记(8)--开发网页

    1.使用 mongoose 动态倒入数据 mongoimport -d student -c areas areas.dat -d 后面是数据库名称: -c后面是表名称 最后是数据源 2.使用vue的 ...

  6. vue全家桶+Koa2开发笔记(4)--redis

    redis用来在服务器端存放session 1 安装redis    brew install redis 启动redis   redis-server 2 安装两个中间件  npm i koa-ge ...

  7. vue全家桶+Koa2开发笔记(3)--mongodb

    1. 安装 momgodb brew install mongodb安装成功后执行 which mongod启动:mongod 2. 下载可视化操作数据库的软件 https://robomongo.o ...

  8. Vue 全家桶 + Electron 开发的一个跨三端的应用

    代码地址如下:http://www.demodashi.com/demo/11738.html GitHub Repo:vue-objccn Follow: halfrost · GitHub 利用 ...

  9. [在线+源码]vue全家桶+Typescript开发一款习惯养成APP

    # vue-ts-daily 基于Vue.js的2.5.13版本和TypeScript编写的模仿原生应用的WebApp. [源码地址](https://github.com/xiaomuzhu/vue ...

随机推荐

  1. Linux防火墙iptables的策略

    iptables策略 iptables -L #查看现有防火墙所有策略 iptables -F #清除现有防火墙策略 只允许特定流量通过,禁用其他流量 1.允许SSH流量(重要) iptables - ...

  2. python全栈开发笔记--------条件语句

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false. Py ...

  3. Python Django 之 Template 模板的使用

    一.模板样式 注意: 1.url urlpatterns = { path('admin/', admin.site.urls), path('order/', views.order), path( ...

  4. Grafana展示報表數據的配置(二)

    一.Grafana以圖表的形式展示KPI報表的結果數據1.按照日期顯示數據達標量與未達標量2.顯示當前報表的最大值.最小值.平均值.總量3.報表結果數據的鏈接分享與頁面嵌入,用戶無需登錄直接訪問報表統 ...

  5. Огонек--灯光--IPA--俄语

    苏联老歌总让人沉浸其中.

  6. SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。

    题目描述               对于如下表actor,其对应的数据为: actor_id first_name last_name last_update 1 PENELOPE GUINESS ...

  7. 玩转X-CTR100 l STM32F4 l MPU6050加速度陀螺仪传感器

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      本文介绍X-CTR100控制器 板载加速度 ...

  8. mySql单列索引与联合索引的区别

    引自https://my.oschina.net/857359351/blog/658668 第一张表gift和索引为联合索引,如图: 第二张表gift2为单列索引,如图: 下面开始进行测试: 相同的 ...

  9. Problem C: 默认参数:求圆面积

    Description 编写一个带默认值的函数,用于求圆面积.其原型为: double area(double r=1.0); 当调用函数时指定参数r,则求半径为r的圆的面积:否则求半径为1的圆面积. ...

  10. GNU C的定义长度为0的数组

    在标准C和C++中,长度为0的数组是被禁止使用的.不过在GNU C中,存在一个非常奇怪的用法,那就是长度为0的数组,比如Array[0];很多人可能觉得不可思议,长度为0的数组是没有什么意义的,不过在 ...