Code Monkey home page Code Monkey logo

vue-query-param's Introduction

vue-query-param

Introduction

vue-query-param is a vue directive that binds and syncs a ref model to the URL's query paramaters.

vue-query-param(2)

Installation

npm i vue-query-param
yarn add vue-query-param

Basic Usage

<script setup lang="ts">
import { vQueryParam } from "vue-query-param";

const filter = ref("")
</script>

<template>
  <input v-model="filter" v-query-param:filter="filter" />
</template>

Hook Arguments

Directive argument

arg must be the key of the model in the URL, for example, this code:

<input v-model="search" v-query-param:q="search" />
<input v-model="filter" v-query-param:filter="filter" />

Will produce this URL https://<domain>/<subpath>/?q=<value>&filter=<value>

Directive value

In the most basic cases you only need to use a ref directly in the directive value but for more complex cases you can use an object with the following options:

{
    model: ref_property,
    format: (val) => val.toString(),
    callback: (val) => ref_property = val
}

model

The ref field that v-query-param will bind on the URL.

format

A function to format the model value, useful when the value is not a primitive type or just masking the value when is set in the URL.

callback

A function that is called during the created hook, use this if you want to set at your ref field the current URL value before mounting the template.

Examples

A common use case could be a search input, usually you want to store in URL the user's input, so when the page is reloaded you still have the user's input.

<script setup lang="ts">
import { vQueryParam } from "vue-query-param";

const search = ref("")
</script>

<template>
  <input v-model="search" v-query-param:q="search" />
</template>

For complex types as dates, you can use the format and callback options so you can store in URL the date in a specific format and retrieve it again during the created hook by parsing the formatted value.

<script setup lang="ts">
import { vQueryParam } from "vue-query-param";

const date = ref()
</script>

<template>
  <div>
    <VueDatePicker v-query-param:datefrom="{
      model: date,
      format: (val: Date) => val.toLocaleDateString(),
      callback: (val: string) => date = new Date(val)
    }" v-model="date"/> 
  </div>
</template>

vue-query-param's People

Contributors

tato30 avatar

Stargazers

Pascal Martineau avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.