All Projects → ktsn → Vuex Connect

ktsn / Vuex Connect

Licence: mit
A binding utility for a Vue component and a Vuex store.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Vuex Connect

Vue Content Loader
SVG component to create placeholder loading, like Facebook cards loading.
Stars: ✭ 2,790 (+1025%)
Mutual labels:  component
React Loading
Build a smooth and lightweight react component loading with css 🎉 .
Stars: ✭ 234 (-5.65%)
Mutual labels:  component
Autocomplete
Blazing fast and lightweight autocomplete widget without dependencies. Only 1KB gzipped. Demo:
Stars: ✭ 244 (-1.61%)
Mutual labels:  component
Blog
Personal Blog
Stars: ✭ 234 (-5.65%)
Mutual labels:  component
Options Resolver
The OptionsResolver component is array_replace() on steroids. It allows you to create an options system with required options, defaults, validation (type, value), normalization and more.
Stars: ✭ 2,723 (+997.98%)
Mutual labels:  component
Blog
若川的博客—学习源码整体架构系列8篇,前端面试高频源码,微信搜索「若川视野」关注我,长期交流学习~
Stars: ✭ 234 (-5.65%)
Mutual labels:  vuex
Mmf Blog Vue2
mmf-blog vue2.0 (vue2, vue-router, vuex)
Stars: ✭ 232 (-6.45%)
Mutual labels:  vuex
React Native Scalable Image
React Native Image component which scales width or height automatically to keep the original aspect ratio
Stars: ✭ 241 (-2.82%)
Mutual labels:  component
Copilot
Responsive Bootstrap 3 Admin Template based on AdminLTE with vue.js
Stars: ✭ 2,698 (+987.9%)
Mutual labels:  vuex
Vuetimeline
🕵️‍♀️🕵️‍♂️ One easy-to-use component for Vue.js to build beautiful responsive timelines.
Stars: ✭ 242 (-2.42%)
Mutual labels:  component
Vuex Typescript
A simple way to make Vuex type-safe with intuitive intellisense
Stars: ✭ 234 (-5.65%)
Mutual labels:  vuex
React Native Hero
🤘 A super duper easy hero unit react-native component with support for dynamic image, dynamic sizing, color overlays, and more
Stars: ✭ 234 (-5.65%)
Mutual labels:  component
Vuex Along
📝 A plugins to auto save and restore state for vuex
Stars: ✭ 239 (-3.63%)
Mutual labels:  vuex
Vue Element Loading
⏳ Loading inside a container or full screen for Vue.js
Stars: ✭ 234 (-5.65%)
Mutual labels:  component
Full Stack Fastapi Couchbase
Full stack, modern web application generator. Using FastAPI, Couchbase as database, Docker, automatic HTTPS and more.
Stars: ✭ 243 (-2.02%)
Mutual labels:  vuex
Asset
The Asset component manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files.
Stars: ✭ 2,771 (+1017.34%)
Mutual labels:  component
Vue Axios Github
Vue 全家桶 + axios 前端实现登录拦截、登出、拦截器等功能
Stars: ✭ 2,622 (+957.26%)
Mutual labels:  vuex
Vue Firebase Auth Vuex
Vue Firebase🔥 Authentication with Vuex
Stars: ✭ 248 (+0%)
Mutual labels:  vuex
Vuex Mock Store
✅Simple and straightforward Vuex Store mock for vue-test-utils
Stars: ✭ 246 (-0.81%)
Mutual labels:  vuex
Vue Class Store
Universal Vue stores you write once and use anywhere
Stars: ✭ 243 (-2.02%)
Mutual labels:  vuex

vuex-connect

npm version Build Status vuex-connect Dev Token

A binding utility for a Vue component and a Vuex store.
Inspired by react-redux's connect function.

Example

First, create a Vue component. This component can communicate to a parent component using events and recieve data from the parent through props.

// hello-component.js
export default {
  props: {
    message: {
      type: String,
      required: true
    }
  },
  methods: {
    updateMessage(event) {
      this.$emit('update', event.target.value)
    }
  },
  template: `
  <div>
    <p>{{ message }}</p>
    <input type="text" :value="message" @input="updateMessage">
  </div>
  `
}

You can bind the component and the Vuex store by vuex-connect.
The connect function wraps the component, returning a new wrapper component.

import { connect } from 'vuex-connect'
import HelloComponent from './hello-component'

export default connect({
  stateToProps: {
    message: state => state.message
  },

  methodsToEvents: {
    update: ({ commit }, value) => commit('UPDATE_INPUT', value)
  },

  lifecycle: {
    mounted: ({ commit }) => {
      fetch(URL)
        .then(res => res.text())
        .then(value => commit('UPDATE_INPUT', value));
    }
  }
})('hello', HelloComponent)

You can use getters, actions and mutations if you define them in your store.

import { connect } from 'vuex-connect'
import HelloComponent from './hello-component'

export default connect({
  gettersToProps: {
    message: 'inputMessage' // 'prop name': 'getter name'
  },

  mutationsToEvents: {
    update: 'UPDATE_INPUT' // 'event name': 'mutation type'
  },

  lifecycle: {
    mounted: store => store.dispatch('FETCH_INPUT', URL)
  }
})('hello', HelloComponent)

API

connect(options) -> (componentName, Component) -> WrapperComponent

  • options: Object
    • stateToProps
    • gettersToProps
    • actionsToProps
    • actionsToEvents
    • mutationsToProps
    • mutationsToEvents
    • methodsToProps
    • methodsToEvents
    • lifecycle
  • componentName: string
  • Component: Vue component or component option
  • WrapperComponent: Vue component

Connects a Vue component to a Vuex store.

stateToProps, gettersToProps, actionsTo(Props|Events) and mutationsTo(Props|Events) have the same interface as Vuex's mapState, mapGetters, mapActions and mapMutations. In addition, you can define inline methods by using methodsTo(Props|Events).

The options suffixed by Props indicate that they will be passed to the wrapped component's props. For example, the following option retrieves a store's state via the inputMessage getter and passes it to the message prop of the wrapped component.

connect({
  gettersToProps: {
    message: 'inputMessage'
  }
})

The options suffixed by Events indicate that they will listen to the wrapped component's events. For example, the following option observes the update event of the wrapped component and if it is emitted, the UPDATE_INPUT mutation is committed.

connect({
  mutationsToEvents: {
    update: 'UPDATE_INPUT'
  }
})

lifecycle is lifecycle hooks for a Vue component. The lifecycle hooks receive a Vuex store for their first argument. You can dispatch some actions or mutations in the lifecycle hooks.

connect returns another function. The function expects a component name and the component constructor. The component name should be a string. It is useful to specify the component in the debug phase.

createConnect(fn) -> ConnectFunction

Creates a customized connect function. fn is transform function of the wrapper component options and will receive a component options object and a lifecycle option of the connect function. You may want to inject some additional lifecycle hooks in fn.

Example of defining vue-router lifecycle hooks:

// connect.js
import { createConnect } from 'vuex-connect'

export const connect = createConnect((options, lifecycle) => {
  options.beforeRouteEnter = lifecycle.beforeRouteEnter

  options.beforeRouteUpdate = function(to, from, next) {
    return lifecycle.beforeRouteUpdate.call(this, this.store, to, from, next)
  }

  options.beforeRouteLeave = function(to, from, next) {
    return lifecycle.beforeRouteLeave.call(this, this.store, to, from, next)
  }
})

It can be used as:

import { connect } from './connect'
import { Example } from './example'

export default connect({
  lifecycle: {
    beforeRouteEnter(to, from, next) {
      // ...
    },

    beforeRouteUpdate(store, to, from, next) {
      // ...
    },

    beforeRouteLeave(store, to, from, next) {
      // ...
    }
  }
})('example', Example)

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].