vue脚手架

npm install -g vue-cli

usage:

vue init

example:

vue init webpack myvue

安装vuex:

npm i -S vuex

详情看官网:

https://vuex.vuejs.org/zh/guide/

使用main.js 引入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/>'
})

src中创建store文件夹,文件夹中创建index.js

import Vue from 'vue';
import Vuex from 'vuex'; Vue.use(Vuex); let store = new Vuex.Store({
state:{
msg:'vuex测试文件',
todos: [
{ id: 1, text: '...', done: true },
{ id: 2, text: '...', done: false }
]
},
getters:{
doneTodos: state => {
console.log(23333)
return state.todos.filter(todo => todo.done)
}
},
mutations:{
changeMutation(state,payload){
//state.msg = "vuex测试文件change";
state.msg = payload.tmp;
console.log(123,state.msg,payload)//{tmp:"vuex测试文件change"}
},
},
actions:{
changeAction({commit},payload){
commit("changeMutation",{tmp:"vuex测试文件change"});
console.log(123,payload)//undefined
},
},
modules:{},
}) export default store;

components文件夹中的About.vue

<template>
<div class="hello">
<h1>{{ msg }}</h1> <h2 @click='change({tmp:"vuex测试文件change"})' >vuex-test</h2>
<div>{{this.$store.state.msg}}</div>
</div>
</template> <script>
import Vue from 'vue';
import {mapActions,mapMutations,mapState,mapGetters} from "vuex"; export default {
name: 'About',
data () {
return {
msg: 'Hellow world'
}
},
methods:{
test(){
//console.log(1,this.$route.params);
//console.log(2,this.$route.query.name);
//console.log(3,this.$store.state.msg)
//console.log(this.msg)
// console.log(this.$store.getters.doneTodos) },
//change({tmp:"vuex测试文件change"})中的参数映射到changeAction({commit},payload){}中的payload
...mapActions({change:"changeAction"}),
//...getActions() },
computed:{
...mapState({
//设置state的msg值为store的state.msg
msg2: state => state.msg
}),
//...mapGetters({rename:"doneTodos"}),
...mapGetters(["doneTodos"]), },
updated(){
this.test();
//this.rename;
this.doneTodos
},
mounted(){
//this.test(); } }
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
} </style>

Module

Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割:

const moduleA = {
state: { ... },
mutations: { ... },
actions: { ... },
getters: { ... }
} const moduleB = {
state: { ... },
mutations: { ... },
actions: { ... }
} const store = new Vuex.Store({
modules: {
a: moduleA,
b: moduleB
}
}) store.state.a // -> moduleA 的状态
store.state.b // -> moduleB 的状态

9、vuex快速上手的更多相关文章

  1. vue.js和vue-router和vuex快速上手知识

    vue.js和vue-router和vuex快速上手知识 一直以来,认为vue相比react而言,学习成本会更低,会更简单,但最近真正接触后,发现vue的各方面都有做一些客户化的优化,有一些亮点,但也 ...

  2. vuex 快速上手,具体使用方法总结(含使用例子)

    网上有关vuex的文章很多,但有些比较复杂,这篇文章能让你快速使用vuex: vuex 用处:管理全局状态(类似全局变量,每个组件都能访问到) vuex 用法: //下面是一个js文件,用最简单最全的 ...

  3. 如何快速上手一个新技术之vue学习经验

    碰到紧急项目挪别人的vue项目过来直接改,但是vue是18年初看过一遍,18年底再来用,早就忘到九霄云外了,结果丢脸的从打开vue开始学,虽然之前在有道云笔记做了很多记录,然后没有系统整理.所以借这次 ...

  4. 【转】Vue.js 2.0 快速上手精华梳理

    Vue.js 2.0 快速上手精华梳理 Sandy 发掘代码技巧:公众号:daimajiqiao 自从Vue2.0发布后,Vue就成了前端领域的热门话题,github也突破了三万的star,那么对于新 ...

  5. 【Python五篇慢慢弹】快速上手学python

    快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...

  6. 快速上手Unity原生Json库

    现在新版的Unity(印象中是从5.3开始)已经提供了原生的Json库,以前一直使用LitJson,研究了一下Unity用的JsonUtility工具类的使用,发现使用还挺方便的,所以打算把项目中的J ...

  7. [译]:Xamarin.Android开发入门——Hello,Android Multiscreen快速上手

    原文链接:Hello, Android Multiscreen Quickstart. 译文链接:Hello,Android Multiscreen快速上手 本部分介绍利用Xamarin.Androi ...

  8. [译]:Xamarin.Android开发入门——Hello,Android快速上手

    返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...

  9. 快速上手seajs——简单易用Seajs

    快速上手seajs——简单易用Seajs   原文  http://www.cnblogs.com/xjchenhao/p/4021775.html 主题 SeaJS 简易手册 http://yslo ...

随机推荐

  1. IISExpress使用64位

    C#有一些函数如GetHashCode和x86,X64版本有关系,为了和服务器保持一致,本地iis Express也需要设置64位. 方法如下,vs2010不支持. vs2012可以手动添加配置 re ...

  2. LaTeX技巧561:LaTeX如何让每一章带有目录?

    转自: http://blog.sina.com.cn/s/blog_5e16f1770102ds8b.html LaTeX技巧561:LaTeX如何让每一章带有目录? [问题描述] 当前章节列出该章 ...

  3. [再寄小读者之数学篇](2014-04-18 from 352558840@qq.com [南开大学 2014 年高等代数考研试题]反对称矩阵的组合)

    (2014-04-18 from 352558840@qq.com [南开大学 2014 年高等代数考研试题]反对称矩阵的组合) 设 ${\bf A},{\bf B}$ 都是反对称矩阵, 且 ${\b ...

  4. AngularJS DI(依赖注入)实现推测

    AngularJS DI(依赖注入) http://www.cnblogs.com/whitewolf/archive/2012/09/11/2680659.html 回到angularjs:在框架中 ...

  5. Blender 快捷键笔记

    A Select All/Unselect All shift+A Create Z 切换wireframe和solid mode TAB Start or quit EditMode B Activ ...

  6. Celery - 一个懂得 异步任务 , 定时任务 , 周期任务 的芹菜

    1.什么是Celery?Celery 是芹菜Celery 是基于Python实现的模块, 用于执行异步定时周期任务的其结构的组成是由    1.用户任务 app    2.管道 broker 用于存储 ...

  7. 409 javascript if and while表达式

    数组定义.特点.运算符:算术运算 ++ --(自减 自加) 赋值运算发 =比较:!= == === 逻辑运算 有 && || ! 正则表达式 修饰符 i:用来表示 g:很少演示(在第一 ...

  8. EDID:识别和解决常见问题指南

    随着 HDMI.DVI 和 DisplayPort 的迅速采用,专业视音频行业正继续从模拟向数字视频技术转换.虽然在很大程度上取得了成功,但在向最终用户提供稳定.无故障的视频系统时,这种转变仍带来了诸 ...

  9. Alpha 冲刺 (9/10)

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:冲刺倒计时之9 团队部分 后敬甲(组长) 过去两天完成了哪些任务 答辩准备中 和大佬们跟进进度 接下来的计划 准备答辩 ...

  10. FastDFS使用

    1.在linux系统中安装FastDFS服务image-server.7z 2.导入FastDFS jar包 fastdfs_client_v1.20.jar 3.创建配置文件fastdfs_clie ...