A Vue plugin to sync router queries and hash with state.
This plugin requires that your project use Vue Router in history mode
Check out the demo
- Why?
- Install
- Examples
- Hashes vs Queries
- Global Mixins
- Scripts
- How to Contribute
- License
Allow users to deep link (bookmark) to state outside of normal routing such as:
- filtered lists
- searched lists
- modal states
- tabs
- etc.
yarn add -D vue-url-state-sync
# or
npm i -D vue-url-state-sync
import Vue from 'vue';
import VueRouter from 'vue-router';
import VueUrlStateSync from 'vue-url-state-sync';
Vue.use(VueRouter);
Vue.use(VueUrlStateSync);
const router = new Router({
mode: 'history', // must be in history mode for hashes to work!
routes: [...]
});
const app = new Vue({
router
}).$mount('#app')
<template>
<input v-model="term" type="text" placeholder="Search term" />
</template>
<script>
export default {
name: 'Users',
data() {
return {
showModal: false,
searchTerm: 'foobar'
};
},
beforeMount() {
this.$vuss.h.sync('showModal');
this.$vuss.q.sync('searchTerm');
/**
* Resulting URL
* ?search_term=foobar#show-modal
*/
}
};
</script>
import { mapState } = 'vuex';
export default {
name: 'Users',
computed: {
...mapState('users', ['showModal'])
},
beforeMount() {
// define a callback for when the hash changes
this.$vuss.h.sync('showModal', 'showAddUserModal', (newVal) => {
this.$store.commit('users/setShowModal', newVal);
});
}
};
export default {
name: 'Users',
computed: {
showModal: {
get() {
return this.$store.state.users.showModal;
},
set(newVal) {
this.$store.commit('users/setShowModal', newVal);
}
}
},
beforeMount() {
this.$vuss.h.sync('showModal', 'showAddUserModal');
}
};
export default {
name: 'Users',
data() {
return {
list: {
filters: {
animal: 'dog',
color: 'red',
gender: 'male'
}
}
};
},
beforeMount() {
this.$vuss.q.sync('list.filters');
/**
* Resulting URL
* ?list_filters=%7B"animal"%3A"dog","color"%3A"red","gender"%3A"male"%7D
*/
this.$vuss.q.sync('list.filters', 'filters');
/**
* Resulting URL
* ?filters=%7B"animal"%3A"dog","color"%3A"red","gender"%3A"male"%7D
*/
}
};
Hashes is intended to be used for simple boolean state, such as whether or not to show a modal. It is always converted to kebab-case
.
export default {
// data
data() {
return {
modal: true
};
},
// sync
beforeMount() {
// you can define both the state key and the hash value if you wish
this.$vuss.h.sync('modal', 'showModal');
// optional - define a callback for when the hash changes
this.$vuss.h.sync('modal', 'showModal', newVal => {
this.modal = newVal;
});
}
};
// result
('https://mysite.com#show-modal');
Queries is intended for data such as search terms, filters, current tab, etc. It is stringified on write, and parsed on read. Keys are always converted to snake_case
.
export default {
// data
data() {
return {
filters: {
animal: 'dog',
color: 'red',
gender: 'male'
}
};
},
// sync
beforeMount() {
// you can define both the state key and the query key if you wish
this.$vuss.q.sync('filters', 'f');
// optional - define a callback for when the query changes
this.$vuss.h.sync('filters', 'f', newVal => {
this.filters = { ...newVal };
});
}
};
// result
('https://mysite.com?f=%7B"animal"%3A"dog","color"%3A"red","gender"%3A"male"%7D');
Two global mixins are installed by this plugin - one for syncing hash and one for syncing query queries.
Provides hash
- Returns
{string}
vm.$hash = 'show-modal'
Provides query
- Returns
{object}
{
term: 'foobar'
}
Clears hash
- Returns
{void}
Sets hash
- Arguments
{string} hash
{boolean} [replaceHistory=false]
-- if true, a new history entry will not be added to the navigation stack
- Returns
{void}
Syncs the hash with specific component state.
When first called, it will sync the current hash to the state OR state to hash, in that order. It then sets up two watchers -- one for hash changes and one for state changes.
- Arguments
{string} state
{string} [hash]
-- if not passed, is set to the value ofstate
{function} [onHashChange]
- Called when Vue Router hash change detected
- Arguments
{*} newVal
- Returns
{void}
Clears query
- Returns
{void}
Provides if query key exists
- Arguments
{string} key
- Returns
{boolean}
Updates query query object.
- Arguments
{object} query
{function} [next=() => {}]
{boolean} [replaceHistory=false]
-- if true, a new history entry will not be added to the navigation stack
- Returns
{void}
Removes query value by key
- Arguments
{object} query
- Returns
{void}
Sets query value by key
- Arguments
{string} key
{*} value
{boolean} [replaceHistory=false]
-- if true, a new history entry will not be added to the navigation stack
- Returns
{void}
Syncs a query key with specific component state.
When first called, it will sync the current query to the state OR state to query, in that order. It then sets up two watchers -- one for query changes and one for state changes.
- Arguments
{string} state
{string} [key]
-- if not passed, is set to the value ofstate
{function} [onQueryChange]
- Called when Vue Router query change detected
- Arguments
{*} newVal
- Returns
{void}
yarn lint
yarn test
yarn build
- Fork the repository
- Create a new branch for each feature or improvement
- Send a pull request from each feature branch to the develop branch