@msom/vue-query-preserve-v3 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,48 @@
1
+ function createQueryPreservePlugin(options = {}) {
2
+ const { preserveKeys = [], beforeNavigate } = options;
3
+ return {
4
+ install(app) {
5
+ const router = app.config.globalProperties.$router;
6
+ if (!router) {
7
+ console.error(
8
+ "[vue-query-preserve] Router instance not found. Please ensure app.use(router) is called before."
9
+ );
10
+ return;
11
+ }
12
+ router.beforeEach((to, from) => {
13
+ if (beforeNavigate) {
14
+ const result = beforeNavigate(to, from);
15
+ if (result === false) {
16
+ return false;
17
+ }
18
+ }
19
+ const preserved = {};
20
+ if (preserveKeys.length > 0 && from.query) {
21
+ preserveKeys.forEach((key) => {
22
+ if (from.query[key] !== void 0) {
23
+ preserved[key] = from.query[key];
24
+ }
25
+ });
26
+ }
27
+ if (Object.keys(preserved).length > 0) {
28
+ const hasPreservedParams = preserveKeys.some(
29
+ (key) => to.query[key] === void 0
30
+ );
31
+ if (hasPreservedParams) {
32
+ return {
33
+ ...to,
34
+ query: {
35
+ ...preserved,
36
+ ...to.query
37
+ }
38
+ };
39
+ }
40
+ }
41
+ });
42
+ }
43
+ };
44
+ }
45
+ export {
46
+ createQueryPreservePlugin as default
47
+ };
48
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * Vue 3 版本插件 - 路由查询参数保留插件\n * 适配 Vue 3.x + vue-router@4\n */\nimport type { Plugin, App } from \"vue\";\nimport type { Router } from \"vue-router\";\n\nexport interface QueryPreserveOptions {\n /**\n * 需要保留的查询参数键名白名单\n */\n preserveKeys?: string[];\n /**\n * 跳转前的自定义钩子函数\n * @param to 目标路由对象\n * @param from 来源路由对象\n * @returns 返回 false 可阻止跳转\n */\n beforeNavigate?: (to: any, from: any) => boolean | void;\n}\n\n/**\n * Vue 3 版本的路由查询参数保留插件\n * @param options 插件配置选项\n */\nfunction createQueryPreservePlugin(options: QueryPreserveOptions = {}): Plugin {\n const { preserveKeys = [], beforeNavigate } = options;\n\n return {\n install(app: App) {\n const router: Router = app.config.globalProperties.$router;\n\n if (!router) {\n console.error(\n \"[vue-query-preserve] Router instance not found. Please ensure app.use(router) is called before.\",\n );\n return;\n }\n\n router.beforeEach((to, from) => {\n // 如果配置了 beforeNavigate 钩子,执行它\n if (beforeNavigate) {\n const result = beforeNavigate(to, from);\n if (result === false) {\n return false;\n }\n }\n\n // 从来源路由中提取需要保留的参数\n const preserved: Record<string, string> = {};\n if (preserveKeys.length > 0 && from.query) {\n preserveKeys.forEach((key) => {\n if (from.query[key] !== undefined) {\n preserved[key] = from.query[key] as string;\n }\n });\n }\n\n // 如果有需要保留的参数,且目标路由没有这些参数,则合并\n if (Object.keys(preserved).length > 0) {\n const hasPreservedParams = preserveKeys.some(\n (key) => to.query[key] === undefined,\n );\n if (hasPreservedParams) {\n return {\n ...to,\n query: {\n ...preserved,\n ...to.query,\n },\n };\n }\n }\n });\n },\n };\n}\n\nexport default createQueryPreservePlugin;\n"],"names":[],"mappings":"AAyBA,SAAS,0BAA0B,UAAgC,IAAY;AAC7E,QAAM,EAAE,eAAe,IAAI,mBAAmB;AAE9C,SAAO;AAAA,IACL,QAAQ,KAAU;AAChB,YAAM,SAAiB,IAAI,OAAO,iBAAiB;AAEnD,UAAI,CAAC,QAAQ;AACX,gBAAQ;AAAA,UACN;AAAA,QAAA;AAEF;AAAA,MACF;AAEA,aAAO,WAAW,CAAC,IAAI,SAAS;AAE9B,YAAI,gBAAgB;AAClB,gBAAM,SAAS,eAAe,IAAI,IAAI;AACtC,cAAI,WAAW,OAAO;AACpB,mBAAO;AAAA,UACT;AAAA,QACF;AAGA,cAAM,YAAoC,CAAA;AAC1C,YAAI,aAAa,SAAS,KAAK,KAAK,OAAO;AACzC,uBAAa,QAAQ,CAAC,QAAQ;AAC5B,gBAAI,KAAK,MAAM,GAAG,MAAM,QAAW;AACjC,wBAAU,GAAG,IAAI,KAAK,MAAM,GAAG;AAAA,YACjC;AAAA,UACF,CAAC;AAAA,QACH;AAGA,YAAI,OAAO,KAAK,SAAS,EAAE,SAAS,GAAG;AACrC,gBAAM,qBAAqB,aAAa;AAAA,YACtC,CAAC,QAAQ,GAAG,MAAM,GAAG,MAAM;AAAA,UAAA;AAE7B,cAAI,oBAAoB;AACtB,mBAAO;AAAA,cACL,GAAG;AAAA,cACH,OAAO;AAAA,gBACL,GAAG;AAAA,gBACH,GAAG,GAAG;AAAA,cAAA;AAAA,YACR;AAAA,UAEJ;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EAAA;AAEJ;"}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/package",
3
+ "name": "@msom/vue-query-preserve-v3",
4
+ "version": "1.0.0",
5
+ "description": "Vue 3 插件,在路由跳转时自动保留指定的查询参数",
6
+ "main": "./dist/index.cjs.js",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.cjs.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "keywords": [
19
+ "vue",
20
+ "vue-router",
21
+ "vue3",
22
+ "query",
23
+ "preserve",
24
+ "plugin"
25
+ ],
26
+ "author": "",
27
+ "license": "ISC",
28
+ "type": "module",
29
+ "peerDependencies": {
30
+ "vue": "^3.x",
31
+ "vue-router": "^4.x"
32
+ },
33
+ "devDependencies": {
34
+ "vite": "^5.0.0",
35
+ "vue": "^3.5.0",
36
+ "vue-router": "^4.5.0"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public",
40
+ "registry": "https://registry.npmjs.org"
41
+ },
42
+ "scripts": {
43
+ "build": "tsc --noEmit && vite build --config ../../vite.config.ts",
44
+ "release": "pnpm run build && pnpm publish --git-checks=false"
45
+ }
46
+ }