vuex的使用二
1.先看项目的目录结构

2.在main.js里需要引入store这个文件并挂在实例上
import store from './store/store'
............ new Vue({
el: '#app',
router,
store,
template: '<App/>',
components: { App }
})
3.store.js里引入action.js和mutation.js文件
// 状态管理器
import Vue from 'vue'
import Vuex from 'vuex'
import actions from './action'
import mutations from './mutation'
Vue.use(Vuex) const store = new Vuex.Store({
state : {
author: 'Wise Wrong',
amsg: '',
},
actions,
mutations,
getters:{
author(state){
console.log(state)
return state.author
}
}
})
export default store
4.action.js
import * as types from './mutation_type'
export default{
newAuthor({commit},bData){
commit(types.NEWAUTHOR,bData);
}
}
5.mutation.js
import * as types from './mutation_type'
export default{
[types.NEWAUTHOR](state,msg){
state.author=msg;
}
}
6.mutation_type.js
export const SEND_A="SEND_A"
export const NEWAUTHOR="NEWAUTHOR"
7.head.vue
temple:
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" v-model="inputTxt" class="form-control" placeholder="通过input改变author">
</div>
<button type="button" class="btn btn-default" @click="setAuthor">Submit</button>
</form> script: data () {
return {
inputTxt:""
}
},
methods:{
setAuthor: function () {
this.$store.dispatch("newAuthor", this.inputTxt);
}
}
8.foot.vue
<p>
Copyright © author:{{author}} - 2016 All rights reserved
</p> import { mapGetters} from 'vuex' computed: {
...mapGetters(['author'])
}
9.效果


vuex的使用二的更多相关文章
- vuex学习(二)
参考:https://segmentfault.com/a/1190000015782272 vue 2.0+ 你的vue-cli项目中安装 vuex : npm install vuex --sav ...
- vuex之getter(二)
说明 使用vue,如果想对data数据派生一些状态,我们就用到计算属性或者侦听器,同样vux想要派生state中的一些状态,可以在store中定义一个getters属性,它相当于state的计算属性. ...
- Vuex以及axios
Vuex 简介 vuex是一个专门为Vue.js设计的集中式状态管理架构. 状态? 我们把它理解为在data中需要共享给其他组件使用的部分. Vuex和单纯的全局对象有以下不同: 1.Vuex 的状态 ...
- vuex 子组件传值
以下是基础的使用方法,详细且深入使用方法详细见博客:https://segmentfault.com/a/1190000015782272 Vuex官网地址:https://vuex.vuejs.or ...
- Vuex以及axios 看这个
vuex -- 安装 npm i vuex -- 配置 -- 导入vuex import vuex from "vuex" -- vue使用vuex ...
- vuex秘籍
vue项目开发中,大型项目一般vuex所需要存储的状态一般都很都,这时,我们便需要进性模块化划分,然后 再页面中采用映射来实现state的调用: 目录一般如下: store为总的状态库存放文件. mo ...
- Vuex+axios
Vuex+axios Vuex简介 vuex是一个专门为Vue.js设计的集中式状态管理架构. 状态? 我们把它理解为在data中需要共享给其他组件使用的部分. Vuex和单纯的全局对象有以下不同 ...
- NO--12模拟服务器端请求之node.js
最近几天项目上线,工作比较忙,没时间更博了,好在今天有点时间并且同事问道我一个问题,正好一块解决 使用 Vue 写项目肯定会遇到一个问题,如何模拟服务端请求数据,那这就需要用到 node.js 了. ...
- vuejs学习之 项目打包之后的首屏加载优化
vuejs学习之 项目打包之后的首屏加载优化 一:使用CDN资源 我们在打包时,会将package.json里,dependencies对象里插件打包起来,我们可以将其中的一些使用cdn的方式加载,例 ...
随机推荐
- [No000017A]改善C#程序的建议3:在C#中选择正确的集合进行编码
要选择正确的集合,我们首先要了解一些数据结构的知识.所谓数据结构,就是相互之间存在一种或多种特定关系的数据元素的集合.结合下图,我们看一下对集合的分类. 集合分类 在上图中,可以看到,集合总体上分为线 ...
- [No0000E3]C# 数据类型
在 C# 中,变量分为以下几种类型: 值类型(Value types) 引用类型(Reference types) 指针类型(Pointer types) 值类型(Value types) 值类型变量 ...
- python面向对象:类方法
类的方法包括以下几种: 构造方法 :__init__(self,) 析构方法 :__del__(self) 类方法@classmethod.实例方法.静态方法@staticmethod 一.构造方法 ...
- iOS 模拟器运行不能联网 PAC Fetch failed with error
app在模拟器是哪个启动成功会自动连接服务器,然后Xcode控制台报错, 模拟器 PAC Fetch failed with error [NSURLErrorDomain:-1001] 这类问题有好 ...
- GIS软件相关安装(持续更新)
软件安装是GIS专业的必修课,总会忘记步骤,在此汇总 1.oracle ①无法登录 管理员登录 sqlplus sys/密码 as sysdba https://www.linuxidc.com/li ...
- Improved SEO with mod_rewrite
PHP Advanced and Object-Oriented Programming Third Edition <?php //D:\wamp64\www\0613pm\w_wwwroot ...
- Appium入门(3)__ Appium Server安装
安装Appium 1.下载并安装:https://bitbucket.org/appium/appium.app/downloads/ 2. 系统变量PATH 增加 C:\Program Files ...
- MonkeyRunner_真机_运行脚本(二)
# -*- coding: UTF-8 -*- #手机分辨率为1080*1920 import sys from com.android.monkeyrunner import MonkeyRunne ...
- go不在dock显示运行
用这种方法就可以了go build -ldflags -H=windowsgui XXX.go
- java 数组(二)
public class ArrayDemo{ public static void main(String[] args){ int[] arr = {1,5,3,8,2,9,17,13}; get ...