UNPKG

vue-router-sync

Version:

Keep vm data and router state in sync.

50 lines (42 loc) 1.41 kB
'use strict'; var index = function (Vue) { Vue.mixin({ mounted: function mounted() { var this$1 = this; var ref = this.$options; var queryMap = ref.syncDataRouter; if (queryMap) { if (!this.$router) { throw new Error('You need to use vue-router!') } var dataKeysThatDiffFromRouter = Object.keys(queryMap) .filter(function (dataKey) { var queryKey = queryMap[dataKey]; return this$1[dataKey] && this$1[dataKey] !== this$1.$route.query[queryKey] }); if (dataKeysThatDiffFromRouter.length > 0) { var newQuery = dataKeysThatDiffFromRouter.reduce(function (res, dataKey) { var queryKey = queryMap[dataKey]; res[queryKey] = this$1[dataKey]; return res }, {}); this.$router.push({ query: Object.assign({}, this.$route.query, newQuery) }); } var loop = function ( dataKey ) { var queryKey = queryMap[dataKey]; this$1.$watch(dataKey, function (newValue) { this$1.$router.push({ query: ( obj = Object.assign({}, this$1.$route.query), obj[queryKey] = newValue, obj ) }); var obj; }); }; for (var dataKey in queryMap) loop( dataKey ); } } }); }; module.exports = index;