当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vuex实现简易计数器

vuex实现简易计数器

2018年05月10日  | 移动技术网IT编程  | 我要评论

合肥房地产,重生1994之足球风云,新房装修步骤

本文实例为大家分享了vue.js计数器的制作方法,供大家参考,具体内容如下

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')

效果图:

本文已被整理到了《vue.js前端组件学习教程》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网