@msom/vue-query-preserve-v2 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,49 @@
1
+ function createQueryPreservePlugin(options = {}) {
2
+ const { preserveKeys = [], beforeNavigate } = options;
3
+ return {
4
+ install(Vue) {
5
+ const router = Vue.prototype.$router;
6
+ if (!router) {
7
+ console.error(
8
+ "[vue-query-preserve] Router instance not found. Please ensure Vue.use(router) is called before."
9
+ );
10
+ return;
11
+ }
12
+ router.beforeEach((to, from, next) => {
13
+ if (beforeNavigate) {
14
+ const result = beforeNavigate(to, from);
15
+ if (result === false) {
16
+ return next(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 next({
33
+ ...to,
34
+ query: {
35
+ ...preserved,
36
+ ...to.query
37
+ }
38
+ });
39
+ }
40
+ }
41
+ next();
42
+ });
43
+ }
44
+ };
45
+ }
46
+ export {
47
+ createQueryPreservePlugin as default
48
+ };
49
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * Vue 2 版本插件 - 路由查询参数保留插件\n * 适配 Vue 2.x + vue-router@3\n */\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 2 版本的路由查询参数保留插件\n * @param options 插件配置选项\n */\nfunction createQueryPreservePlugin(options: QueryPreserveOptions = {}) {\n const { preserveKeys = [], beforeNavigate } = options;\n\n return {\n install(Vue: any) {\n const router = Vue.prototype.$router;\n\n if (!router) {\n console.error(\n \"[vue-query-preserve] Router instance not found. Please ensure Vue.use(router) is called before.\",\n );\n return;\n }\n\n router.beforeEach((to: any, from: any, next: any) => {\n // 如果配置了 beforeNavigate 钩子,执行它\n if (beforeNavigate) {\n const result = beforeNavigate(to, from);\n if (result === false) {\n return next(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 next({\n ...to,\n query: {\n ...preserved,\n ...to.query,\n },\n });\n }\n }\n\n next();\n });\n },\n };\n}\n\nexport default createQueryPreservePlugin;\n"],"names":[],"mappings":"AAuBA,SAAS,0BAA0B,UAAgC,IAAI;AACrE,QAAM,EAAE,eAAe,IAAI,mBAAmB;AAE9C,SAAO;AAAA,IACL,QAAQ,KAAU;AAChB,YAAM,SAAS,IAAI,UAAU;AAE7B,UAAI,CAAC,QAAQ;AACX,gBAAQ;AAAA,UACN;AAAA,QAAA;AAEF;AAAA,MACF;AAEA,aAAO,WAAW,CAAC,IAAS,MAAW,SAAc;AAEnD,YAAI,gBAAgB;AAClB,gBAAM,SAAS,eAAe,IAAI,IAAI;AACtC,cAAI,WAAW,OAAO;AACpB,mBAAO,KAAK,KAAK;AAAA,UACnB;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,KAAK;AAAA,cACV,GAAG;AAAA,cACH,OAAO;AAAA,gBACL,GAAG;AAAA,gBACH,GAAG,GAAG;AAAA,cAAA;AAAA,YACR,CACD;AAAA,UACH;AAAA,QACF;AAEA,aAAA;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EAAA;AAEJ;"}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/package",
3
+ "name": "@msom/vue-query-preserve-v2",
4
+ "version": "1.0.0",
5
+ "description": "Vue 2 插件,在路由跳转时自动保留指定的查询参数",
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
+ "vue2",
22
+ "query",
23
+ "preserve",
24
+ "plugin"
25
+ ],
26
+ "author": "",
27
+ "license": "ISC",
28
+ "type": "module",
29
+ "peerDependencies": {
30
+ "vue": "^2.x",
31
+ "vue-router": "^3.x"
32
+ },
33
+ "devDependencies": {
34
+ "@types/vue": "^2.0.0",
35
+ "vite": "^5.0.0",
36
+ "vue": "^2.7.0",
37
+ "vue-router": "^3.6.0"
38
+ },
39
+ "publishConfig": {
40
+ "access": "public",
41
+ "registry": "https://registry.npmjs.org"
42
+ },
43
+ "scripts": {
44
+ "build": "tsc --noEmit && vite build --config ../../vite.config.ts",
45
+ "release": "pnpm run build && pnpm publish --git-checks=false"
46
+ }
47
+ }