Git Product home page Git Product logo

redtail's Introduction

Redtail

redtail 是一个简化vuex概念和接口的插件, redtail只有两个接口, 可以更方便的接入vuex

安装

npm i redtail

使用

新建一个组件

import {init} from 'redtail';
const store = init(new Vuex.Store({
    state: {
        message: 'Hello!'
    },
    // 直接书写异步代码
    setMessageAsync (context, newValue) {
        setTimeout(() => {
            context.commit('message', newValue)
        });
    }
}));

绑定store到组件

<template>
    <div id="app">
        <p>{{ message }}</p>
    </div>
</template>
<script>
    import {connet} from 'redtail';
    new Vue(connet({
        el: '#app',
        methods: {
            setMessage () {
                store.commit('setMessageAsync', 'World!')
            }
        }
    }));
</script>

对应原方式

完整的vuex实例应该是

const store = new Vuex.Store({
    state: {
        message: 'Hello!'
    },
    // getter 集合
    getters: {
        getMessage: state => {
            return state.message;
        }
    },
    // setter 集合
    mutations: {
        setMessage (state, newValue) {
            // 变更状态
            state.message = newValue;
        }
    },
    actions: {
        setMessageAsync (context, newValue) {
            setTimeout(() => {
                context.commit('setMessage', newValue)
            });
        }
    }
});

最后把store和组件绑定

<template>
    <div id="app">
        <p>{{ message }}</p>
    </div>
</template>
<script>
    new Vue({
        el: '#app',
        computed: {
            ...mapGetters([
                'getMessage',
                // ...
            ])
        },
        methods: {
            setMessage () {
                store.commit('setMessageAsync', 'World!')
            }
        }
    });
</script>

redtail's People

Contributors

luvebugs avatar

Watchers

 avatar

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.