项目作者: gabrielmbmb

项目描述 :
💾🔗🖥️ Share, synchronize and persist state between multiple tabs with this plugin for Vuex. TypeScript types included.
高级语言: TypeScript
项目地址: git://github.com/gabrielmbmb/vuex-multi-tab-state.git
创建时间: 2020-02-15T15:16:42Z
项目社区:https://github.com/gabrielmbmb/vuex-multi-tab-state

开源协议:MIT License

下载


vuex-multi-tab-state

Tests
npm
codecov
codebeat badge
npm
npm bundle size
npm type definitions
code style: prettier
demo

This Vuex plugin allows you to sync and share the status of your Vue application
across multiple tabs or windows using the local storage.

This repository has a gitter chat where you can ask questions and propose new features:

Gitter

Vue 3 and Vuex 4 compatibility :warning:

The plugin has been tested with Vue 3 and Vuex 4 and no problems have been found.
There is an issue
where you can find how the plugin has been used using the new Vue 3 Composition
API. If you encounter a problem using the plugin with Vue 3 and Vuex 4, please
post it there.

Installation

vuex-multi-tab-state is available in npm and can be installed with the following command:

  1. npm i vuex-multi-tab-state

Usage

Just import vuex-multi-tab-state and add it in the plugins list of your Vuex Store object.

  1. import Vue from 'vue';
  2. import Vuex from 'vuex';
  3. import createMultiTabState from 'vuex-multi-tab-state';
  4. Vue.use(Vuex);
  5. export default new Vuex.Store({
  6. state: { ... },
  7. mutations: { ... },
  8. actions: { ... },
  9. getters: { ... },
  10. plugins: [
  11. createMultiTabState(),
  12. ],
  13. });

You can check the example provided here

NuxtJS

Integrating the plugin in NuxtJS requires a little more effort than in Vue. First
of all, we have to create a file inside the plugins directory.

  1. // ~/plugins/multiTabState.client.js
  2. import createMultiTabState from 'vuex-multi-tab-state';
  3. export default ({ store }) => {
  4. createMultiTabState()(store);
  5. };

Note that the filename must have the following format *.client.js. The next
step is to add this plugin to NuxtJS in nuxt.config.js:

  1. // nuxt.config.js
  2. export default {
  3. ...
  4. plugins: [{ src: '~/plugins/multiTabState.client.js' }],
  5. ...
  6. }

If you didn’t name the file according to the specified format, you can add the
plugin this way:

  1. // nuxt.config.js
  2. export default {
  3. ...
  4. plugins: [{ src: '~/plugins/multiTabState.client.js', mode: 'client' }],
  5. ...
  6. }

Both ways tell NuxtJS that the plugin should only be run client-side
(because the plugin uses window, not available server-side).

API

createMultiTabState({options})

Creates a new instance of the plugin with the given options. The possible options
are as follows:

  • statesPaths Array<String>: contains the name of the states to be synchronized
    with dot notation. If the param is not provided, the whole state of your app will
    be sync. Defaults to [].

    Example: Only the oranges will be synchronized.

    1. export default new Vuex.Store({
    2. state: {
    3. fruits: {
    4. oranges: 0,
    5. apples: 0,
    6. },
    7. },
    8. plugins: [createMultiTabState({
    9. statesPaths: ['fruits.oranges'],
    10. })],
    11. });
  • key? <String>: key of the local storage in which the state will be stored.
    Defaults to 'vuex-multi-tab'.

  • onBeforeReplace? <Function>: hook function that receives the state and allows to modify it before replacing it. The function can return either a modified state or a falsy value (which means that no modifications has been done inside the hook).
  • onBeforeSave? <Function>: hook function that receives the state and allows to modify it before saving it in local storage. The function can return either a modified state or a falsy value (which means that no modifications has been done inside the hook).

    1. export default new Vuex.Store({
    2. state: {
    3. fruits: {
    4. oranges: 0,
    5. apples: 0,
    6. },
    7. },
    8. plugins: [createMultiTabState({
    9. statesPaths: ['fruits.oranges'],
    10. onBeforeSave: (state) => {
    11. // Modify state here
    12. return state;
    13. },
    14. onBeforeReplace: (state) => {
    15. // Modify state here
    16. return state;
    17. }
    18. })],
    19. });

Test

The tests have been written with mocha and chai.

  1. npm install
  2. npm run test

Collaborate

npm collaborators

If you feel that something can be improved, go on, create a pull request! Please
follow the programming style and document your changes correctly.

License

NPM

This project is under the MIT license. More information here.