Gathering detailed insights and metrics for vue-use-query-param
Gathering detailed insights and metrics for vue-use-query-param
Gathering detailed insights and metrics for vue-use-query-param
Gathering detailed insights and metrics for vue-use-query-param
Use url query parameter like regular refs in Vue, in a typesafe way.
npm install vue-use-query-param
Typescript
Module System
Node Version
NPM Version
TypeScript (98.64%)
JavaScript (1.36%)
Total Downloads
512,319
Last Day
5
Last Week
110
Last Month
637
Last Year
511,373
9 Stars
38 Commits
1 Forks
1 Watchers
1 Branches
1 Contributors
Updated on Sep 21, 2024
Latest Version
1.0.2
Package Id
vue-use-query-param@1.0.2
Unpacked Size
83.00 kB
Size
16.26 kB
File Count
43
NPM Version
10.7.0
Node Version
18.20.3
Published on
Jun 03, 2024
Cumulative downloads
Total Downloads
Last Day
0%
5
Compared to previous day
Last Week
-50.5%
110
Compared to previous week
Last Month
4.9%
637
Compared to previous month
Last Year
53,956.3%
511,373
Compared to previous year
2
7
IMPORTANT: This plugin has a peer dependency on vue-router
.
It will only work when using vue-router
in your Vue app.
Don't want to read the rest of the README? Check out the examples.
1npm install vue-use-query-param
1pnpm add vue-use-query-param
1yarn add vue-use-query-param
1import { createApp } from "vue"; 2import { createRouter } from "vue-router"; 3import useQueryParamPlugin from "vue-use-query-param"; 4 5const router = createRouter({ ... }); 6 7const app = createApp(App); 8app.use(router); 9app.use(useQueryParamPlugin, {}); 10app.mount("#app"); 11
useQueryParam
composable1<script setup lang="ts"> 2import { stringParam, useQueryParam } from "vue-use-query-param"; 3 4// foo is a `Ref<string | undefined>`, use it like any other ref 5// When setting foo, the URL will be modified, a query parameter `foo` will 6// be added or updated. (or removed if you're setting `foo` to undefined.) 7const foo = useQueryParam("foo", stringParam()); 8 9foo.value = "bar"; 10</script> 11 12<template> 13 <!-- Reactively updates --> 14 {{ foo }} 15</template>
If your query parameter should always have a value, you can set a default value like this:
1import { stringParam, useQueryParam, withDefault } from "vue-use-query-param"; 2const foo = useQueryParam("foo", withDefault(stringParam(), "bar"));
Currently only a few basic types are supported.
stringParam
: for stringsnumberParam
: for numbers (integers and floats)objectParam
: custom Objects that will be serialized to JSON.booleanParam
: for booleansdateParam
: for dates and datetimearrayParam
: for arrays of the above typesobjectParam
. e.g.: objectParam<number[][]>()
;For some examples on how to use these, check out these examples.
Internally, to be able to set multiple query parameters at once, we don't update the URL immediately after every change. We use setTimeout
to debounce the updates.
By default the debounce time is set to 0
, meaning it will update the URL as soon as possible.
If however you want to delay this more, you can set the debounceTime
option when adding the plugin to the Vue app.
1app.use(useQueryParamPlugin, { debounceTime: 100 }); 2... 3const foo = useQueryParam("foo", stringParam()); 4const bar = useQueryParam("bar", stringParam()); 5 6// the `foo` and `bar` query parameters in the URL won't be updated immediately, but only after 150ms. 7// (50 ms for the bar timeout + 100ms for the debounce time) 8foo.value = "fooval"; 9setTimeout(() => { 10 bar.value = "barval"; 11}, 50);
TypeScript NPM Package Publishing: A Beginner’s Guide (by Paul Ehikhuemen)
I'm originally a React dev and I made much use of the use-query-params hook.
The original need for this plugin came when working on a project for ai-Gust. An initial implementation of useQueryParam
was made there.
They were so kind to let me make this into a separate npm module and make it open source for the world to enjoy.
I've since built on this initial implementation.
No vulnerabilities found.
No security vulnerabilities found.