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
Allow users to deep link (bookmark) to state outside of normal routing such as:
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.
vm.$hash
Provides hash
{string}
vm.$hash = 'show-modal'
vm.$query
Provides query
{object}
{
term: 'foobar'
}
vm.$vuss.h.clear()
Clears hash
{void}
vm.$vuss.h.set(hash, replaceHistory)
Sets hash
{string} hash
{boolean} [replaceHistory=false]
– if true, a new history entry will not be added to the navigation stack{void}
vm.$vuss.h.sync(state, hash, onHashChange)
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.
{string} state
{string} [hash]
– if not passed, is set to the value of state
{function} [onHashChange]
{*} newVal
{void}
vm.$vuss.q.clear()
Clears query
{void}
vm.$vuss.q.exists(key)
Provides if query key exists
{string} key
{boolean}
vm.$vuss.q.push(hash)
Updates query query object.
{object} query
{function} [next=() => {}]
{boolean} [replaceHistory=false]
– if true, a new history entry will not be added to the navigation stack{void}
vm.$vuss.q.remove(key)
Removes query value by key
{object} query
{void}
vm.$vuss.q.set(key, value, replaceHistory)
Sets query value by key
{string} key
{*} value
{boolean} [replaceHistory=false]
– if true, a new history entry will not be added to the navigation stack{void}
vm.$vuss.q.sync(state, key, onQueryChange)
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.
{string} state
{string} [key]
– if not passed, is set to the value of state
{function} [onQueryChange]
{*} newVal
{void}
yarn lint
yarn test
yarn build