Giter Club home page Giter Club logo

msg-system-with-vue's Introduction

msg-system-with-vue

一个简单的消息管理系统

命名空间

//摘自官网

// types.js

// 定义 getter、action、和 mutation 的名称为常量,以模块名 `todos` 为前缀
export const DONE_COUNT = 'todos/DONE_COUNT'
export const FETCH_ALL = 'todos/FETCH_ALL'
export const TOGGLE_DONE = 'todos/TOGGLE_DONE'
// modules/todos.js
import * as types from '../types'

// 使用添加了前缀的名称定义 getter、action 和 mutation
const todosModule = {
  state: { todos: [] },

  getters: {
    [types.DONE_COUNT] (state) {
      // ...
    }
  },

  actions: {
    [types.FETCH_ALL] (context, payload) {
      // ...
    }
  },

  mutations: {
    [types.TOGGLE_DONE] (state, payload) {
      // ...
    }
  }
}

以及@jxlarrea的命名空间方法

// namespace.js helper utility

function mapValues (obj, f) {
    const res = {}
    Object.keys(obj).forEach(key => {
        res[key] = f(obj[key], key)
    })
    return res
}

export default (module, types) => {
    let newObj = {};

    mapValues(types, (names, type) => {       
        newObj[type] = {};
        types[type].forEach(name=> {
            var newKey = module + ':' + name;             
            newObj[type][name] = newKey;
        });
    })   
    return newObj;
}
// types.js

import namespace from 'utils/namespace'

export default namespace('auth', {
    getters: [
        'user'
    ],
    actions: [
        'fetchUser'
    ],
    mutations: [
        'receiveUser'     
    ]
})
//module.js

import * as accountApi from 'api/account'
import types from './types'

const state = {
    user: null
}

const actions = {
    [types.actions.fetchUser]: context => {
        return accountApi.me().then(response => {
            context.commit(types.mutations.receiveUser, response)
        });
    }
}

const getters = {
    [types.getters.user]: state => state.user
}

const mutations = {
    [types.mutations.receiveUser]: (state, apiResponse) => {
        state.user = apiResponse.payload;
    }
}

export default {
  state,
  actions,
  getters,
  mutations
};

Build Setup

# install dependencies
npm install

# serve with hot reload at localhost:8080
npm run dev

# build for production with minification
npm run build

教程地址

demo GIF

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.