• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >vue > vuex实现简易计数器

vuex实现简易计数器

作者:SikiChan 字体:[增加 减小] 来源:互联网 时间:2017-05-30

本文主要包含vuex,vuex中文文档,vuex教程,vuex官网,vue vuex等相关知识,SikiChan 希望在学习及工作中可以帮助到您

本文实例为大家分享了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>

</div>

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>

</div>

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

</div>

action.js

export const incrementCounter = ({dispatch}) => dispatch('INCREMENT', 3)
export const reduceCounter = ({dispatch}) => dispatch('REDUCE')

</div>

getters.js

export function getCount (state) {
 return state.count
}

</div>

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>

</div>

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

</div>

效果图:

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

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

</div>

您可能想查找下面的文章:

  • 使用vuex的一点心得
  • 详解Vue 非父子组件通信方法(非Vuex)
  • 详解vuex 中的 state 在组件中如何监听
  • 详解Vue中状态管理Vuex
  • Vuex和前端缓存的整合策略详解
  • vue+vuex+axio从后台获取数据存入vuex实现组件之间共享数据
  • Vue.use源码分析
  • 一篇看懂vuejs的状态管理神器 vuex状态管理模式
  • Vuex之理解Mutations的用法实例
  • Vuex之理解Getters的用法实例

相关文章

  • 2017-05-30VUE开发一个图片轮播的组件示例代码
  • 2017-05-30Vuex之理解Getters的用法实例
  • 2017-05-30Vuejs第九篇之组件作用域及props数据传递实例详解
  • 2017-05-30Vue响应式添加、修改数组和对象的值
  • 2017-05-30Vue如何引入远程JS文件
  • 2017-05-30Vue.js实现无限加载与分页功能开发
  • 2017-05-30基于vuejs+webpack的日期选择插件
  • 2017-05-30详解如何使用vue-cli脚手架搭建Vue.js项目
  • 2017-05-30Vue.js每天必学之内部响应式原理探究
  • 2017-05-30详解vue之页面缓存问题(基于2.0)

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • Vue响应式添加、修改数组和对象的值
    • Vue开发中整合axios的文件整理
    • vue.js中$watch的用法示例
    • 简单理解vue中el、template、replace元素
    • vue省市区三联动下拉选择组件的实现
    • 利用vue实现模态框组件
    • vue-router 中router-view不能渲染的解决方法
    • Vue响应式原理详解
    • 详解Weex基于Vue2.0开发模板搭建
    • 如何在vue中引入第三方jquery,swiper等库(一)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有