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 ...
随机推荐
- 小程序 TabBar 定制
使用微信小程序开发时,用到了其 API - tabBar,设置如下(详细的内容可以参考官网 api): "tabBar": { "color": "# ...
- String总结
- Python3:判断三角形的类型
# 判断三角形类型def triangle(a,b,c): if a>0 and b>0 and c>0: if a+b>c and b+c>a and a+c>b ...
- for 循环 与forEach 里面return 的区别
for 循环里面 return 可以直接终止 跳出 循环 forEach 是中断本次循环 直接 下一个循环 forEach想要取到值之后 直接跳出循环 可以用 try catch let arr = ...
- 浅谈axios
在我们的项目中有用到: fetch有直接用的,也有自己封装之后用的; vue-resource在vue1的时候使用,把方法抽象出来后,总需要往方法里传 this.$http ,感觉是个超级不爽的设计, ...
- MySql存储过程及函数
存储过程和函数类似于Java中的方法. ⒈存储过程 一组预先编译好的sql语句的集合,理解成批处理语句. 好处: ①提高代码的重用性 ②简化操作 ③减少了编译次数并且减少了和数据库服务器的连接次数,提 ...
- Windows密钥容器和证书的关系
其实CSP主要是对容器里的密钥对操作的,和证书关系不大. 容器里的密钥对有两种类型:一种是AT_KEYEXCHANGE,表示加密的密钥对,一种是AT_SIGNATURE表示签名的密钥对. 由于美国的出 ...
- 【原创】大数据基础之Kafka(1)简介、安装及使用
kafka2.0 http://kafka.apache.org 一 简介 Kafka® is used for building real-time data pipelines and strea ...
- C# 微信开发-----微信会员卡(一)
这是微信的官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025283,能看懂的朋友就请不要往下看了,我是看不懂 ...
- Confluence 使用常见问题列表
Confluence 6 管理 Atlassian 提供的 App 摘要: Confluence 用户可以使用桌面应用来编辑一个已经上传到 Confluence 的文件,然后这个文件自动保存回 Con ...