Vue sync-query mixin
vue-sync-query-mixin@1.x
see branchv1
Effortlessly keep local state and
$route.query
in sync for Vue 1.x | Vue 2.x
Intellectual property of Oneway.mobi
Requirement
- Vue 1.x | 2.x
- Vue Router 0.7.x | 2.x
Installation
npm i vue-sync-query-mixin -S
alternatively:<script src="dist/vue-sync-query-mixin.min.js"></script>
which exposes VueSyncQuery
as a global variable
Example
Vue 1.x online example is here (source code is ./example-vue1.html
)
Vue 2.x online example is here (source code is ./example-vue2.html
)
Usage
// This is a Vue component mixins: syncQuery limit: 10 { // `limit` will keep in sync with `$route.query.limit` this } // Vue 1.x can use beforeCompile/compiled/ready // Vue 2.x can use created/beforeMount/mounted
syncQuery
accepts 4 types of argument:
string|string[]
thisthis
object|object[]
thisthis
Magic
More detail in source code
Vue.js officialvm.$watch( expOrFn, callback, [options] )
API documentation is here
{ { // backup the default value const defaultVal = thislocalField // local ==(sync)==> query this // local <==(sync)== query this }}
Notice
local <==(sync)== query
, default type isstring
, or else you need to writeformatter
yourself- default
local2query
andquery2local
shown as below:
/** * default descriptor generator for $watch * @param * @return */ { return localField: field queryField: field local2query: v immediate: false deep: false query2local: v immediate: true deep: false // P.S. watching deep of a string makes no sense }
But they can be function
type, and then we regard them as the formatter
What's more, all the options are merge
not override
, for example:
// ↑ is same as ↓
Build
npm run build