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. VSCode CSS自动补充前缀

    1.安装AuotPrefixer. 2.代码里写css样式后,Ctrl+Shift+P,选择AutoPrefix CSS执行 结果如下

  2. 应用留数定理计算实积分 $\dps{I(x)=\int_{-1}^1\frac{\rd t}{\sqrt{1-t^2}(t-x)}\ (|x|>1,x\in\bbR)}$ [华中师范大学2010年复变函数复试试题]

    应用留数定理计算实积分 $\dps{I(x)=\int_{-1}^1\frac{\rd t}{\sqrt{1-t^2}(t-x)}\ (|x|>1,x\in\bbR)}$ [华中师范大学2010 ...

  3. web全栈应用【爬取(scrapy)数据 -> 通过restful接口存入数据库 -> websocket推送展示到前台】

    作为 https://github.com/fanqingsong/web_full_stack_application 子项目的一功能的核心部分,使用scrapy抓取数据,解析完的数据,使用 pyt ...

  4. TPS和QPS 并发量区别;日活 访问量 活跃度

    一.系统承载吞度量 系统的吞度量(承压能力)与request对CPU的消耗.外部接口.IO等等紧密关联.单个reqeust 对CPU消耗越高,外部系统接口.IO影响速度越慢,系统吞吐能力越低,反之越高 ...

  5. Kotlin 检查空类型

    Kotlin 会检查你定义的非空类型 如果运行过程中被赋空值了  会直接crash

  6. vueRouter lazyLoad

    import Vue from 'vue' import Router from 'vue-router' import HelloWorld from '@/components/hello/ind ...

  7. Database学习 - mysql 视图/触发器/函数

  8. 服务器资源迁移到aliyun对象存储及oss的权限管理配置

    chinasoft-download增值服务的迁移和部署 需求: 增值服务网站需要从网宿迁移到阿里云,以前的增值服务历史软件存放在服务器中需要迁移到阿里云的oss中存放 需要改造程序给程序添加一个os ...

  9. iis7 设置自定义404页面无效解决方案

    想给自己做的的网站自定义一个404页面,开始 双击红框提示的错误页图标 双击上图红框提示的所示404行 修改上图红框提示的内容如下:我是直接在根目录放了一个自己做的404.html,实际情况要填写你自 ...

  10. Flask开发微电影网站(十)

    1.后台管理之角色管理 1.1 角色管理之定义角色表单 在app的admin目录的forms.py文件中,定义角色表单 # 角色表单 class RoleForm(FlaskForm): name = ...