@iuxs/solid-keep-alive 0.0.1
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/LICENSE +21 -0
- package/README.md +225 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 iuxs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# solid-keepalive
|
|
2
|
+
|
|
3
|
+
#### 安装(install)
|
|
4
|
+
|
|
5
|
+
- pnpm add @iuxs/solid-keep-alive / npm i @iuxs/solid-keep-alive / yarn add @iuxs/solid-keep-alive
|
|
6
|
+
|
|
7
|
+
#### 参数(describe)
|
|
8
|
+
|
|
9
|
+
- **AliveProvider**
|
|
10
|
+
- children :JSXElement 必须
|
|
11
|
+
- include : [] 缓存的id 数组, 默认都不缓存, 如 ["home",'abc'], 当你 删除一个缓存时, 父id与子id要一起删除
|
|
12
|
+
|
|
13
|
+
- **aliveTransfer**
|
|
14
|
+
- component : JSXElement, 必须
|
|
15
|
+
- id : string 保存的唯一值, 如 home, 在AliveProvider 的 include 要包括这些id 才会缓存, 必须
|
|
16
|
+
- params : Object
|
|
17
|
+
- isolated : 单独组件, 当不是Route组件 时使用, 默认false, !!!不要在已缓存的组件中使用,不要在已缓存的组件中使用,不要在已缓存的组件中使用
|
|
18
|
+
|
|
19
|
+
- **onActivated** : 激活缓存页面时会触发的函数
|
|
20
|
+
- **onDeactivated** : 离开缓存页面时会触发的函数
|
|
21
|
+
- **useAliveContext** : 父子缓存路由会有 Context 时, 可能会用到的hooks
|
|
22
|
+
<!-- - aliveFrozen: 在你重新生成 Route 时 , 要执行这个函数 1iuxs -->
|
|
23
|
+
|
|
24
|
+
#### 使用(use)
|
|
25
|
+
|
|
26
|
+
1. AliveProvider
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
// src/index.tsx
|
|
30
|
+
import { render } from "solid-js/web"
|
|
31
|
+
|
|
32
|
+
import { AliveProvider, aliveTransfer } from "@iuxs/solid-keep-alive"
|
|
33
|
+
// import { userInfo } from "./store/userInfo.ts"
|
|
34
|
+
|
|
35
|
+
import "@/assets/css/index.css"
|
|
36
|
+
// import Container from "./layout/Container/index.tsx"
|
|
37
|
+
|
|
38
|
+
import { lazy, type Component, For } from "solid-js"
|
|
39
|
+
import { Route, Router } from "@solidjs/router"
|
|
40
|
+
|
|
41
|
+
// 使用 import.meta.glob 动态导入所有组件
|
|
42
|
+
// const modules = import.meta.glob<{ default: Component<any> }>([
|
|
43
|
+
// "./view/**/**.tsx",
|
|
44
|
+
// "./view/**.tsx",
|
|
45
|
+
// ])
|
|
46
|
+
// const transferRouter = (data: MenuData[]) => (
|
|
47
|
+
// <For each={data}>
|
|
48
|
+
// {(item) => {
|
|
49
|
+
// let module = modules[`./view${item.realPath}`]
|
|
50
|
+
// if (!module) return null
|
|
51
|
+
// return (
|
|
52
|
+
// <Route
|
|
53
|
+
// component={aliveTransfer(lazy(module), item.id)}
|
|
54
|
+
// path={item.path}
|
|
55
|
+
// >
|
|
56
|
+
// {item.children ? transferRouter(item.children) : null}
|
|
57
|
+
// </Route>
|
|
58
|
+
// )
|
|
59
|
+
// }}
|
|
60
|
+
// </For>
|
|
61
|
+
// )
|
|
62
|
+
|
|
63
|
+
// 导入 Home 组件
|
|
64
|
+
// import Home from "@/view/Home/index.tsx"
|
|
65
|
+
// const Home1 = aliveTransfer(Home, "home")
|
|
66
|
+
|
|
67
|
+
render(
|
|
68
|
+
() => (
|
|
69
|
+
<AliveProvider include={["home", "about"]}>
|
|
70
|
+
{/* 1 */}
|
|
71
|
+
{/* <App /> */}
|
|
72
|
+
{/* 2 */}
|
|
73
|
+
{/* <Router root={Container} children={routes} /> */}
|
|
74
|
+
{/* 3 */}
|
|
75
|
+
<Router children={routes} />
|
|
76
|
+
{/* 4 */}
|
|
77
|
+
{/* <Router root={Container}>
|
|
78
|
+
{transferRouter([])}
|
|
79
|
+
</Router> */}
|
|
80
|
+
{/* 5 */}
|
|
81
|
+
{/* <Router root={Container}>
|
|
82
|
+
<Route path={"/cart"} component={Hom1}>
|
|
83
|
+
<Route path={""} component={G1}></Route>
|
|
84
|
+
<Route path={"h"} component={H1}></Route>
|
|
85
|
+
</Route>
|
|
86
|
+
</Router> */}
|
|
87
|
+
</AliveProvider>
|
|
88
|
+
),
|
|
89
|
+
document.getElementById("root")!,
|
|
90
|
+
)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
// src/router/index.ts
|
|
95
|
+
import { lazy } from "solid-js"
|
|
96
|
+
import { aliveTransfer } from "@iuxs/solid-keep-alive"
|
|
97
|
+
const routes = [
|
|
98
|
+
{
|
|
99
|
+
path: "/",
|
|
100
|
+
component: aliveTransfer(
|
|
101
|
+
lazy(() => import("@/view/Home/index")),
|
|
102
|
+
"home",
|
|
103
|
+
),
|
|
104
|
+
children: [
|
|
105
|
+
{
|
|
106
|
+
path: "",
|
|
107
|
+
component: aliveTransfer(
|
|
108
|
+
lazy(() => import("@/view/Home/C")),
|
|
109
|
+
"c",
|
|
110
|
+
// {
|
|
111
|
+
// "isolated":true,
|
|
112
|
+
// }
|
|
113
|
+
),
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
path: "/about",
|
|
119
|
+
component: aliveTransfer(
|
|
120
|
+
lazy(() => import("@/view/About/index")),
|
|
121
|
+
"about",
|
|
122
|
+
),
|
|
123
|
+
},
|
|
124
|
+
]
|
|
125
|
+
|
|
126
|
+
export default routes
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
2. Home 组件
|
|
130
|
+
|
|
131
|
+
```tsx
|
|
132
|
+
// src/view/Home/index.tsx
|
|
133
|
+
|
|
134
|
+
import { createContext, useContext } from "solid-js"
|
|
135
|
+
import { onActivated, onDeactivated, type Setter } from "@iuxs/solid-keep-alive"
|
|
136
|
+
import type { RouteSectionProps } from "@solidjs/router"
|
|
137
|
+
export const DataContext = createContext<{
|
|
138
|
+
data: () => string
|
|
139
|
+
setData: Setter<string>
|
|
140
|
+
}>()
|
|
141
|
+
|
|
142
|
+
export default function Home(props: RouteSectionProps) {
|
|
143
|
+
const [data, setData] = createSignal("123")
|
|
144
|
+
onActivated(() => {
|
|
145
|
+
console.log("home - onActivated")
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
onDeactivated(() => {
|
|
149
|
+
console.log("home - onDeactivated")
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
return (
|
|
153
|
+
<ThemeContext.Provider value={{ data, setData }}>
|
|
154
|
+
<div>
|
|
155
|
+
home <button> 清除路由 </button>
|
|
156
|
+
{props.children}
|
|
157
|
+
<div>
|
|
158
|
+
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ut
|
|
159
|
+
praesentium debitis dolorum sed dolorem doloremque aliquam beatae,
|
|
160
|
+
architecto corrupti in qui, harum expedita voluptatum fugit
|
|
161
|
+
necessitatibus suscipit! Animi, nemo quas! Lorem ipsum dolor sit amet
|
|
162
|
+
consectetur adipisicing elit. Reiciendis, quaerat asperiores libero,
|
|
163
|
+
quasi vitae hic officiis repellendus sint tempore totam saepe dolores
|
|
164
|
+
dolorem magni ab suscipit magnam distinctio exercitationem omnis.
|
|
165
|
+
</div>
|
|
166
|
+
</div>
|
|
167
|
+
</ThemeContext.Provider>
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
3. Home 下的 子路由 c.tsx
|
|
173
|
+
|
|
174
|
+
```tsx
|
|
175
|
+
import { getOwner, useContext } from "solid-js"
|
|
176
|
+
import { onActivated, useAliveContext } from "@iuxs/solid-keep-alive"
|
|
177
|
+
import { DataContext } from "."
|
|
178
|
+
|
|
179
|
+
export default function C() {
|
|
180
|
+
// no value
|
|
181
|
+
const data = useContext(DataContext)
|
|
182
|
+
// have value
|
|
183
|
+
const data1 = useAliveContext(DataContext)
|
|
184
|
+
console.log(data, "data")
|
|
185
|
+
console.log(data1, "data1")
|
|
186
|
+
onActivated(() => {
|
|
187
|
+
console.log(getOwner())
|
|
188
|
+
console.log("C - onActivated")
|
|
189
|
+
// console.log(getOwner())
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
return (
|
|
193
|
+
<div>
|
|
194
|
+
ccccccccc <input type="text" /> {data?.value?.()}cccccc {data1?.value?.()}
|
|
195
|
+
</div>
|
|
196
|
+
)
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
4. isolated
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
// @/layout/Container/index.tsx
|
|
204
|
+
import { aliveTransfer } from "@iuxs/solid-keep-alive"
|
|
205
|
+
import type { RouteSectionProps } from "@solidjs/router"
|
|
206
|
+
import Aside from "xxxx"
|
|
207
|
+
|
|
208
|
+
const Aside1 = aliveTransfer(Aside, "aside", { isolated: true })
|
|
209
|
+
|
|
210
|
+
/** 最外的容器 */
|
|
211
|
+
export function Container(props: RouteSectionProps) {
|
|
212
|
+
return (
|
|
213
|
+
<div class="container">
|
|
214
|
+
{props.children}
|
|
215
|
+
<Aside1 />
|
|
216
|
+
</div>
|
|
217
|
+
)
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
#### 已知问题(issue)
|
|
222
|
+
|
|
223
|
+
- 父子路由的 context 问题
|
|
224
|
+
- 多个已缓存的路由有子路由时, 未缓存的子路由 会共享子组件
|
|
225
|
+
- 如果父路由缓存, 子路由不缓存时会有 路由组件共享问题
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@iuxs/solid-keep-alive",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"author": "1iuxs",
|
|
5
|
+
"description": "@iuxs/solid-keep-alive",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.cjs",
|
|
8
|
+
"module": "dist/index.js",
|
|
9
|
+
"types": "dist/types/index.d.ts",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc && rollup -c",
|
|
12
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"solid",
|
|
16
|
+
"solidjs",
|
|
17
|
+
"keep-alive",
|
|
18
|
+
"solid-alive",
|
|
19
|
+
"solid-keep-alive"
|
|
20
|
+
],
|
|
21
|
+
"files": [
|
|
22
|
+
"package.json",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/iuxs/solid-keepalive"
|
|
30
|
+
},
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"solid-js": "^1.9.11"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
37
|
+
"@rollup/plugin-node-resolve": "^15.3.1",
|
|
38
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
39
|
+
"@types/css-modules": "^1.0.5",
|
|
40
|
+
"autoprefixer": "^10.4.27",
|
|
41
|
+
"rollup": "^4.59.0",
|
|
42
|
+
"rollup-plugin-cleanup": "^3.2.1",
|
|
43
|
+
"rollup-plugin-postcss": "^4.0.2",
|
|
44
|
+
"rollup-plugin-serve": "^1.1.1",
|
|
45
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
46
|
+
"rollup-plugin-typescript2": "^0.36.0",
|
|
47
|
+
"rollup-preset-solid": "^2.0.1",
|
|
48
|
+
"typescript": "^5.9.3"
|
|
49
|
+
}
|
|
50
|
+
}
|