Skip to content

Commit b9a1da3

Browse files
committed
update version and readme
1 parent 8210b0d commit b9a1da3

File tree

3 files changed

+70
-16
lines changed

3 files changed

+70
-16
lines changed

README.md

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
# vue-utils
1+
# vue-utils <!-- omit in toc -->
22

33
Useful utilities for Vue projects.
44

55
[![npm (scoped)](https://img.shields.io/npm/v/@gradin/vue-utils)](https://www.npmjs.com/package/@gradin/vue-utils)
66
![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@gradin/vue-utils)
77
![npm](https://img.shields.io/npm/dt/@gradin/vue-utils)
88

9-
## Installation
9+
Table of Contents:
10+
<!-- no toc -->
11+
- [Installation](#installation)
12+
- [Features](#features)
13+
- [Improved Vue Reactive](#improved-vue-reactive)
14+
- [Watch Route Query and Params Changes](#watch-route-query-and-params-changes)
15+
16+
## Installation
1017

1118
```sh
1219
# Using npm
@@ -16,18 +23,15 @@ npm install @gradin/vue-utils
1623
yarn add @gradin/vue-utils
1724
```
1825

19-
## Featurs
26+
## Features
2027

2128
### Improved Vue Reactive
2229

2330
The `gReactive` function is a wrapper around Vue's `reactive` function that adds some useful features.
2431
- **Reset**: The `reset` method resets the reactive object to its initial state.
2532
- **Set**: The `set` method sets the reactive object to a new value, merging the new value with the existing value.
2633

27-
Example `reactive` vs `gReactive`
28-
29-
<details>
30-
<summary>Show Codes</summary>
34+
Example `reactive` vs `gReactive`:
3135

3236
```typescript
3337
// before
@@ -82,4 +86,54 @@ Example `reactive` vs `gReactive`
8286
})
8387
}
8488
```
85-
</details>
89+
90+
### Watch Route Query and Params Changes
91+
92+
The `whenRouteChange` will allow you to do something changes but still on the same page. It is also triggered on the first mount of the component.
93+
94+
`whenRouteChange` accepts two arguments:
95+
- `callback`: The function to be called when the route query or params changes.
96+
- `watchSource`: Optional. A function that returns the value to be watched. If not provided, it will watch the entire route query and route params object.
97+
98+
| When | Triggered |
99+
| :--------------------------------------------------------- | :-------: |
100+
| Route query changed (e.g /products to /products?page=2) | Yes |
101+
| Route params changed (e.g /products/1 to /products/2) | Yes |
102+
| Script setup loaded (using `watch` with `immediate: true`) | Yes |
103+
| Route name changed (e.g /products to /home) | No |
104+
| Also Route name changed (e.g /products to /product/1) | No |
105+
106+
Usage:
107+
```typescript
108+
import { whenRouteChange } from '@gradin/vue-utils';
109+
import { useRoute } from 'vue-router';
110+
import axios from 'axios';
111+
112+
const route = useRoute();
113+
const products = ref<Product[]>([]);
114+
115+
const getData = () => {
116+
const response = await axios.get('/api/products', {
117+
params: {
118+
page: route.query.page,
119+
search: route.query.search,
120+
category_id: route.query.category_id,
121+
sort: route.query.sort,
122+
}
123+
})
124+
products.value = response.data;
125+
}
126+
127+
whenRouteChange(getData) // will call getData once at the beginning, and again when the route query or params changes.
128+
129+
// or if you want just to track route query page
130+
const highlightMatchesSearch = () => {
131+
//
132+
}
133+
whenRouteChange(
134+
() => {
135+
highlightMatchesSearch()
136+
},
137+
() => route.query.search
138+
)
139+
```

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "@gradin/vue-utils",
3-
"version": "0.0.3",
3+
"version": "0.0.6",
44
"description": "Useful utilities for Vue projects.",
55
"type": "module",
66
"main": "dist/index.js",
7-
"types": "dist/index.d.ts",
7+
"types": "dist/types/index.d.ts",
88
"scripts": {
99
"build": "tsc",
1010
"lint": "eslint . --fix",
11-
"publish": "npm run build && npm publish --access public"
11+
"publish": "npm run build"
1212
},
1313
"files": [
1414
"dist"

0 commit comments

Comments
 (0)