9、vuex快速上手
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快速上手的更多相关文章
- vue.js和vue-router和vuex快速上手知识
vue.js和vue-router和vuex快速上手知识 一直以来,认为vue相比react而言,学习成本会更低,会更简单,但最近真正接触后,发现vue的各方面都有做一些客户化的优化,有一些亮点,但也 ...
- vuex 快速上手,具体使用方法总结(含使用例子)
网上有关vuex的文章很多,但有些比较复杂,这篇文章能让你快速使用vuex: vuex 用处:管理全局状态(类似全局变量,每个组件都能访问到) vuex 用法: //下面是一个js文件,用最简单最全的 ...
- 如何快速上手一个新技术之vue学习经验
碰到紧急项目挪别人的vue项目过来直接改,但是vue是18年初看过一遍,18年底再来用,早就忘到九霄云外了,结果丢脸的从打开vue开始学,虽然之前在有道云笔记做了很多记录,然后没有系统整理.所以借这次 ...
- 【转】Vue.js 2.0 快速上手精华梳理
Vue.js 2.0 快速上手精华梳理 Sandy 发掘代码技巧:公众号:daimajiqiao 自从Vue2.0发布后,Vue就成了前端领域的热门话题,github也突破了三万的star,那么对于新 ...
- 【Python五篇慢慢弹】快速上手学python
快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...
- 快速上手Unity原生Json库
现在新版的Unity(印象中是从5.3开始)已经提供了原生的Json库,以前一直使用LitJson,研究了一下Unity用的JsonUtility工具类的使用,发现使用还挺方便的,所以打算把项目中的J ...
- [译]:Xamarin.Android开发入门——Hello,Android Multiscreen快速上手
原文链接:Hello, Android Multiscreen Quickstart. 译文链接:Hello,Android Multiscreen快速上手 本部分介绍利用Xamarin.Androi ...
- [译]:Xamarin.Android开发入门——Hello,Android快速上手
返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...
- 快速上手seajs——简单易用Seajs
快速上手seajs——简单易用Seajs 原文 http://www.cnblogs.com/xjchenhao/p/4021775.html 主题 SeaJS 简易手册 http://yslo ...
随机推荐
- luogu 1731 搜索剪枝好题
搜索剪枝这个东西真的是骗分利器,然鹅我这方面菜的不行,所以搜索数学dp三方面是真的应该好好训练一下 一本通的确该认真的刷嗯 #include<bits/stdc++.h> using na ...
- APPLE-SA-2019-3-25-1 iOS 12.2
APPLE-SA-2019-3-25-1 iOS 12.2 iOS 12.2 is now available and addresses the following: CFStringAvailab ...
- MySQL数据库学习2 - 数据库的操作
一.系统数据库 二.创建数据库 三.数据库相关操作 四.了解内容 一.系统数据库 执行如下命令,查看系统库 show databases; information_schema: 虚拟库,不占用磁盘空 ...
- oracle的安装与卸载
安装oracle: 下载符合系统要求的oracle数据库 2. 将上面的压缩文件都解压到一个文件夹中,然后以管理员的身份运行其中的可执行文件(.exe) 3. 配置安全更新(可选可不选,学习时我没 ...
- codeblocks1712设置中文
下载汉化包:百度网盘,密码:7rrz 下载后放到安装目录:D:\Program Files (x86)\CodeBlocks\share\CodeBlocks\locale\zh_CN(根据个人安装目 ...
- android studio发布项目到github
点击file setting ,打开对话框,如下,判断git是否安装成功 选择GitHub,填写github地址及密码 发布项目:
- C语言官网蓝桥杯训练1115DNA
这道题是比较简单的输出字符图形的题,但是有几个坑还是要注意下. 1.题中所述的X是大写的,不要看成小写了.(我就错了好几次) 2.每一行输出最后的X后不能在输出空格. 3.输出两个DNA中间有一个空行 ...
- selenium——键盘操作
很多键盘操作实际是没有意义的.
- rsync3.1.3的编译安装和常用操作
.rsync的编译安装 .tar.gz cd rsync- ./configure --prefix=/usr/local/rsync- --disable-ipv6 .rsync的配置文件: [ro ...
- 【原创】大数据基础之Airflow(1)简介、安装、使用
airflow 1.10.0 官方:http://airflow.apache.org/ 一 简介 Airflow is a platform to programmatically author, ...