# vx-refactor

> 【vx-refactor】简化原生 vuex 的复杂操作.

Latest version **1.0.5** (published 2024-01-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install vx-refactor
pnpm add vx-refactor
yarn add vx-refactor
bun add vx-refactor
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2024-01-23 |
| First published | 2020-12-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 18.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Pengo |
| Maintainers | pengo.c |
| Keywords | vue, vuex, vuex工具, refactor, vx-refactor, 按需加载, 面向对象调用, 私有模块处理 |

## Links

- npm: https://www.npmjs.com/package/vx-refactor
- Homepage: https://github.com/cpg0525/vx-refactor
- Issues: https://github.com/cpg0525/vx-refactor/issues
- npm.io page: https://npm.io/package/vx-refactor

## Dependencies (2)

- [shvl](https://npm.io/package/shvl.md) ^2.0.3
- [deepmerge](https://npm.io/package/deepmerge.md) ^4.2.2

## Recent versions

- 1.0.5 (latest) — 2024-01-23
- 1.0.4 — 2024-01-23
- 1.0.3 — 2020-12-14

## README

- ### vx-refactor 由来

  <p style="color: #333333;font-size: 1em;font-family: sans-serif;font-weight: 600;margin-bottom:0.8em">命名:</p>
   
   <p style="color: grey;font-size: 1em;font-family: sans-serif;font-weight: 600;margin-bottom:0.8em"><mark style="font-size:1.5em">vx</mark> 为vuex的读音缩写。refactor 意为重构。整合起来，就是对原生vuex的重构。</p>

  <p style="color: #333333;font-size: 1em;font-family: sans-serif;font-weight: 600;margin-bottom:1.5em">解决的问题:</p>

  | 注入方式                                                                       | 调用方式                                                            |
  | :----------------------------------------------------------------------------- | :------------------------------------------------------------------ |
  | 原生 vuex 需大量注入模块                                                       | 原生 vuex 调用 action & mutation 需配合 dispatch & commit 等方法    |
  | <p><mark>vx-refactor</mark> 只需调用 <mark>connect</mark> 方法便可自动注入</p> | <mark>vx-refactor</mark> 面向对象调用。如：models.setFn[调用的方法] |

- ### 安装

  <p style="color: #000000;font-size: 1em;font-family: sans-serif;font-weight: 600">$ npm install vx-refactor or yarn add vx-refactor；</p>

- ### 使用方法

   <p style="color: grey;font-size: 0.8em;font-family: sans-serif;font-weight: 600;margin-bottom:0.8em">示例代码的目录如下：</p>

  ├── index.scss
  ├── index.vue
  └── model.js

  <p style="color: grey;font-size: 1em;font-family: sans-serif;font-weight: 600;margin-top:1em;margin-bottom:0.8em">1. 在 main.js 中注入 store 实例。</p>

  ```javascript
  import vxRefactor from "vx-refactor";

  vxRefactor(store);
  ```

  <p style="color: grey;font-size: 1em;font-family: sans-serif;font-weight: 600;margin-bottom:0.8em">2. 在业务 model 中用 connect 方法进行改造。</p>

  ```javascript
  import { connect } from "vx-refactor";

  export const ns = "namespace";
  const state = {
    count: 0,
  };

  const mutations = {
    addCount(state, payload) {
      state.count = payload;
    },
  };

  const actions = {
    setCount({ commit }, params) {
      commit("addCount", params);
    },
  };

  const getters = {
    getCount(state) {
      return state.count;
    },
  };

  export default connect({
    ns,
    state,
    mutations,
    actions,
    getters,
  });
  ```

  <p style="color: grey;font-size: 1em;font-family: sans-serif;font-weight: 600;margin-bottom:0.8em">3. 在业务页面中调用。</p>

  ```javascript
  import models, {ns} from './model'; // 引入当前业务所在的model

   computed: {
      ...mapState(ns, ['count'])
   },

   methods: {
      introduce() {
        // 支持选传第二个参数options。允许在命名空间模块里分发根的 action
        models.setCount(2) || models.setCount(2, {root: true});
        models.commit.addCount(3）|| models.commit.addCount(3, {root: true});
      }
   }

  ```

  <mark style="font-size:16px;font-weight:600">写在最后：</mark>
  <p style="font-size: 14px">使用vuex，在模块注入及调用上需要遵守vuex约定的方式。eg：this.$store.commit or this.$store.dispatch。而vx-refactor只关注模块中的方法，直接采用内置暴露对象models进行调用。eg: models.setFn。vx-refactor中connect方法实现了真正的按需引入。换句话说：不论你的model层写在何处，只要你调用connect方法对你的model进行处理。那么它将自动引入到store中进行管理。且对于命名空间需手动开启namespaced: true 引入时需再次为模块的具名。而vx-refactor只需在模块中export一个空间名称即可实现全局或私有模块的识别并支持分别调用。使其实现真正意义上的“模块化”。</p>

---
_Source: https://npm.io/package/vx-refactor · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
