项目作者: evantianx

项目描述 :
a msg-system
高级语言: JavaScript
项目地址: git://github.com/evantianx/msg-system-with-vue.git
创建时间: 2016-12-08T12:01:50Z
项目社区:https://github.com/evantianx/msg-system-with-vue

开源协议:

下载


msg-system-with-vue

一个简单的消息管理系统

命名空间

  1. //摘自官网
  2. // types.js
  3. // 定义 getter、action、和 mutation 的名称为常量,以模块名 `todos` 为前缀
  4. export const DONE_COUNT = 'todos/DONE_COUNT'
  5. export const FETCH_ALL = 'todos/FETCH_ALL'
  6. export const TOGGLE_DONE = 'todos/TOGGLE_DONE'
  7. // modules/todos.js
  8. import * as types from '../types'
  9. // 使用添加了前缀的名称定义 getter、action 和 mutation
  10. const todosModule = {
  11. state: { todos: [] },
  12. getters: {
  13. [types.DONE_COUNT] (state) {
  14. // ...
  15. }
  16. },
  17. actions: {
  18. [types.FETCH_ALL] (context, payload) {
  19. // ...
  20. }
  21. },
  22. mutations: {
  23. [types.TOGGLE_DONE] (state, payload) {
  24. // ...
  25. }
  26. }
  27. }

以及@jxlarrea的命名空间方法

  1. // namespace.js helper utility
  2. function mapValues (obj, f) {
  3. const res = {}
  4. Object.keys(obj).forEach(key => {
  5. res[key] = f(obj[key], key)
  6. })
  7. return res
  8. }
  9. export default (module, types) => {
  10. let newObj = {};
  11. mapValues(types, (names, type) => {
  12. newObj[type] = {};
  13. types[type].forEach(name=> {
  14. var newKey = module + ':' + name;
  15. newObj[type][name] = newKey;
  16. });
  17. })
  18. return newObj;
  19. }
  1. // types.js
  2. import namespace from 'utils/namespace'
  3. export default namespace('auth', {
  4. getters: [
  5. 'user'
  6. ],
  7. actions: [
  8. 'fetchUser'
  9. ],
  10. mutations: [
  11. 'receiveUser'
  12. ]
  13. })
  1. //module.js
  2. import * as accountApi from 'api/account'
  3. import types from './types'
  4. const state = {
  5. user: null
  6. }
  7. const actions = {
  8. [types.actions.fetchUser]: context => {
  9. return accountApi.me().then(response => {
  10. context.commit(types.mutations.receiveUser, response)
  11. });
  12. }
  13. }
  14. const getters = {
  15. [types.getters.user]: state => state.user
  16. }
  17. const mutations = {
  18. [types.mutations.receiveUser]: (state, apiResponse) => {
  19. state.user = apiResponse.payload;
  20. }
  21. }
  22. export default {
  23. state,
  24. actions,
  25. getters,
  26. mutations
  27. };

Build Setup

  1. # install dependencies
  2. npm install
  3. # serve with hot reload at localhost:8080
  4. npm run dev
  5. # build for production with minification
  6. npm run build

教程地址

demo GIF