mp-weixin-back 0.0.15 → 0.0.17

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/client.d.ts CHANGED
@@ -1,32 +1,109 @@
1
1
  declare module 'mp-weixin-back-helper' {
2
- type Config = {
2
+ /**
3
+ * Per-page options for `onPageBack()`.
4
+ */
5
+ type OnPageBackOptions = {
3
6
  /**
4
- * 初始化时是否监听手势返回,默认值为`true`
7
+ * Whether to block the default back navigation for this page.
8
+ * When `true`, the user stays on the current page and only the callback fires.
9
+ * @default false
5
10
  */
6
- initialValue: boolean
11
+ preventDefault?: boolean
12
+
7
13
  /**
8
- * 是否阻止默认的回退事件,默认为 false
14
+ * How many times to intercept the back event before allowing through.
15
+ * Set to a large number (e.g. `9999`) for persistent interception.
16
+ * @default 1
9
17
  */
10
- preventDefault: boolean
18
+ frequency?: number
19
+
11
20
  /**
12
- * 阻止次数,默认是 `1`
21
+ * Whether to start listening immediately when the page mounts.
22
+ * Set to `false` and call `activeMpBack()` manually to enable later.
23
+ * @default true
13
24
  */
14
- frequency: number
25
+ initialValue?: boolean
15
26
  }
16
27
 
17
- function onPageBack(callback: () => void, params?: Partial<Config>)
28
+ /**
29
+ * Listen to back navigation events (gesture back + navbar back button) on the current page.
30
+ *
31
+ * Must be called inside `<script setup>` at the top level.
32
+ *
33
+ * @param callback - Function to call when back navigation is detected.
34
+ * @param options - Optional per-page configuration. Overrides global plugin config.
35
+ *
36
+ * @example
37
+ * // Basic usage
38
+ * import onPageBack from 'mp-weixin-back-helper'
39
+ *
40
+ * onPageBack(() => {
41
+ * console.log('back detected')
42
+ * })
43
+ *
44
+ * @example
45
+ * // Prevent back and show a confirmation dialog
46
+ * import onPageBack from 'mp-weixin-back-helper'
47
+ *
48
+ * onPageBack(
49
+ * () => {
50
+ * uni.showModal({
51
+ * title: '提示',
52
+ * content: '确定要返回吗?',
53
+ * success: (res) => {
54
+ * if (res.confirm) uni.navigateBack()
55
+ * },
56
+ * })
57
+ * },
58
+ * { preventDefault: true }
59
+ * )
60
+ *
61
+ * @example
62
+ * // Start disabled, enable after a button click
63
+ * import onPageBack, { activeMpBack, inactiveMpBack } from 'mp-weixin-back-helper'
64
+ *
65
+ * onPageBack(() => { handleBack() }, { initialValue: false })
66
+ *
67
+ * function startListening() { activeMpBack() }
68
+ * function stopListening() { inactiveMpBack() }
69
+ */
70
+ function onPageBack(callback: () => void, options?: OnPageBackOptions): void
18
71
 
19
72
  /**
20
- * 开启监听手势滑动事件
73
+ * Enable the back event listener for the current page.
74
+ *
75
+ * Use when `initialValue: false` was passed to `onPageBack()`,
76
+ * or to re-enable after calling `inactiveMpBack()`.
77
+ *
78
+ * Must be called inside `<script setup>`.
79
+ *
80
+ * @example
81
+ * import { activeMpBack } from 'mp-weixin-back-helper'
82
+ *
83
+ * // Enable on button click
84
+ * function handleEnable() {
85
+ * activeMpBack()
86
+ * }
21
87
  */
22
- function activeMpBack()
88
+ function activeMpBack(): void
23
89
 
24
90
  /**
25
- * 禁用监听手势滑动事件
91
+ * Disable the back event listener for the current page.
92
+ *
93
+ * The page will resume its default back behavior until `activeMpBack()` is called again.
94
+ *
95
+ * Must be called inside `<script setup>`.
96
+ *
97
+ * @example
98
+ * import { inactiveMpBack } from 'mp-weixin-back-helper'
99
+ *
100
+ * // Disable when a modal is open
101
+ * function handleModalOpen() {
102
+ * inactiveMpBack()
103
+ * }
26
104
  */
27
- function inactiveMpBack()
105
+ function inactiveMpBack(): void
28
106
 
29
107
  export default onPageBack
30
-
31
108
  export { activeMpBack, inactiveMpBack }
32
109
  }
package/dist/index.cjs CHANGED
@@ -423,9 +423,10 @@ async function resolveCompiler(root) {
423
423
  return await import('@vue/compiler-sfc');
424
424
  } catch (secondError) {
425
425
  throw new Error(
426
- `\u65E0\u6CD5\u89E3\u6790 @vue/compiler-sfc\u3002
427
- \u6B64\u63D2\u4EF6\u9700\u8981\u9879\u76EE\u4E2D\u5B89\u88C5 @vue/compiler-sfc\u3002
428
- \u8BF7\u624B\u52A8\u5B89\u88C5\uFF1Apnpm add -D @vue/compiler-sfc
426
+ `[mp-weixin-back] Cannot resolve @vue/compiler-sfc.
427
+ This plugin requires @vue/compiler-sfc to be installed in your project.
428
+ Fix: pnpm add -D @vue/compiler-sfc
429
+ Docs: https://github.com/DBAAZzz/mp-weixin-back#%EF%B8%8F-vite-\u914D\u7F6E
429
430
  `
430
431
  );
431
432
  }
@@ -447,7 +448,10 @@ async function transformVueFile(code, id) {
447
448
  const walker = scriptSetup ? "compositionWalk" : "optionsWalk";
448
449
  return vueWalker[walker](this, code, sfc, id);
449
450
  } catch (error) {
450
- this.log.error("\u89E3\u6790vue\u6587\u4EF6\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5\u6587\u4EF6\u662F\u5426\u6B63\u786E");
451
+ this.log.error(
452
+ `Failed to transform ${id}. Please check the file is a valid Vue SFC.
453
+ Docs: https://github.com/DBAAZzz/mp-weixin-back#-\u5FEB\u901F\u5F00\u59CB`
454
+ );
451
455
  this.log.error(String(error));
452
456
  return code;
453
457
  }
@@ -511,7 +515,11 @@ class pageContext {
511
515
  }
512
516
  }
513
517
  } catch (error) {
514
- this.log.error("\u8BFB\u53D6pages.json\u6587\u4EF6\u5931\u8D25");
518
+ this.log.error(
519
+ `Failed to read pages.json. Make sure src/pages.json exists and is valid JSON/JSON5.
520
+ Path checked: ${this.getPagesJsonPath()}
521
+ Docs: https://github.com/DBAAZzz/mp-weixin-back#%EF%B8%8F-vite-\u914D\u7F6E`
522
+ );
515
523
  this.log.debugLog(String(error));
516
524
  }
517
525
  }
package/dist/index.d.cts CHANGED
@@ -1,34 +1,67 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
- type UserOptions = Partial<Config>;
4
- type Config = {
3
+ /**
4
+ * Parameters passed to the onPageBack callback.
5
+ */
6
+ type BackParams = {
5
7
  /**
6
- * 初始化时是否监听手势返回,默认值为`true`
8
+ * The path of the current page that triggered the back event.
9
+ * @example 'pages/index/index'
7
10
  */
8
- initialValue: boolean;
11
+ page: string;
12
+ };
13
+ /**
14
+ * Per-page options for `onPageBack()`.
15
+ */
16
+ type OnPageBackOptions = {
9
17
  /**
10
- * 是否阻止默认的回退事件,默认为 false
18
+ * Whether to block the default back navigation for this page.
19
+ * When `true`, the user stays on the current page and only the callback fires.
20
+ * @default false
21
+ * @example
22
+ * onPageBack(() => showDialog(), { preventDefault: true })
11
23
  */
12
24
  preventDefault: boolean;
13
25
  /**
14
- * 阻止次数,默认是 `1`
26
+ * How many times to intercept the back event before allowing through.
27
+ * Set to a large number (e.g. `9999`) for persistent interception.
28
+ * @default 1
29
+ * @example
30
+ * // Block 3 times, then allow back
31
+ * onPageBack(() => {}, { frequency: 3 })
15
32
  */
16
33
  frequency: number;
17
34
  /**
18
- * 调试模式
35
+ * Whether to start listening immediately when the page mounts.
36
+ * Set to `false` to start disabled and enable manually via `activeMpBack()`.
37
+ * @default true
38
+ * @example
39
+ * // Start disabled, enable after user action
40
+ * onPageBack(() => {}, { initialValue: false })
41
+ * activeMpBack()
19
42
  */
20
- debug: boolean;
43
+ initialValue: boolean;
44
+ };
45
+ /**
46
+ * Global plugin options passed to `mpBackPlugin()` in `vite.config.ts`.
47
+ */
48
+ type Config = OnPageBackOptions & {
21
49
  /**
22
- * 页面回退时触发
50
+ * Enable debug logging in development mode.
51
+ * @default false
23
52
  */
24
- onPageBack?: (params: BackParams) => void;
25
- };
26
- type BackParams = {
53
+ debug: boolean;
27
54
  /**
28
- * 当前页面路径
55
+ * Global callback fired every time a back event is detected on any page.
56
+ * Page-level callbacks registered via `onPageBack()` run in addition to this.
57
+ * @example
58
+ * mpBackPlugin({
59
+ * onPageBack: ({ page }) => console.log('back on:', page)
60
+ * })
29
61
  */
30
- page: string;
62
+ onPageBack?: (params: BackParams) => void;
31
63
  };
64
+ type UserOptions = Partial<Config>;
32
65
 
33
66
  declare function MpBackPlugin(userOptions?: UserOptions): Plugin;
34
67
 
package/dist/index.d.mts CHANGED
@@ -1,34 +1,67 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
- type UserOptions = Partial<Config>;
4
- type Config = {
3
+ /**
4
+ * Parameters passed to the onPageBack callback.
5
+ */
6
+ type BackParams = {
5
7
  /**
6
- * 初始化时是否监听手势返回,默认值为`true`
8
+ * The path of the current page that triggered the back event.
9
+ * @example 'pages/index/index'
7
10
  */
8
- initialValue: boolean;
11
+ page: string;
12
+ };
13
+ /**
14
+ * Per-page options for `onPageBack()`.
15
+ */
16
+ type OnPageBackOptions = {
9
17
  /**
10
- * 是否阻止默认的回退事件,默认为 false
18
+ * Whether to block the default back navigation for this page.
19
+ * When `true`, the user stays on the current page and only the callback fires.
20
+ * @default false
21
+ * @example
22
+ * onPageBack(() => showDialog(), { preventDefault: true })
11
23
  */
12
24
  preventDefault: boolean;
13
25
  /**
14
- * 阻止次数,默认是 `1`
26
+ * How many times to intercept the back event before allowing through.
27
+ * Set to a large number (e.g. `9999`) for persistent interception.
28
+ * @default 1
29
+ * @example
30
+ * // Block 3 times, then allow back
31
+ * onPageBack(() => {}, { frequency: 3 })
15
32
  */
16
33
  frequency: number;
17
34
  /**
18
- * 调试模式
35
+ * Whether to start listening immediately when the page mounts.
36
+ * Set to `false` to start disabled and enable manually via `activeMpBack()`.
37
+ * @default true
38
+ * @example
39
+ * // Start disabled, enable after user action
40
+ * onPageBack(() => {}, { initialValue: false })
41
+ * activeMpBack()
19
42
  */
20
- debug: boolean;
43
+ initialValue: boolean;
44
+ };
45
+ /**
46
+ * Global plugin options passed to `mpBackPlugin()` in `vite.config.ts`.
47
+ */
48
+ type Config = OnPageBackOptions & {
21
49
  /**
22
- * 页面回退时触发
50
+ * Enable debug logging in development mode.
51
+ * @default false
23
52
  */
24
- onPageBack?: (params: BackParams) => void;
25
- };
26
- type BackParams = {
53
+ debug: boolean;
27
54
  /**
28
- * 当前页面路径
55
+ * Global callback fired every time a back event is detected on any page.
56
+ * Page-level callbacks registered via `onPageBack()` run in addition to this.
57
+ * @example
58
+ * mpBackPlugin({
59
+ * onPageBack: ({ page }) => console.log('back on:', page)
60
+ * })
29
61
  */
30
- page: string;
62
+ onPageBack?: (params: BackParams) => void;
31
63
  };
64
+ type UserOptions = Partial<Config>;
32
65
 
33
66
  declare function MpBackPlugin(userOptions?: UserOptions): Plugin;
34
67
 
package/dist/index.d.ts CHANGED
@@ -1,34 +1,67 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
- type UserOptions = Partial<Config>;
4
- type Config = {
3
+ /**
4
+ * Parameters passed to the onPageBack callback.
5
+ */
6
+ type BackParams = {
5
7
  /**
6
- * 初始化时是否监听手势返回,默认值为`true`
8
+ * The path of the current page that triggered the back event.
9
+ * @example 'pages/index/index'
7
10
  */
8
- initialValue: boolean;
11
+ page: string;
12
+ };
13
+ /**
14
+ * Per-page options for `onPageBack()`.
15
+ */
16
+ type OnPageBackOptions = {
9
17
  /**
10
- * 是否阻止默认的回退事件,默认为 false
18
+ * Whether to block the default back navigation for this page.
19
+ * When `true`, the user stays on the current page and only the callback fires.
20
+ * @default false
21
+ * @example
22
+ * onPageBack(() => showDialog(), { preventDefault: true })
11
23
  */
12
24
  preventDefault: boolean;
13
25
  /**
14
- * 阻止次数,默认是 `1`
26
+ * How many times to intercept the back event before allowing through.
27
+ * Set to a large number (e.g. `9999`) for persistent interception.
28
+ * @default 1
29
+ * @example
30
+ * // Block 3 times, then allow back
31
+ * onPageBack(() => {}, { frequency: 3 })
15
32
  */
16
33
  frequency: number;
17
34
  /**
18
- * 调试模式
35
+ * Whether to start listening immediately when the page mounts.
36
+ * Set to `false` to start disabled and enable manually via `activeMpBack()`.
37
+ * @default true
38
+ * @example
39
+ * // Start disabled, enable after user action
40
+ * onPageBack(() => {}, { initialValue: false })
41
+ * activeMpBack()
19
42
  */
20
- debug: boolean;
43
+ initialValue: boolean;
44
+ };
45
+ /**
46
+ * Global plugin options passed to `mpBackPlugin()` in `vite.config.ts`.
47
+ */
48
+ type Config = OnPageBackOptions & {
21
49
  /**
22
- * 页面回退时触发
50
+ * Enable debug logging in development mode.
51
+ * @default false
23
52
  */
24
- onPageBack?: (params: BackParams) => void;
25
- };
26
- type BackParams = {
53
+ debug: boolean;
27
54
  /**
28
- * 当前页面路径
55
+ * Global callback fired every time a back event is detected on any page.
56
+ * Page-level callbacks registered via `onPageBack()` run in addition to this.
57
+ * @example
58
+ * mpBackPlugin({
59
+ * onPageBack: ({ page }) => console.log('back on:', page)
60
+ * })
29
61
  */
30
- page: string;
62
+ onPageBack?: (params: BackParams) => void;
31
63
  };
64
+ type UserOptions = Partial<Config>;
32
65
 
33
66
  declare function MpBackPlugin(userOptions?: UserOptions): Plugin;
34
67
 
package/dist/index.mjs CHANGED
@@ -412,9 +412,10 @@ async function resolveCompiler(root) {
412
412
  return await import('@vue/compiler-sfc');
413
413
  } catch (secondError) {
414
414
  throw new Error(
415
- `\u65E0\u6CD5\u89E3\u6790 @vue/compiler-sfc\u3002
416
- \u6B64\u63D2\u4EF6\u9700\u8981\u9879\u76EE\u4E2D\u5B89\u88C5 @vue/compiler-sfc\u3002
417
- \u8BF7\u624B\u52A8\u5B89\u88C5\uFF1Apnpm add -D @vue/compiler-sfc
415
+ `[mp-weixin-back] Cannot resolve @vue/compiler-sfc.
416
+ This plugin requires @vue/compiler-sfc to be installed in your project.
417
+ Fix: pnpm add -D @vue/compiler-sfc
418
+ Docs: https://github.com/DBAAZzz/mp-weixin-back#%EF%B8%8F-vite-\u914D\u7F6E
418
419
  `
419
420
  );
420
421
  }
@@ -436,7 +437,10 @@ async function transformVueFile(code, id) {
436
437
  const walker = scriptSetup ? "compositionWalk" : "optionsWalk";
437
438
  return vueWalker[walker](this, code, sfc, id);
438
439
  } catch (error) {
439
- this.log.error("\u89E3\u6790vue\u6587\u4EF6\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5\u6587\u4EF6\u662F\u5426\u6B63\u786E");
440
+ this.log.error(
441
+ `Failed to transform ${id}. Please check the file is a valid Vue SFC.
442
+ Docs: https://github.com/DBAAZzz/mp-weixin-back#-\u5FEB\u901F\u5F00\u59CB`
443
+ );
440
444
  this.log.error(String(error));
441
445
  return code;
442
446
  }
@@ -500,7 +504,11 @@ class pageContext {
500
504
  }
501
505
  }
502
506
  } catch (error) {
503
- this.log.error("\u8BFB\u53D6pages.json\u6587\u4EF6\u5931\u8D25");
507
+ this.log.error(
508
+ `Failed to read pages.json. Make sure src/pages.json exists and is valid JSON/JSON5.
509
+ Path checked: ${this.getPagesJsonPath()}
510
+ Docs: https://github.com/DBAAZzz/mp-weixin-back#%EF%B8%8F-vite-\u914D\u7F6E`
511
+ );
504
512
  this.log.debugLog(String(error));
505
513
  }
506
514
  }
package/package.json CHANGED
@@ -1,20 +1,27 @@
1
1
  {
2
2
  "name": "mp-weixin-back",
3
3
  "type": "module",
4
- "version": "0.0.15",
5
- "description": "监听微信小程序的手势返回和页面默认导航栏的返回",
4
+ "version": "0.0.17",
5
+ "description": "Vite plugin to intercept back navigation (gesture back & navbar back) in WeChat miniprogram (mp-weixin) built with uni-app + Vue 3. 监听微信小程序的手势返回和页面默认导航栏的返回",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.mjs",
8
8
  "types": "./dist/index.d.ts",
9
- "scripts": {
10
- "build": "unbuild",
11
- "test": "vitest"
12
- },
13
9
  "keywords": [
10
+ "miniprogram",
11
+ "wechat",
12
+ "mp-weixin",
13
+ "back-navigation",
14
+ "gesture-back",
15
+ "navigate-back",
16
+ "page-back",
17
+ "navigation-intercept",
18
+ "vite-plugin",
19
+ "uniapp",
20
+ "uni-app",
21
+ "vue3",
14
22
  "微信小程序",
15
23
  "手势返回",
16
- "vite",
17
- "uniapp"
24
+ "页面返回拦截"
18
25
  ],
19
26
  "exports": {
20
27
  ".": {
@@ -31,6 +38,11 @@
31
38
  "types": "./client.d.ts"
32
39
  }
33
40
  },
41
+ "files": [
42
+ "dist",
43
+ "client.d.ts",
44
+ "README.md"
45
+ ],
34
46
  "author": "DBAAZzz",
35
47
  "license": "ISC",
36
48
  "repository": {
@@ -54,6 +66,7 @@
54
66
  "*": "prettier --write"
55
67
  },
56
68
  "devDependencies": {
69
+ "@changesets/cli": "^2.29.8",
57
70
  "@types/babel__generator": "^7.6.8",
58
71
  "@types/node": "^22.9.3",
59
72
  "@vitejs/plugin-vue": "^5.2.0",
@@ -63,5 +76,12 @@
63
76
  "unbuild": "^2.0.0",
64
77
  "vitest": "^2.1.5",
65
78
  "vue": "^3.5.13"
79
+ },
80
+ "scripts": {
81
+ "build": "unbuild",
82
+ "test": "vitest",
83
+ "changeset": "changeset",
84
+ "version": "changeset version",
85
+ "release": "pnpm build && changeset publish"
66
86
  }
67
- }
87
+ }
package/readme.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # mp-weixin-back
2
2
 
3
+ Vite plugin to intercept back navigation (gesture back + navbar back button) in WeChat miniprogram (mp-weixin) built with uni-app + Vue 3.
4
+
5
+ ## TL;DR
6
+
7
+ ```ts
8
+ // vite.config.ts
9
+ import { defineConfig } from 'vite'
10
+ import mpBackPlugin from 'mp-weixin-back'
11
+
12
+ export default defineConfig({
13
+ plugins: [mpBackPlugin()],
14
+ })
15
+ ```
16
+
17
+ ```ts
18
+ // Any page .vue file — inside <script setup>
19
+ import onPageBack from 'mp-weixin-back-helper'
20
+
21
+ onPageBack(() => {
22
+ // handle back: show dialog, log analytics, etc.
23
+ })
24
+ ```
25
+
26
+ ---
27
+
3
28
  ## 功能概述
4
29
 
5
30
  `mp-weixin-back` 是一个专门用于监听微信小程序`手势返回`、`导航栏返回事件`、`navigateBack`的工具库,提供灵活的配置选项和简洁的 API。
@@ -9,7 +34,7 @@
9
34
  ```bash
10
35
  npm install mp-weixin-back
11
36
  # 或
12
- yarn add mp-weixin-back
37
+ pnpm add mp-weixin-back
13
38
  ```
14
39
 
15
40
  ## ⚙️ Vite 配置
@@ -27,9 +52,9 @@ export default defineConfig({
27
52
  preventDefault: false, // 是否阻止默认返回行为,设置成 true 则不会返回上一层
28
53
  frequency: 1, // 阻止次数,需要一直拦截则设置一个很大的值即可,如:9999
29
54
  debug: false, // 调试模式,默认为 false
30
- onPageBack: () => {
31
- console.log('返回事件触发')
32
- }, // 统一钩子,事件触发时执行
55
+ onPageBack: ({ page }) => {
56
+ console.log('返回事件触发,当前页面:', page)
57
+ }, // 全局钩子,任意页面触发时执行
33
58
  }),
34
59
  ],
35
60
  })
@@ -68,24 +93,45 @@ onPageBack(
68
93
  )
69
94
  ```
70
95
 
96
+ ### 显示确认弹窗(常见场景)
97
+
98
+ ```ts
99
+ <script setup>
100
+ import onPageBack from 'mp-weixin-back-helper'
101
+
102
+ onPageBack(
103
+ () => {
104
+ uni.showModal({
105
+ title: '提示',
106
+ content: '确定要离开当前页面吗?',
107
+ success: (res) => {
108
+ if (res.confirm) uni.navigateBack()
109
+ },
110
+ })
111
+ },
112
+ { preventDefault: true }
113
+ )
114
+ </script>
115
+ ```
116
+
71
117
  ## 📚 API 文档
72
118
 
73
- ### `onPageBack(callback, config?)`
119
+ ### `onPageBack(callback, options?)`
74
120
 
75
- 监听页面返回事件
121
+ 监听页面返回事件,必须在 `<script setup>` 顶层调用。
76
122
 
77
- | 参数 | 类型 | 必填 | 说明 |
78
- | -------- | ------------ | ---- | ------------------------ |
79
- | callback | `() => void` | 是 | 返回事件触发时的回调函数 |
80
- | options | Object | 否 | 监听器配置选项 |
123
+ | 参数 | 类型 | 必填 | 说明 |
124
+ | ---------- | ------------------- | ---- | ------------------------ |
125
+ | `callback` | `() => void` | 是 | 返回事件触发时的回调函数 |
126
+ | `options` | `OnPageBackOptions` | 否 | 监听器配置选项 |
81
127
 
82
- #### 配置选项
128
+ #### 配置选项 `OnPageBackOptions`
83
129
 
84
- | 参数 | 类型 | 默认值 | 说明 |
85
- | -------------- | ------- | ------ | ----------------------------------------------- |
86
- | preventDefault | boolean | false | 是否阻止默认返回行为(true 时页面不会实际返回) |
87
- | frequency | number | 1 | 阻止次数 |
88
- | initialValue | boolean | true | 是否立即启用监听(设为 false 时需手动激活) |
130
+ | 参数 | 类型 | 默认值 | 说明 |
131
+ | ---------------- | --------- | ------- | --------------------------------------------------------------- |
132
+ | `preventDefault` | `boolean` | `false` | 是否阻止默认返回行为(`true` 时页面不会实际返回) |
133
+ | `frequency` | `number` | `1` | 阻止次数 |
134
+ | `initialValue` | `boolean` | `true` | 是否立即启用监听(设为 `false` 时需手动调用 `activeMpBack()`) |
89
135
 
90
136
  ### 辅助方法
91
137
 
@@ -103,20 +149,27 @@ onPageBack(
103
149
  <template>
104
150
  <div>
105
151
  <!-- 页面代码 -->
106
- <button @click="toggleListener(true)">开启</button>
107
- <button @click="toggleListener(false)">禁用</button>
152
+ <button @click="activeMpBack()">开启</button>
153
+ <button @click="inactiveMpBack()">禁用</button>
108
154
  </div>
109
155
  </template>
110
156
 
111
157
  <script setup>
112
158
  import onPageBack, { activeMpBack, inactiveMpBack } from 'mp-weixin-back-helper'
113
159
 
114
- const toggleListener = (enable) => {
115
- enable ? activeMpBack() : inactiveMpBack()
116
- }
160
+ onPageBack(() => { /* 处理返回 */ }, { initialValue: false })
117
161
  </script>
118
162
  ```
119
163
 
164
+ ### 插件全局配置 `mpBackPlugin(options)`
165
+
166
+ | 参数 | 类型 | 默认值 | 说明 |
167
+ | ---------------- | ------------------------------------------- | ------- | -------------------------- |
168
+ | `preventDefault` | `boolean` | `false` | 全局阻止默认返回行为 |
169
+ | `frequency` | `number` | `1` | 全局阻止次数 |
170
+ | `debug` | `boolean` | `false` | 开发模式下开启调试日志 |
171
+ | `onPageBack` | `(params: { page: string }) => void` | — | 全局回调,任意页面触发执行 |
172
+
120
173
  ## 🎯 选项式 API 支持(未完善)
121
174
 
122
175
  组件内直接声明
@@ -171,3 +224,10 @@ onPageBack(
171
224
  ### Q2: 全局配置与页面配置的优先级?
172
225
 
173
226
  页面级配置会覆盖全局配置,建议将通用配置放在全局,特殊需求在页面单独设置。
227
+
228
+ ### Q3: 不生效怎么排查?
229
+
230
+ 1. 确认 `src/pages.json` 存在且格式正确
231
+ 2. 确认是页面级 `.vue` 文件(非组件)
232
+ 3. 开启 `debug: true` 查看插件日志
233
+ 4. 确认 `@vue/compiler-sfc` 已安装:`pnpm add -D @vue/compiler-sfc`