{"id":39247931,"url":"https://github.com/syntaxe3/vuex-util","last_synced_at":"2026-01-18T00:00:09.411Z","repository":{"id":95190468,"uuid":"94427348","full_name":"Syntaxe3/vuex-util","owner":"Syntaxe3","description":"vuex 和restful的一些action，mutation精简","archived":false,"fork":false,"pushed_at":"2017-07-31T03:56:35.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-01-12T18:32:59.887Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Syntaxe3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2017-06-15T10:11:43.000Z","updated_at":"2017-10-18T06:00:25.000Z","dependencies_parsed_at":"2023-06-12T09:45:38.830Z","dependency_job_id":null,"html_url":"https://github.com/Syntaxe3/vuex-util","commit_stats":null,"previous_names":["syntaxe3/vuex-util"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/Syntaxe3/vuex-util","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Syntaxe3%2Fvuex-util","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Syntaxe3%2Fvuex-util/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Syntaxe3%2Fvuex-util/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Syntaxe3%2Fvuex-util/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Syntaxe3","download_url":"https://codeload.github.com/Syntaxe3/vuex-util/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Syntaxe3%2Fvuex-util/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28522999,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T23:53:28.710Z","status":"ssl_error","status_checked_at":"2026-01-17T23:52:20.131Z","response_time":85,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-18T00:00:04.374Z","updated_at":"2026-01-18T00:00:09.338Z","avatar_url":"https://github.com/Syntaxe3.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vuex-util\n\n\n通常呢一个state会有以下的结构\n\n\nstate\n\n```javascript\n\nconst state = {\n  posts: [],\n  post: {}\n}\n\n```\n\n\nmutations\n\n```javascript\nconst mutations = {\n  [CLEAR_POSTS]: (state) {\n    state.posts = []\n  },\n  [GET_POST_SUCCESS] (state, newPost) {\n    state.post = newPost\n  },\n  [GET_MORE_POSTS_SUCCESS] (state, posts) {\n    state.posts = [...state.posts, ...posts]\n  }\n}\n```\n\n\nactions\n\n```javascript\nconst actions = {\n  [GET_POST] ({ commit }, payload) {\n    fetch(something).then(() =\u003e { \n      commit(GET_POST_SUCCESS)\n    })\n  },\n  [GET_MORE_POST] ({ commit }, payload) {\n    fetch(otherthing).then((json) =\u003e {\n      commit(GET_MORE_POSTS_SUCCESS, json)\n    }\n  }\n}\n```\n\n\n使用以下代码避免重复的state赋值\n\n```javascript\n\n/*\n * @author: chai_xb@163.com\n */\nexport function factoryArray(field) {\n  return function (state) {\n    // 没有参数检测，请确保arguments可用\n    state[field] = [...state[field], ...arguments[1]]\n  }\n}\n\n\nexport function set(field, value) {\n  return function (state) {\n    state[field] = value == null ? arguments[1] : value\n  }\n}\n\nexport function factoryObject(field) {\n  return function (state) {\n    state[field] = { ...state[field], ...arguments[1] }\n  }\n}\n\n\n```\n\n然后mutations就变成了这样\n\n```javascript\nconst mutations = {\n  [CLEAR_POSTS]: set('posts', []),\n  [GET_POST_SUCCESS]: set('post'),\n  // 对象的情况类似\n  [GET_MORE_POSTS_SUCCESS]: factoryArray('posts')\n}\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntaxe3%2Fvuex-util","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntaxe3%2Fvuex-util","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntaxe3%2Fvuex-util/lists"}