[vue.js]vuex实现一个简易计数器
来源:互联网 发布:电子软件有哪些 编辑:程序博客网 时间:2024/06/07 01:50
src/components
Hello.vue
<template> <div class="hello"> <h1>Now count is {{counterValue}}</h1> <br> </div></template><script>import { getCount } from '../vuex/getters'export default { vuex: { getters: { counterValue: getCount } }, data () { return { } }}</script><style scoped>h1 { color: #42b983;}</style>
Increate.vue
<template> <div> <button @click='increment' class="btn btn-success">click me + 3</button> <button @click='reduce' class="btn btn-warning">click me - 1</button> </div></template><script>import { incrementCounter, reduceCounter } from '../vuex/action'export default { vuex: { actions: { increment: incrementCounter, reduce: reduceCounter } }, data: function () { return { } }, computed: {}, ready: function () {}, attached: function () {}, methods: {}, components: {}}</script><style lang="css"></style>
src/vuex
store.js
import Vue from 'vue'import Vuex from 'Vuex'Vue.use(Vuex)const state = { count: 0}const mutations = { INCREMENT (state, n) { state.count = state.count + n }, REDUCE (state) { state.count-- }}export default new Vuex.Store({ state, mutations})
action.js
export const incrementCounter = ({dispatch}) => dispatch('INCREMENT', 3)export const reduceCounter = ({dispatch}) => dispatch('REDUCE')
getters.js
export function getCount (state) { return state.count}
src/App.vue
<template> <div id="app"> <img class="logo" src="./assets/logo.png"> <hello></hello> <increate></increate> </div></template><script>import Hello from './components/Hello'import Increate from './components/Increate'import store from './vuex/store'export default { store, components: { Hello, Increate }}</script><style>html { height: 100%;}body { display: flex; align-items: center; justify-content: center; height: 100%;}#app { color: #2c3e50; margin-top: -100px; max-width: 600px; font-family: Source Sans Pro, Helvetica, sans-serif; text-align: center;}#app a { color: #42b983; text-decoration: none;}.logo { width: 100px; height: 100px}</style>
src/main.js
// 入口文件import Vue from 'vue'import App from './App'import VueRouter from 'vue-router'import VueResource from 'vue-resource'/* eslint-disable import VueRouter from 'vue-router'no-new */new Vue({ el: 'body', components: { App }})Vue.use(VueRouter)Vue.use(VueResource)var router = new VueRouter({ hashbang: false, // 设置为true时,所有的路径都会被格式化为#!开头 history: true // 默认false,利用history.pushState(), history.replaceState()来管理浏览历史记录})// require('./routers')(router)router.start(App, '#app')
完整代码点击:→ [vuex-demo简易计数器]
1 0
- [vue.js]vuex实现一个简易计数器
- Vue.js+Vuex:一个简单的记事本
- vue.js 之道vuex
- vue+vuex+localStorage实现记事本
- vue.js实现一个计算器
- Vue.js 系列教程 4:Vuex
- Vue.js 系列教程 4:Vuex
- Vue.js数据状态管理-Vuex(-)
- Vuex + Vue.js 小小单应用
- Vue.js状态管理工具Vuex快速上手
- vue.js移动端app实战3:从一个购物车入门vuex
- vue.js移动端从一个购物车入门vuex环境搭建
- Vue.js学习之vue-router vuex axios webpack
- Vue.js学习之vue-router vuex axios webpack
- 一个完整的vue应用 ( vuex+vue-router ) 起手
- 如何使用vue实现一个简易的购物车
- 实现js计数器
- Vue.js框架简易入门
- 从源码一次彻底理解Android的消息机制
- 反向投影(BackProjection)
- WireShark 过滤语法
- Javaweb学习总结(四)--eclipse中使用github之public key的生成和设置
- React Native,安装以及运行。
- [vue.js]vuex实现一个简易计数器
- Sequelize 和 MySQL 对照
- php+ajax实现分页
- RX操作符之Observable的创建方式一(just、from、repeat、repeatWhen)
- 制作Mac系统安装U盘 及安装流行 (OS X EI Capitan 10.11.5)
- EditText搜索关键字,返回结果匹配关键字改变颜色
- C的转义字符,格式控制符,常量变量
- linux兴趣小组暑假第二次讲座总结
- poj1511Invitation Cards