omi/packages/omiv
dntzhang 375c07e0ab publish(omiv): v1.0.6 2020-06-24 15:16:23 +08:00
..
config publish(omiv): v1.0.4, fix build script 2019-11-19 13:16:19 +08:00
dist publish(omiv): v1.0.6 2020-06-24 15:16:23 +08:00
examples chore(omiv): 更新 pmiv-examples 内容 2019-11-29 19:37:01 +08:00
src publish(omiv): v1.0.6 2020-06-24 15:16:23 +08:00
test test(omiv): add test case to make vue.js has 100% coverage 2019-11-17 21:22:17 +08:00
.huskyrc feat(omiv): add husky 2019-11-11 13:50:13 +08:00
README.md docs: update readme of omiv 2020-04-02 11:30:04 +08:00
package.json publish(omiv): v1.0.6 2020-06-24 15:16:23 +08:00

README.md

Omiv

1kb store system for Vue apps.

Usage

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <HelloWorld msg="Welcome to Your Vue + Omiv App" />
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
  components: {
    HelloWorld
  }
}
</script>

HelloWrold.vue:

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <button @click="$store.sub">-</button>
    <span>{{$state.count}}</span>
    <button @click="$store.add">+</button>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  useSelf: ['count']
  //or, use will update all the children components 
  //use: ['count']
}
</script>

Store injection:

import Vue from 'vue'
import Omiv, { render } from 'omiv'
import App from './App.vue'

Vue.use(Omiv)

const store = new class {
  data = {
    count: 1
  };
  sub = () => {
    this.data.count--
  };
  add = () => {
    this.data.count++
  }
}

render(App, '#app', store)

You can pass options to Vue through the fourth parameter:

render(App, '#app', store, {
  router
})

Multi-store injection

Injecting multiple stores by render method:

const cs = new class {
  data = {
    count: 1
  }
  sub = () => {
    this.data.count--
  }
  add = () => {
    this.data.count++
  }
}

const ns = new class {
  data = {
    name: 'omiv'
  }
  rename = () => {
    this.data.name = 'omiv + vue'
  }
}

render(App, '#app', { cs, ns })

HelloWorld.vue:

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <button @click="$store.cs.sub">-</button>
    <span>{{$state.cs.count}}</span>
    <button @click="$store.cs.add">+</button>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  useSelf: {
    cs: ['count']
  }
}
</script>

Computed

render(App, '#app', new class {
  data = {
    count: 1
  }
  sub = () => {
    this.data.count--
  }
  add = () => {
    this.data.count++
  }
  //look here
  getDoubleCount = () => {
    return this.data.count * 2
  }
})

Bind getDoubleCount to template:

<div>double: {{$store.getDoubleCount()}}</div>

Differences to Vuex

Vuex:

data.items[1] = 'x' // is NOT reactive
data.items.length = 2 // is NOT reactive

Omiv:

data.items[1] = 'x' // is reactive
data.items.size(2)  // is reactive

License

MIT © Tencent