前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vuex从入门到精通(二)

Vuex从入门到精通(二)

作者头像
lonelydawn
发布2018-02-09 12:26:38
8650
发布2018-02-09 12:26:38
举报
文章被收录于专栏:lonelydawn的前端猿区

前言

在本篇,我将演示如何使用

来完成一个 小型demo的制作(点击按钮,计数器自增)。

环境搭建

vue-cli & webpack

在任意目录下打开控制台, 输入

代码语言:javascript
复制
vue init webpack counter

中间的项目可以随便填选,但最后一项是否自动安装依赖一定要选 No

cnpm

npm 依赖有一些是国外的,安装比较慢还容易出错误.

命令行输入 :

代码语言:javascript
复制
npm install cnpm -g --registry=https://registry.npm.taobao.org

安装国内淘宝镜像源的cnpm.

使用cnpm 安装项目依赖 :

代码语言:javascript
复制
cnpm install

安装Vuex :

代码语言:javascript
复制
cnpm i vuex -S

启动项目服务 :

代码语言:javascript
复制
npm start

项目开发

目录结构

源代码

1. 建立store, 存放在store.js

代码语言:javascript
复制
import Vue from 'vue'
import Vuex from 'vuex'

// Vue 引入Vue插件方式
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    counter: 0
  },
  getters: {
    counter: state => state.counter
  },
  mutations: {
    addCounter (state) {
      state.counter ++
    }
  }
})

2. 修改webpack的入口文件 main.js

代码语言:javascript
复制
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
// 引入建立好的 store
import store from './store'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
// 绑定到 Vue 实例上
  store,
  template: '<App/>',
  components: { App }
})

将store绑定到Vue实例上之后,可以在每个组件中 使用 this.$store 进行操作.

3. 建立 计数器 组件 Counter.vue

代码语言:javascript
复制
<template>
  <div>
    <h1>counter: {{counter}}</h1>
    <button @click="addCounter">add counter</button>
  </div>
</template>

<script>
  import { mapMutations, mapGetters } from 'vuex'

  export default {
    name: 'Counter',
    computed: mapGetters([
      'counter'
    ]),
    methods: mapMutations([
      'addCounter'
    ])
  }
</script>

当然, Vuex 提供了 一种 十分友好的方式让组件引用store 中的属性和方法 :

  • getters 对应 mapGetters
  • mutations 对应 mapMutations
  • actions 对应mapActions

使用方式 :

代码语言:javascript
复制
<script>
  import { mapMutations, mapGetters } from 'vuex'

  export default {
    name: 'Counter',
    computed: mapGetters([
      'counter'
    ]),
    methods: mapMutations([
      'addCounter'
    ])
  }
</script>

或者

代码语言:javascript
复制
<script>
  import { mapMutations, mapGetters } from 'vuex'

  export default {
    name: 'Counter',
    computed: mapGetters({
      counter: 'counter'
    }),
    methods: mapMutations({
      addCounter: 'addCounter'
    })
  }
</script>

如果methods和computed中 存在组件的私有属性和方法, 那么可以使用 ES6 对象展开运算符

代码语言:javascript
复制
<script>
  import { mapMutations, mapGetters } from 'vuex'

  export default {
    name: 'Counter',
    computed: {
      ...mapGetters([
        'counter'
      ]),
      selfProperty () {
        return 'self property'
      }
    },
    methods: {
      ...mapMutations([
        'addCounter'
      ]),
      selfMethod () {
        return 'self method'
      }
    }
  }
</script>

4. 在 根组件 上挂载 Counter, App.vue

代码语言:javascript
复制
<template>
  <div id="app">
    <counter></counter>
  </div>
</template>

<script>
  import Counter from './components/Counter.vue'
  export default {
    name: 'app',
    components: {
      Counter
    }
  }
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

运行结果

最后

本节源码 : 

https://github.com/lonelydawn/counter

下一节我们将探讨一个更复杂的案例, 实战用户需求

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 环境搭建
    • vue-cli & webpack
      • cnpm
      • 项目开发
        • 目录结构
          • 源代码
            • 运行结果
            • 最后
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档