在研究VUEX的过程中的一个范例
来源:互联网 发布:小学四年级体测数据 编辑:程序博客网 时间:2024/06/01 22:57
首先vuex概念比较多,一定要搞懂里面的概念,可以参考官网Vuex2.0概念,我写此文的目的是希望能对前端爱好者提供个参考,加深对vuex2.0各核心概念的理解。
测试组件:vuex.vue
<template> <div> <h1>vuex 测试</h1> Clicked: {{ getCount }} times <button @click="increment">+</button> <button @click="decrement">-</button> </div></template><script> import { mapGetters } from 'vuex' import { mapActions } from 'vuex' export default { computed: { // 使用对象展开运算符将 getters 混入 computed 对象中 ...mapGetters([ 'getCount' // ... ]) }, methods: { ...mapActions([ 'increment', // 映射 this.increment() 为 this.$store.dispatch('increment') 'decrement' ]) //...mapActions({ // add: 'increment' // 映射 this.add() 为 this.$store.dispatch('increment') //}) } }</script>
actions.js文件:
//testexport const increment = ({commit}) => { commit('INCREMENT')}export const decrement = ({commit}) => { commit('DECREMENT')}
getters.js文件:
//testexport const getCount = state => { return state.count}
store.js文件:
import Vue from 'vue'import Vuex from 'vuex'import * as actions from './actions'import * as getters from './getters'Vue.use(Vuex)// 应用初始状态const state = { count: 10}// 定义所需的 mutationsconst mutations = { INCREMENT(state) { state.count++ }, DECREMENT(state) { state.count-- }}// 创建 store 实例export default new Vuex.Store({ actions, getters, state, mutations})
切记:
要先在main.js 引入
import store from './vuex/store'import Vuex from 'vuex'new Vue({ //el: '#app', //template: '<App/>', router, store, //components: { App } render: h => h(App)}).$mount('#app')
阅读全文
1 0
- 在研究VUEX的过程中的一个范例
- vuex在vue项目中的使用
- struts2的validate在使用过程中的一个问题
- struts2的validate在使用过程中的一个问题
- Google在Codility.com上的一个测试范例
- Vue.js+Vuex:一个简单的记事本
- 关于对vuex使用过程中的一些心得体会
- C++ 库研究笔记——拷贝构造函数的一个错误范例
- 。NET在安装过程中的一个问题
- 一个存储过程在.net中的调用
- 一个变量在uefi中的传递过程
- 一个不错的SQL范例
- 抽象工厂的一个范例
- 一个简单的 fwrite 范例
- 一个JinternalFrame的子类范例
- 虚函数的一个范例
- 一个有趣的HTML范例
- 一个常用的代码范例
- CardView
- 【JavaSE】接口和抽象类
- 把char*转换为wchar_t*
- Linux上使用tomcat8上传图片,通过nginx无法访问
- redis-3.2.8-Sentinel集群安装和部署
- 在研究VUEX的过程中的一个范例
- android静默安装与静默卸载
- MKT USB OTG功能如何打开及实现
- Laravel 常用命令合集
- 测试onmouseover事件
- 【教程】树莓派安装OS之后的初始配置,以安装OpenCV 3.1.0为例
- 想成为并做好一个IT项目经理,你需要坚持做的事情
- 深度学习所需的python-学习笔记10
- uiautomator中相同控件名该如何获取想要的控件,instance(0)区分布局一样的控件