# @tingyuan/vue-store

> simpler vuex alternative

Latest version **0.0.15** (published 2019-09-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tingyuan/vue-store
pnpm add @tingyuan/vue-store
yarn add @tingyuan/vue-store
bun add @tingyuan/vue-store
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.15 |
| Published | 2019-09-16 |
| First published | 2019-08-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 68.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | tingyuan |
| Maintainers | tingyuan |
| Keywords | vuex, vue, store, state, management, redux, flux, reducer |

## Links

- npm: https://www.npmjs.com/package/@tingyuan/vue-store
- Repository: https://github.com/lovetingyuan/simple-vuex
- Homepage: https://github.com/lovetingyuan/vue-store#readme
- Issues: https://github.com/lovetingyuan/vue-store/issues
- npm.io page: https://npm.io/package/@tingyuan/vue-store

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 0.0.15 (latest) — 2019-09-16
- 0.0.14 — 2019-09-16
- 0.0.13 — 2019-08-28
- 0.0.12 — 2019-08-26
- 0.0.11 — 2019-08-26
- 0.0.10 — 2019-08-26
- 0.0.9 — 2019-08-26
- 0.0.8 — 2019-08-21
- 0.0.7 — 2019-08-20
- 0.0.6 — 2019-08-19
- 0.0.5 — 2019-08-18
- 0.0.4 — 2019-08-18
- 0.0.3 — 2019-08-08
- 0.0.2 — 2019-08-07
- 0.0.1 — 2019-08-07

## README

# vue-store
*still developing...*

[![npm version](https://img.shields.io/npm/v/@tingyuan/vue-store.svg)](https://www.npmjs.com/package/@tingyuan/vue-store)

a simpler way to use redux-like state management in vue project

* Simpler way to share and change your application state, NO ACTION_TYPE, commit, dispatch, helpers...
* Better and natural support for typescript
* Small size but with adequate support

### install
```bash
npm install @tingyuan/vue-store
```
or
```html
<script src="https://cdn.jsdelivr.net/npm/@tingyuan/vue-store">
/* export "VueStore" as global name. */
</script>
```

### example
`store.ts`
```typescript
type Item = { text: string, done: boolean }

const todoModule = {
  list: [] as Item[],
  get doneCount() {
    return this.list.filter(v => v.done).length
  },
  setList(list: Item[]) {
    this.list = list
  },
  addItem(text: string) {
    this.list.push({
      text, done: false
    })
  },
  async $fetchList() {
    const list = await request('/api/get-list')
    this.setList(list)
  }
}

const modules = {
  user: {
    name: 'nobody'
  },
  setUserName(username: string) {
    this.user.name = username
  },
  Todo: todoModule
}

import Vue from 'vue'
import VueStore from '@tingyuan/vue-store'

Vue.use(VueStore)

const store = VueStore.createStore(modules, {
  strict: true,
  plugins: [
    store => {
      store.subscribe(({ type, actionType, payload }, state) => {
        if (type) {
          console.log('mutation called: ' + type)
        } else {
          console.log('action called: ' + actionType)
        }
      })
    }
  ]
})

export default store
```

```html
<template>
  <div>
    <h1>Hello {{$store.user.name}}, Todo List({{counter}})</h1>
    <input type="text" placeholder="enter whatever" v-model.trim="newItem" @keyup.enter="onAdd">
    <ol>
      <li v-for="item in todoList" :key="item.id" :class="item.done ? 'done' : ''">{{item.text}}</li>
    </ol>
  </div>
</template>
<script>
export default {
  data() {
    return { newItem: '' }
  },
  computed: {
    todoList() { return this.$store.Todo.list },
    counter() { return this.$store.Todo.doneCount + '/' + this.todoList.length }
  },
  methods: {
    onAdd() {
      this.newItem && this.$store.Todo.addItem(this.newItem)
    }
  },
  created() {
    this.$store.Todo.$fetchList()
  }
}
</script>
<style>
.done { text-decoration: line-through; }
</style>
```

### declare `store` option

```typescript
import store from './store'
import Vue, { ComponentOptions } from 'vue'

declare module 'vue/types/options' {
  interface ComponentOptions<V extends Vue> {
    store?: typeof store
  }
}

declare module 'vue/types/vue' {
  interface Vue {
    $store: typeof store
  }
}
```

### api
* `store = VueStore.createStore(modules, options)`
* `store.watch(getter, callback)`
* `store.subscribe(listener)`
* `store.addModule(modulePath, module, options)`
* `store.removeModule(modulePath)`
* `store.replaceState(newState)`
* `store.getState()`
* `store.hotUpdate(path, module)`

### convention(compulsory in fact)
1. name of sub-module(namespace) starts with **capital letter**
2. name of action method(with side-effect) starts with **'$'**
3. getter property will be taken as 'getters'

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