Giter VIP home page Giter VIP logo

stupid-vuex's Introduction

STUPID VUEX

傻瓜式vuex,我心目中真正正宗的vuex。

安装

npm i stupid-vuex

使用

仅支持vue3!

import { createApp } from 'vue'
import { createStore } from 'stupid-vuex'

const app = createApp(...)
const store = createStore({
  some: 1,
})

app.use(store)
app.mount('#root')

在app内的任意组件中,你可以这样使用:

export default {
  computed: {
    some() {
      return this.$state.some
    },
  },
  methods: {
    update() {
      this.$state.some ++
    },
  },
}

或者使用composition api:

<script setup>
import { useState } from 'stupid-vuex'

const $state = useState()
const change = () => {
  $state.a ++
}
</script>

<template>
  <div>
    <span>a: {{$state.a}}</span>
    <button @click="change">change</button>
  </div>
</template>

追踪改动

如果你想知道状态在哪个地方被修改了(一般在开发时才有这个需求),你可以使用下面这个操作:

store.subscribe(({ keyPath, next, prev, stack }) => {
  console.log(keyPath, next, prev)
  console.log(stack) // 需要单独一行才能在浏览器里面显示为可点击的地址
})

你可以通过stack追踪到具体修改状态的那一行代码。

stupid-vuex's People

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.