vue-page-store 0.1.0 → 0.2.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/README.md +136 -24
- package/dist/index.cjs.js +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,16 +13,16 @@
|
|
|
13
13
|
| **provide / inject** | 只传数据,不管通信和副作用 |
|
|
14
14
|
| **组件 data** | 跨组件共享困难,深层传递 props 地狱 |
|
|
15
15
|
|
|
16
|
-
`vue-page-store` 解决的就是这个中间地带:**页面内部需要共享状态 + 通信 +
|
|
16
|
+
`vue-page-store` 解决的就是这个中间地带:**页面内部需要共享状态 + 通信 + 生命周期管理,但不应该污染全局。**
|
|
17
17
|
|
|
18
18
|
## 核心特性
|
|
19
19
|
|
|
20
20
|
- **页面级作用域隔离** — 每个 store 独立一份 state 和事件,互不干扰
|
|
21
|
-
-
|
|
22
|
-
-
|
|
21
|
+
- **`useStore(this)` 一行绑定** — 自动挂载生命周期,自动销毁,零手动清理
|
|
22
|
+
- **页面生命周期** — `mount` / `unmount` / `activate` / `deactivate`,keep-alive 原生支持
|
|
23
|
+
- **API 对齐 Pinia** — `state` / `getters` / `actions` / `$patch` / `$reset`,零学习成本
|
|
23
24
|
- **内置作用域事件** — `$emit` / `$on` / `$off` 限定在当前 store 内,替代全局 EventBus
|
|
24
25
|
- **声明式 watch** — 页面级副作用自动绑定生命周期
|
|
25
|
-
- **TypeScript 支持** — 开箱即用的类型定义
|
|
26
26
|
|
|
27
27
|
## 安装
|
|
28
28
|
|
|
@@ -37,7 +37,6 @@ npm install vue-page-store
|
|
|
37
37
|
### 1. 定义 Store
|
|
38
38
|
|
|
39
39
|
```javascript
|
|
40
|
-
// store.js
|
|
41
40
|
import { definePageStore } from 'vue-page-store';
|
|
42
41
|
|
|
43
42
|
export const useFunnelStore = definePageStore('funnelDetail', {
|
|
@@ -66,26 +65,38 @@ export const useFunnelStore = definePageStore('funnelDetail', {
|
|
|
66
65
|
|
|
67
66
|
watch: {
|
|
68
67
|
'filters.platform'(newVal) {
|
|
69
|
-
// platform 变了自动重新拉数据
|
|
70
68
|
this.fetchData();
|
|
71
69
|
},
|
|
72
70
|
},
|
|
71
|
+
|
|
72
|
+
// 0.2.0 新增:页面生命周期
|
|
73
|
+
lifecycle: {
|
|
74
|
+
mount() {
|
|
75
|
+
// 页面挂载时自动拉数据
|
|
76
|
+
this.fetchData();
|
|
77
|
+
},
|
|
78
|
+
unmount() {
|
|
79
|
+
console.log('页面销毁');
|
|
80
|
+
},
|
|
81
|
+
},
|
|
73
82
|
});
|
|
74
83
|
```
|
|
75
84
|
|
|
76
85
|
### 2. 组件中使用
|
|
77
86
|
|
|
78
87
|
```javascript
|
|
79
|
-
// 任意子组件
|
|
80
88
|
export default {
|
|
89
|
+
created() {
|
|
90
|
+
// 传 this → 自动绑定生命周期 + 自动销毁,不需要 beforeDestroy
|
|
91
|
+
this.store = useFunnelStore(this);
|
|
92
|
+
},
|
|
93
|
+
|
|
81
94
|
computed: {
|
|
82
|
-
store() {
|
|
83
|
-
return useFunnelStore();
|
|
84
|
-
},
|
|
85
95
|
isReady() {
|
|
86
96
|
return this.store.isReady;
|
|
87
97
|
},
|
|
88
98
|
},
|
|
99
|
+
|
|
89
100
|
methods: {
|
|
90
101
|
handleSearch() {
|
|
91
102
|
this.store.$patch({ filters: this.localFilters });
|
|
@@ -95,6 +106,8 @@ export default {
|
|
|
95
106
|
};
|
|
96
107
|
```
|
|
97
108
|
|
|
109
|
+
> **不传 `this`** 也完全可以,行为和 0.1.0 一样,需要自己在 `beforeDestroy` 里调 `$destroy()`。
|
|
110
|
+
|
|
98
111
|
### 3. 页面内通信
|
|
99
112
|
|
|
100
113
|
```javascript
|
|
@@ -109,15 +122,73 @@ const off = this.store.$on('filter:change', (filters) => {
|
|
|
109
122
|
// 无需手动清理 —— $destroy 时自动回收
|
|
110
123
|
```
|
|
111
124
|
|
|
112
|
-
|
|
125
|
+
## 页面生命周期(0.2.0)
|
|
126
|
+
|
|
127
|
+
通过 `useStore(this)` 传入组件实例,store 会自动绑定到组件的生命周期:
|
|
128
|
+
|
|
129
|
+
| 组件钩子 | store 行为 | `$status` 变化 |
|
|
130
|
+
|----------|-----------|---------------|
|
|
131
|
+
| `mounted` | 调用 `lifecycle.mount` | `mounted: true, active: true` |
|
|
132
|
+
| `activated` | 调用 `lifecycle.activate` | `active: true` |
|
|
133
|
+
| `deactivated` | 调用 `lifecycle.deactivate` | `active: false` |
|
|
134
|
+
| `beforeDestroy` | 自动调 `$destroy()`,触发 `lifecycle.unmount` | `mounted: false, active: false` |
|
|
135
|
+
|
|
136
|
+
### 无 keep-alive
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
mounted → lifecycle.mount → ... → beforeDestroy → lifecycle.unmount
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### 有 keep-alive
|
|
143
|
+
|
|
144
|
+
```
|
|
145
|
+
mounted → lifecycle.mount
|
|
146
|
+
→ deactivated → lifecycle.deactivate
|
|
147
|
+
→ activated → lifecycle.activate
|
|
148
|
+
→ deactivated → lifecycle.deactivate
|
|
149
|
+
→ activated → lifecycle.activate
|
|
150
|
+
→ ... 反复切换 ...
|
|
151
|
+
→ beforeDestroy → lifecycle.unmount
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### keep-alive 示例
|
|
113
155
|
|
|
114
156
|
```javascript
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
157
|
+
export const useListStore = definePageStore('listPage', {
|
|
158
|
+
state: () => ({
|
|
159
|
+
list: [],
|
|
160
|
+
needRefresh: false,
|
|
161
|
+
}),
|
|
162
|
+
|
|
163
|
+
actions: {
|
|
164
|
+
async fetchList() {
|
|
165
|
+
this.list = await api.getList();
|
|
166
|
+
this.needRefresh = false;
|
|
167
|
+
},
|
|
119
168
|
},
|
|
120
|
-
|
|
169
|
+
|
|
170
|
+
lifecycle: {
|
|
171
|
+
mount() {
|
|
172
|
+
this.fetchList();
|
|
173
|
+
},
|
|
174
|
+
activate() {
|
|
175
|
+
// 从缓存恢复时,按需刷新
|
|
176
|
+
if (this.needRefresh) this.fetchList();
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### `$status` 响应式状态
|
|
183
|
+
|
|
184
|
+
`$status` 是响应式对象,可以直接在模板中使用:
|
|
185
|
+
|
|
186
|
+
```html
|
|
187
|
+
<template>
|
|
188
|
+
<div v-if="store.$status.active">
|
|
189
|
+
<!-- 页面激活时才渲染 -->
|
|
190
|
+
</div>
|
|
191
|
+
</template>
|
|
121
192
|
```
|
|
122
193
|
|
|
123
194
|
## API
|
|
@@ -133,30 +204,71 @@ export default {
|
|
|
133
204
|
| `options.getters` | `Object` | 计算属性,`this` 指向 store |
|
|
134
205
|
| `options.actions` | `Object` | 方法,`this` 指向 store |
|
|
135
206
|
| `options.watch` | `Object` | 声明式侦听,支持点路径 `'a.b.c'` |
|
|
207
|
+
| `options.lifecycle` | `Object` | 页面生命周期钩子 |
|
|
208
|
+
|
|
209
|
+
### `useStore(vm?)`
|
|
210
|
+
|
|
211
|
+
调用 `definePageStore` 返回的函数。
|
|
212
|
+
|
|
213
|
+
| 用法 | 行为 |
|
|
214
|
+
|------|------|
|
|
215
|
+
| `useStore()` | 获取/创建 store,手动管理生命周期(兼容 0.1.0) |
|
|
216
|
+
| `useStore(this)` | 获取/创建 store,自动绑定生命周期 + 自动销毁 |
|
|
217
|
+
|
|
218
|
+
### `lifecycle` 钩子
|
|
219
|
+
|
|
220
|
+
| 钩子 | 触发时机 | `this` 指向 |
|
|
221
|
+
|------|---------|------------|
|
|
222
|
+
| `mount` | 组件 `mounted` | store |
|
|
223
|
+
| `unmount` | 组件 `beforeDestroy`(`$destroy` 时触发) | store |
|
|
224
|
+
| `activate` | 组件 `activated`(keep-alive 恢复) | store |
|
|
225
|
+
| `deactivate` | 组件 `deactivated`(keep-alive 缓存) | store |
|
|
136
226
|
|
|
137
227
|
### Store 实例方法
|
|
138
228
|
|
|
139
229
|
| 方法 | 说明 |
|
|
140
230
|
|------|------|
|
|
141
231
|
| `$patch(partial)` | 浅合并更新 state,接受对象或 `(state) => Object` 函数 |
|
|
142
|
-
| `$subscribe(cb)` | 订阅 state 变化,返回取消函数 |
|
|
143
232
|
| `$reset()` | 重置 state 到初始值 |
|
|
144
233
|
| `$emit(event, payload?)` | 发射事件(仅当前 store 作用域) |
|
|
145
234
|
| `$on(event, handler)` | 订阅事件,返回取消函数 |
|
|
146
235
|
| `$off(event, handler?)` | 取消事件订阅(不传 handler 则取消该事件全部监听) |
|
|
147
236
|
| `$destroy()` | 销毁 store,回收所有资源 |
|
|
237
|
+
| `bindTo(vm)` | 手动绑定到组件实例(通常不需要,`useStore(this)` 内部调用) |
|
|
238
|
+
|
|
239
|
+
### Store 实例属性
|
|
240
|
+
|
|
241
|
+
| 属性 | 说明 |
|
|
242
|
+
|------|------|
|
|
243
|
+
| `$id` | store 唯一标识 |
|
|
244
|
+
| `$state` | 原始响应式 state 对象 |
|
|
245
|
+
| `$status` | 响应式对象 `{ mounted: boolean, active: boolean }` |
|
|
246
|
+
|
|
247
|
+
## 从 0.1.0 升级
|
|
148
248
|
|
|
149
|
-
|
|
249
|
+
**零破坏性变更**。0.1.0 的代码不改一行也能跑。
|
|
150
250
|
|
|
151
|
-
|
|
251
|
+
区别只在于你现在可以把:
|
|
152
252
|
|
|
153
253
|
```javascript
|
|
154
|
-
|
|
254
|
+
created() {
|
|
255
|
+
this.store = useSomeStore();
|
|
256
|
+
},
|
|
257
|
+
beforeDestroy() {
|
|
258
|
+
this.store.$destroy();
|
|
259
|
+
}
|
|
260
|
+
```
|
|
155
261
|
|
|
156
|
-
|
|
157
|
-
|
|
262
|
+
简化成:
|
|
263
|
+
|
|
264
|
+
```javascript
|
|
265
|
+
created() {
|
|
266
|
+
this.store = useSomeStore(this);
|
|
267
|
+
}
|
|
158
268
|
```
|
|
159
269
|
|
|
270
|
+
> `storeToRefs` 和 `$subscribe` 在 0.2.0 中移除——实际项目中无使用场景,精简 API。
|
|
271
|
+
|
|
160
272
|
## 与 Pinia / Vuex 的关系
|
|
161
273
|
|
|
162
274
|
这不是 Pinia 或 Vuex 的替代品,而是补充:
|
|
@@ -164,7 +276,7 @@ const { filters, loading } = storeToRefs(store);
|
|
|
164
276
|
| | Vuex | Pinia | vue-page-store |
|
|
165
277
|
|---|---|---|---|
|
|
166
278
|
| 作用域 | 全局 | 全局 | 页面级 |
|
|
167
|
-
| 生命周期 | 应用级 | 应用级 |
|
|
279
|
+
| 生命周期 | 应用级 | 应用级 | 页面级(自动绑定) |
|
|
168
280
|
| 事件通信 | 无 | 无 | 内置 $emit/$on |
|
|
169
281
|
| Vue 2.6 支持 | ✅ | ⚠️ 需 @vue/composition-api | ✅ 原生支持 |
|
|
170
282
|
| 适用场景 | 用户信息、权限、全局配置 | 同 Vuex | 复杂页面内部状态 |
|
|
@@ -177,4 +289,4 @@ const { filters, loading } = storeToRefs(store);
|
|
|
177
289
|
|
|
178
290
|
## License
|
|
179
291
|
|
|
180
|
-
[MIT](./LICENSE)
|
|
292
|
+
[MIT](./LICENSE)
|
package/dist/index.cjs.js
CHANGED
package/dist/index.esm.js
CHANGED
package/dist/index.umd.js
CHANGED
package/dist/index.umd.min.js
CHANGED