Git Product home page Git Product logo

actiondemo's Introduction

vuexDemo

使用vuex保存收藏的文章信息

vuex如何使用

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。

Vuex 使用单一状态树——是的,用一个对象就包含了全部的应用层级状态,所以就在state.js使用一个对象作为唯一的数据源

vuex 目录,一般拆分下面的文件结构,会更规范点
|-- src
    |-- store
        |-- actions.js
        |-- getters.js
        |-- index.js
        |-- mutations.js
        |-- mutations-type.js
        |-- state.js

使用常量替代 Mutation 事件类型
// mutation-types.js
export const SOME_MUTATION = 'SOME_MUTATION'
// store.js
import Vuex from 'vuex'
import { SOME_MUTATION } from './mutation-types'

const store = new Vuex.Store({
  state: { ... },
  mutations: {
    // 我们可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名
    [SOME_MUTATION] (state) {
      // mutate state
    }
  }
})

vuex提供了辅助函数,为了一个组件获取多个状态,所以在获取状态的时候,都是使用vuex提供的辅助函数 比如mapGetters, mapActions, mapMutations

代码中使用了对象展开运算符

computed: {
  localComputed () { /* ... */ },
  // 使用对象展开运算符将此对象混入到外部对象中
  ...mapGetters({
    // ...
  })
}

项目例子讲解

启动一个REST的api

使用json-server,启动一个REST的api

$ npm install -g json-server

开始Json Server

$ json-server --watch db.json

数据源里面的属性有收藏的文章列表和文章对象

// state.js

import { loadCollectArticles } from 'common/js/cache'

const state = {
  articles: loadCollectArticles(),
  article: {}
}

export default state

其中,收藏的文章列表保存在localStorage里面

// cache.js
import storage from 'good-storage'
const COLLECT_KEY = '__collect__'

export function loadCollectArticles() {
  return storage.get(COLLECT_KEY, [])
}

这是vuex的Store的相关配置,其中的strict是严格模式

在严格模式下,无论何时发生了状态变更且不是由 mutation 函数引起的,将会抛出错误。这能保证所有的状态变更都能被调试工具跟踪到。

而插件createLogger是日志插件用于调试,主要是mutaions的操作调试

// index.js
// 判断当前环境是开发环境还是生产环境
...
import createLogger from 'vuex/dist/logger'
const debug = process.env.NODE_ENV !== 'production'

export default new Vuex.Store({
  state,
  getters,
  actions,
  mutations,
  strict: debug,
  plugins: debug ? [createLogger()] : []
})

使用mapGetters来获取state派生出来的状态,来获取收藏列表的文章

// HelloWrold.vue
...
import { mapGetters, mapActions, mapMutations } from 'vuex'
...

computed: {
  ...mapGetters([
    'articles'
  ])
}

因为保存收藏的文章的进度,我们不知道,所以不用MapMutations(同步),用mapActions来保存收藏文章的进度

// HelloWorld.vue
...
methods: {
    ...mapActions([
      'saveArticle'
    ])
}
...

actiondemo's People

Watchers

Bell avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.