Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Effortlessly keep local state and $route.query in sync for Vue 1.x | 2.x

NotificationsYou must be signed in to change notification settings

OneWayTech/vue-sync-query-mixin

Repository files navigation

vue-sync-query-mixin@1.x see branchv1

npm versionnpm download

Effortlessly keep local state and$route.query in sync for Vue 1.x | Vue 2.x
Intellectual property ofOneway.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 exposesVueSyncQuery as a global variable

Example

Vue 1.x online example ishere (source code is./example-vue1.html)
Vue 2.x online example ishere (source code is./example-vue2.html)

Usage

// This is a Vue componentimportsyncQueryfrom'vue-sync-query-mixin'exportdefault{mixins:[syncQuery],data:()=>({limit:10}),ready(){// `limit` will keep in sync with `$route.query.limit`this.syncQuery('limit')}// Vue 1.x can use beforeCompile/compiled/ready// Vue 2.x can use created/beforeMount/mounted}

syncQuery accepts 4 types of argument:

  • string|string[]
this.syncQuery('limit')this.syncQuery(['limit'])
  • object|object[]
this.syncQuery({localField:'limit',queryField:'limit',local2query:{formatter:v=>v,immediate:false,deep:false},query2local:{formatter:v=>v,immediate:true,deep:false}})this.syncQuery([{localField:'limit',queryField:'limit',local2query:{formatter:v=>v,immediate:false,deep:false},query2local:{formatter:v=>v,immediate:true,deep:false}}])

Magic

More detail insource code
Vue.js officialvm.$watch( expOrFn, callback, [options] ) API documentation ishere

_syncQuery({ localField, queryField, local2query, query2local}){(()=>{// backup the default valueconstdefaultVal=this[localField]// local ==(sync)==> querythis.$watch(localField,function(v,oldV){this.updateQuery({[queryField]:local2query.formatter(v,oldV)})},local2query)// local <==(sync)== querythis.$watch(`$route.query.${queryField}`,function(v,oldV){this[localField]=query2local.formatter(v,oldV)||defaultVal},query2local)})()}

Notice

  • local <==(sync)== query, default type isstring, or else you need to writeformatter yourself
  • defaultlocal2query andquery2local shown as below:
/** * default descriptor generator for $watch *@param  {String} field *@return {Object} */functiondefaultDescGen(field){return{localField:field,queryField:field,local2query:{formatter:v=>v,immediate:false,deep:false},query2local:{formatter:v=>v,immediate:true,deep:false// P.S. watching deep of a string makes no sense}}}

But they can befunction type, and then we regard them as theformatter
What's more, all the options aremerge notoverride, for example:

syncQuery({localField:'limit',queryField:'limit',local2query:{immediate:true},query2local:v=>+v// string to number})// ↑ is same as ↓syncQuery({localField:field,queryField:field,local2query:{formatter:v=>v,immediate:true,// merge!deep:false},query2local:{formatter:v=>+v,// merge!immediate:true,deep:false}})

Build

npm run build

About

Effortlessly keep local state and $route.query in sync for Vue 1.x | 2.x

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp