项目作者: holamundo-app

项目描述 :
Vue mixin for binding a component's data property with vue-router query strings
高级语言: JavaScript
项目地址: git://github.com/holamundo-app/vue-router-qs-binder.git
创建时间: 2019-02-28T21:17:51Z
项目社区:https://github.com/holamundo-app/vue-router-qs-binder

开源协议:MIT License

下载


vue-router-qs-binder

@holamundo.app/vue-router-qs-binder"">NPM Version Netlify Status

A library that simplifies binding a component’s data property with vue-router query strings.

Installation

  1. $ yarn add @holamundo.app/vue-router-qs-binder

:warning: Note: Vue-router is a required peerDependency, make sure you install it if you haven’t already done so, eg. via the Vue CLI.

Usage

Mixin parameters

data (required)

Type: Object | function(route)

An object of the initial data for the mixin, similar to setting initial data for a Vue component via the returned value in the data() method.

A function can also be passed here, but it must return an Object. The function receives this.$route as it’s only parameter, allowing you to customize the mapping of query parameters manually.

Example:

url: https://example.com/?name=Tom&page=2

  1. mixins: [
  2. qsBinder({
  3. dataKey: 'search',
  4. data: ({ query }) => {
  5. const { name = '', page = 1 } = query,
  6. return {
  7. name,
  8. page
  9. }
  10. }
  11. })
  12. ]

dataKey (required)

Type: string

The key to be used for saving the data parameter into the components data.

debounceLength

Type: number
Default: 0

The URL parameter changes everytime the data updates, meaning a new history entry for each keystroke in an input for example.

You can increase the debounceLength (milliseconds) to adjust how often the URL is updated.

didUpdate

Type: function(Instance)

A method fired when data‘s contents update (basically a Vue watch).

Example:

  1. mixins: [
  2. qsBinder({
  3. dataKey: 'search',
  4. data: {
  5. name: ''
  6. },
  7. didUpdate: ($this) => {
  8. $this.runSearch()
  9. }
  10. })
  11. ]

updateDataMethodName

Type: string
Default: updateQsData

Name of the “update method” added to the component where the mixin has been initiated. Useful for when using multiple instances of the mixin in a single component.

API

updateQsData

Parameters: (newData: Object, silent: boolean)

Method used to update the data (and update the URL query string), it receives an object parameter with the new data as key/value pairs.

There’s also a second silent boolean parameter, when set to true the URL will not be updated.

Example

  1. <template>
  2. <input type="text" :value="search.name" @change="updateName" />
  3. </template>
  4. <script>
  5. import qsBinder from '@holamundo.app/vue-router-qs-binder';
  6. export default {
  7. name: 'MyComponent',
  8. mixins: [
  9. qsBinder({
  10. dataKey: 'search',
  11. data: {
  12. name: ''
  13. }
  14. })
  15. ],
  16. methods: {
  17. updateName(e) {
  18. this.updateQsData({ name: e.target.value });
  19. }
  20. }
  21. };
  22. </script>

For a more in-depth example see the demos.

Development

  1. # Install dependencies
  2. $ yarn
  3. # Demo environment
  4. $ yarn serve
  5. # Lint & fix files
  6. $ yarn lint
  7. # Build for production
  8. $ yarn build