@modern-js/main-doc 2.0.0-canary.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/_category_.json +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/use-module-apps.md +62 -31
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/router/router.md +174 -375
- package/en/docusaurus-plugin-content-docs/current/components/enable-bff.md +36 -0
- package/en/docusaurus-plugin-content-docs/current/components/enable-micro-frontend.md +13 -0
- package/en/docusaurus-plugin-content-docs/current/components/micro-master-manifest-config.md +15 -0
- package/en/docusaurus-plugin-content-docs/current/components/micro-runtime-config.md +18 -0
- package/en/docusaurus-plugin-content-docs/current/components/router-legacy-tip.md +1 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/auto-load-plugin.md +62 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/deploy/microFrontend.md +54 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/ssg.md +226 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/master-app.md +20 -39
- package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/router.md +17 -4
- package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/state.md +17 -4
- package/en/docusaurus-plugin-content-docs/current/configure/app/server/enable-framework-ext.md +47 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/frameworks.md +2 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/function.md +10 -6
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/ssg.md +6 -2
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/data-fetch.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/routes.md +0 -2
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/_category_.json +5 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/extend.md +162 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/hook-list.md +803 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/hook.md +169 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/implement.md +247 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/introduction.md +49 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/plugin-api.md +116 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/relationship.md +118 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/common.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/module.md +3 -1
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/mwa.md +1 -9
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/project.md +2 -2
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c01-introduction.md +29 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c02-development.md +191 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c03-main-app.md +246 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c04-communicate.md +54 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c05-mixed-stack.md +24 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/auto-actions.md +90 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/computed-state.md +151 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/define-model.md +66 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/faq.md +43 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/manage-effects.md +259 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/model-communicate.md +219 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/performance.md +173 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/quick-start.md +116 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/redux-integration.md +21 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/test-model.md +43 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/typescript-best-practice.md +71 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/use-model.md +244 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/use-out-of-modernjs.md +51 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/_category_.json +5 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c01-start.md +99 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c02-component.md +56 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c03-css.md +324 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c04-routes.md +169 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c05-loader.md +82 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c06-model.md +260 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c07-container.md +283 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c08-entries.md +137 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/_category_.json +1 -1
- package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/introduction.md +5 -3
- package/package.json +4 -4
- package/zh/apis/app/runtime/core/use-module-apps.md +2 -0
- package/zh/apis/app/runtime/router/router.md +169 -371
- package/zh/components/enable-bff.md +36 -0
- package/zh/components/micro-master-manifest-config.md +15 -0
- package/zh/components/router-legacy-tip.md +1 -0
- package/zh/configure/app/auto-load-plugin.md +62 -0
- package/zh/configure/app/deploy/microFrontend.md +0 -10
- package/zh/configure/app/output/ssg.md +1 -5
- package/zh/configure/app/runtime/master-app.md +4 -18
- package/zh/configure/app/runtime/router.md +19 -4
- package/zh/configure/app/runtime/state.md +7 -7
- package/zh/configure/app/server/enable-framework-ext.md +47 -0
- package/zh/configure/app/server/port.md +1 -1
- package/zh/configure/app/tools/_category_.json +1 -1
- package/zh/guides/advanced-features/bff/frameworks.md +2 -0
- package/zh/guides/advanced-features/bff/function.md +7 -5
- package/zh/guides/advanced-features/eslint.md +2 -1
- package/zh/guides/advanced-features/ssg.md +4 -0
- package/zh/guides/basic-features/data-fetch.md +1 -1
- package/zh/guides/basic-features/env-vars.md +1 -1
- package/zh/guides/basic-features/routes.md +0 -3
- package/zh/guides/topic-detail/generator/config/module.md +3 -1
- package/zh/guides/topic-detail/generator/config/mwa.md +1 -9
- package/zh/guides/topic-detail/model/quick-start.md +1 -1
- package/zh/tutorials/first-app/c06-model.md +5 -1
- package/zh/tutorials/first-app/c08-entries.md +1 -1
- package/zh/tutorials/foundations/introduction.md +5 -3
- package/en/docusaurus-plugin-content-docs/current/apis/app/overview.md +0 -12
- package/en/docusaurus-plugin-content-docs/current/configure/app/bff/fetcher.md +0 -28
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/with-master-app.md +0 -31
- package/en/docusaurus-plugin-content-docs/current/guides/overview.md +0 -11
- package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/basic.md +0 -8
- package/zh/apis/app/overview.md +0 -11
- package/zh/apis/monorepo/overview.md +0 -11
- package/zh/configure/app/bff/fetcher.md +0 -31
- package/zh/configure/app/dev/with-master-app.md +0 -32
- package/zh/guides/overview.md +0 -11
- package/zh/tutorials/foundations/basic.md +0 -8
package/package.json
CHANGED
@@ -11,20 +11,20 @@
|
|
11
11
|
"modern",
|
12
12
|
"modern.js"
|
13
13
|
],
|
14
|
-
"version": "2.0.0
|
14
|
+
"version": "2.0.0",
|
15
15
|
"publishConfig": {
|
16
16
|
"registry": "https://registry.npmjs.org/",
|
17
17
|
"access": "public"
|
18
18
|
},
|
19
19
|
"peerDependencies": {
|
20
|
-
"@modern-js/builder-doc": "2.0.0
|
20
|
+
"@modern-js/builder-doc": "^2.0.0"
|
21
21
|
},
|
22
22
|
"devDependencies": {
|
23
|
-
"@modern-js/builder-doc": "2.0.0-beta.6",
|
24
23
|
"ts-node": "^10",
|
25
24
|
"fs-extra": "^10",
|
26
25
|
"@types/node": "^16",
|
27
|
-
"@types/fs-extra": "^9"
|
26
|
+
"@types/fs-extra": "^9",
|
27
|
+
"@modern-js/builder-doc": "2.0.0"
|
28
28
|
},
|
29
29
|
"scripts": {
|
30
30
|
"build": "npx ts-node ./scripts/sync.ts"
|
@@ -19,6 +19,7 @@ import { useModuleApps } from '@modern-js/plugin-garfish/runtime';
|
|
19
19
|
## 示例
|
20
20
|
|
21
21
|
需要先配置微前端子应用信息。
|
22
|
+
|
22
23
|
import EnableMicroFrontend from '@site-docs/components/enable-micro-frontend.md';
|
23
24
|
|
24
25
|
<EnableMicroFrontend />
|
@@ -63,6 +64,7 @@ defineConfig(App, {
|
|
63
64
|
|
64
65
|
**集中式路由** 是将子应用的激活路由集中配置的方式。我们给子应用列表信息添加 `activeWhen` 字段来启用 **集中式路由**。
|
65
66
|
|
67
|
+
import MicroRuntimeConfig from '@site-docs/components/micro-runtime-config.md';
|
66
68
|
|
67
69
|
<MicroRuntimeConfig />
|
68
70
|
|
@@ -4,31 +4,39 @@ sidebar_position: 1
|
|
4
4
|
---
|
5
5
|
|
6
6
|
:::info 补充信息
|
7
|
-
基于 [react-router](https://reactrouter.com/
|
8
|
-
:::
|
9
|
-
|
10
|
-
:::caution 注意
|
11
|
-
使用该 API 前,请确认没有禁用 [router 插件](#)。
|
7
|
+
基于 [react-router](https://reactrouter.com/) 的路由解决方案。
|
12
8
|
:::
|
13
9
|
|
14
10
|
## hooks
|
15
11
|
|
16
|
-
###
|
12
|
+
### useNavigate
|
17
13
|
|
18
14
|
```ts
|
19
|
-
function
|
15
|
+
declare function useNavigate(): NavigateFunction;
|
16
|
+
|
17
|
+
interface NavigateFunction {
|
18
|
+
(
|
19
|
+
to: To,
|
20
|
+
options?: {
|
21
|
+
replace?: boolean;
|
22
|
+
state?: any;
|
23
|
+
relative?: RelativeRoutingType;
|
24
|
+
}
|
25
|
+
): void;
|
26
|
+
(delta: number): void;
|
27
|
+
}
|
20
28
|
```
|
21
29
|
|
22
|
-
|
30
|
+
`useNavigate` 返回一个可以用于执行导航操作的函数。
|
23
31
|
|
24
32
|
```tsx
|
25
|
-
import {
|
33
|
+
import { useNavigate } from "@modern-js/runtime/router";
|
26
34
|
|
27
35
|
export function HomeButton() {
|
28
|
-
let
|
36
|
+
let navigate = useNavigate();
|
29
37
|
|
30
38
|
function handleClick() {
|
31
|
-
|
39
|
+
navigate("/home");
|
32
40
|
}
|
33
41
|
|
34
42
|
return (
|
@@ -42,20 +50,18 @@ export function HomeButton() {
|
|
42
50
|
### useLocation
|
43
51
|
|
44
52
|
```ts
|
45
|
-
function useLocation
|
53
|
+
declare function useLocation(): Location;
|
54
|
+
|
55
|
+
interface Location extends Path {
|
56
|
+
state: unknown;
|
57
|
+
key: Key;
|
58
|
+
}
|
46
59
|
```
|
47
60
|
|
48
61
|
`useLocation` 返回当前 url 对应的 [location](https://reactrouter.com/web/api/location) 对象。每当路由更新的时候,都会拿到一个新的 `location` 对象。
|
49
62
|
|
50
63
|
```ts
|
51
|
-
import
|
52
|
-
import { bootstrap, createApp } from '@modern-js/runtime';
|
53
|
-
import { router } from '@modern-js/runtime/plugins';
|
54
|
-
import {
|
55
|
-
BrowserRouter as Router,
|
56
|
-
Switch,
|
57
|
-
useLocation
|
58
|
-
} from "@modern-js/runtime/router";
|
64
|
+
import { useLocation } from "@modern-js/runtime/router";
|
59
65
|
|
60
66
|
function usePageViews() {
|
61
67
|
let location = useLocation();
|
@@ -66,24 +72,23 @@ function usePageViews() {
|
|
66
72
|
|
67
73
|
function App() {
|
68
74
|
usePageViews();
|
69
|
-
return
|
75
|
+
return (
|
76
|
+
//...
|
77
|
+
);
|
70
78
|
}
|
71
79
|
```
|
72
80
|
|
73
81
|
### useParams
|
74
82
|
|
75
83
|
```ts
|
76
|
-
function useParams<
|
77
|
-
|
78
|
-
|
79
|
-
} = {}
|
80
|
-
>(): Params;
|
84
|
+
declare function useParams<
|
85
|
+
K extends string = string
|
86
|
+
>(): Readonly<Params<K>>;
|
81
87
|
```
|
82
88
|
|
83
|
-
`useParams` 返回一个 key/value
|
89
|
+
`useParams` 返回一个 key/value 的键值对,表示当前匹配的路由 `<Route path>` 中的参数信息。
|
84
90
|
|
85
91
|
```tsx
|
86
|
-
import React from "react";
|
87
92
|
import {
|
88
93
|
Switch,
|
89
94
|
Route,
|
@@ -96,44 +101,34 @@ function BlogPost() {
|
|
96
101
|
}
|
97
102
|
|
98
103
|
function App() {
|
99
|
-
return
|
100
|
-
<
|
101
|
-
<div>home</div>
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
</Route>
|
106
|
-
</Switch>
|
104
|
+
return (
|
105
|
+
<Routes>
|
106
|
+
<Route path="/" element={ <div>home</div> } />
|
107
|
+
<Route path="/blog/:slug" element={ <BlogPost />}/>
|
108
|
+
</Routes>
|
109
|
+
)
|
107
110
|
}
|
108
111
|
```
|
109
112
|
|
110
|
-
### useRouteMatch
|
111
|
-
|
112
|
-
```ts
|
113
|
-
function useRouteMatch<
|
114
|
-
Params extends { [K in keyof Params]?: string } = {}
|
115
|
-
>(): match<Params>;
|
116
|
-
|
117
|
-
function useRouteMatch<
|
118
|
-
Params extends { [K in keyof Params]?: string } = {}
|
119
|
-
>(
|
120
|
-
path: string | string[] | RouteProps,
|
121
|
-
): match<Params> | null;
|
122
|
-
```
|
123
|
-
|
124
|
-
`useRouteMatch` 和 `<Route />` 一样是对路由进行匹配,但无须去渲染 `<Route />` 组件,便能获取到当前匹配结果。
|
125
|
-
|
126
113
|
## 组件
|
127
114
|
|
128
|
-
|
129
|
-
|
130
115
|
### Link
|
131
116
|
|
132
117
|
```ts
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
118
|
+
declare function Link(props: LinkProps): React.ReactElement;
|
119
|
+
|
120
|
+
interface LinkProps
|
121
|
+
extends Omit<
|
122
|
+
React.AnchorHTMLAttributes<HTMLAnchorElement>,
|
123
|
+
"href"
|
124
|
+
> {
|
125
|
+
replace?: boolean;
|
126
|
+
state?: any;
|
127
|
+
to: To;
|
128
|
+
reloadDocument?: boolean;
|
129
|
+
}
|
130
|
+
|
131
|
+
type To = string | Partial<Path>;
|
137
132
|
```
|
138
133
|
|
139
134
|
可以使用 `Link` 组件进行路由跳转。
|
@@ -142,351 +137,154 @@ interface Link<S = H.LocationState>
|
|
142
137
|
<Link to="/about">About</Link>
|
143
138
|
```
|
144
139
|
|
145
|
-
#### LinkProps
|
146
|
-
|
147
|
-
**to**
|
148
|
-
|
149
|
-
类型:`string | object | function`
|
150
|
-
|
151
|
-
`string`
|
152
|
-
|
153
|
-
```ts
|
154
|
-
<Link to="/courses?sort=name" />
|
155
|
-
```
|
156
|
-
|
157
|
-
`object`
|
158
|
-
|
159
|
-
```tsx
|
160
|
-
<Link
|
161
|
-
to={{
|
162
|
-
pathname: "/courses",
|
163
|
-
search: "?sort=name",
|
164
|
-
hash: "#the-hash",
|
165
|
-
state: { fromDashboard: true }
|
166
|
-
}}
|
167
|
-
/>
|
168
|
-
```
|
169
|
-
|
170
|
-
`function`
|
171
|
-
|
172
|
-
```tsx
|
173
|
-
<Link to={location => ({ ...location, pathname: "/courses" })} />
|
174
|
-
|
175
|
-
<Link to={location => `${location.pathname}?sort=name`} />
|
176
|
-
```
|
177
|
-
|
178
|
-
**replace**
|
179
|
-
|
180
|
-
类型:`boolean`
|
181
|
-
|
182
|
-
当设置为 `true` 时,在跳转的时候替换掉 history 栈中的栈顶路由,而不是添加一个新路由。
|
183
|
-
|
184
|
-
**component**
|
185
|
-
|
186
|
-
类型:`Component`
|
187
|
-
|
188
|
-
如果你想自定义你自己的路由跳转的组件,可以通过传入 `component` 来实现。
|
189
|
-
|
190
|
-
```tsx
|
191
|
-
simply do so by passing it through the component prop.const FancyLink = React.forwardRef((props, ref) => (
|
192
|
-
<a ref={ref} {...props}>💅 {props.children}</a>
|
193
|
-
))
|
194
|
-
|
195
|
-
<Link to="/" component={FancyLink} />
|
196
|
-
```
|
197
|
-
|
198
140
|
### NavLink
|
199
141
|
|
200
142
|
```ts
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
类型:`object`
|
227
|
-
|
228
|
-
设置路由匹配时额外的样式。
|
229
|
-
|
230
|
-
```tsx
|
231
|
-
<NavLink
|
232
|
-
to="/faq"
|
233
|
-
activeStyle={{
|
234
|
-
fontWeight: "bold",
|
235
|
-
color: "red"
|
236
|
-
}}
|
237
|
-
>
|
238
|
-
FAQs
|
239
|
-
</NavLink>
|
240
|
-
```
|
241
|
-
|
242
|
-
**exact**
|
243
|
-
|
244
|
-
类型:`boolean`
|
245
|
-
|
246
|
-
|
247
|
-
**strict**
|
248
|
-
|
249
|
-
类型:`boolean`
|
250
|
-
|
251
|
-
**isActive**
|
252
|
-
|
253
|
-
类型:`function`
|
254
|
-
|
255
|
-
如果你想自定义当前 Link 是否激活的逻辑,可以使用 `isActive`。
|
256
|
-
|
257
|
-
```tsx
|
258
|
-
<NavLink
|
259
|
-
to="/events/123"
|
260
|
-
isActive={(match, location) => {
|
261
|
-
if (!match) {
|
262
|
-
return false;
|
263
|
-
}
|
264
|
-
|
265
|
-
// only consider an event active if its event id is an odd number
|
266
|
-
const eventID = parseInt(match.params.eventID);
|
267
|
-
return !isNaN(eventID) && eventID % 2 === 1;
|
268
|
-
}}
|
269
|
-
>
|
270
|
-
Event 123
|
271
|
-
</NavLink>
|
272
|
-
```
|
273
|
-
|
274
|
-
**location**
|
275
|
-
|
276
|
-
类型:`object`
|
277
|
-
|
278
|
-
`NavLink` 默认会和当前的 `history.location` 进行匹配,判断是否处于激活状态。如果你想指定要匹配的 `location` 对象,可以使用该参数。
|
279
|
-
|
280
|
-
**area-current**
|
281
|
-
|
282
|
-
类型:`string`
|
283
|
-
|
284
|
-
参考 [aria-current](https://www.w3.org/TR/wai-aria-1.1/#aria-current)
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
### Prompt
|
289
|
-
|
290
|
-
```ts
|
291
|
-
interface PromptProps {
|
292
|
-
message: string | ((location: H.Location, action: H.Action) => string | boolean);
|
293
|
-
when?: boolean;
|
143
|
+
declare function NavLink(
|
144
|
+
props: NavLinkProps
|
145
|
+
): React.ReactElement;
|
146
|
+
|
147
|
+
interface NavLinkProps
|
148
|
+
extends Omit<
|
149
|
+
LinkProps,
|
150
|
+
"className" | "style" | "children"
|
151
|
+
> {
|
152
|
+
caseSensitive?: boolean;
|
153
|
+
children?:
|
154
|
+
| React.ReactNode
|
155
|
+
| ((props: { isActive: boolean }) => React.ReactNode);
|
156
|
+
className?:
|
157
|
+
| string
|
158
|
+
| ((props: {
|
159
|
+
isActive: boolean;
|
160
|
+
}) => string | undefined);
|
161
|
+
end?: boolean;
|
162
|
+
style?:
|
163
|
+
| React.CSSProperties
|
164
|
+
| ((props: {
|
165
|
+
isActive: boolean;
|
166
|
+
}) => React.CSSProperties);
|
294
167
|
}
|
295
|
-
|
296
|
-
class Prompt extends React.Component<PromptProps, any> {}
|
297
|
-
```
|
298
|
-
|
299
|
-
`Prompt` 组件可用于在页面跳转前,进行二次确认是否跳转。
|
300
|
-
|
301
|
-
```tsx
|
302
|
-
<Prompt
|
303
|
-
when={formIsHalfFilledOut}
|
304
|
-
message="Are you sure you want to leave?"
|
305
|
-
/>
|
306
168
|
```
|
307
169
|
|
308
|
-
|
309
|
-
|
310
|
-
**message**
|
311
|
-
|
312
|
-
类型:`string` | `function`
|
170
|
+
`<NavLink>` 是一种特殊的 `<Link>`,当 `<NavLink>` 对应的链接和当前 URL 匹配时,会给 `<NavLink>` 所渲染的元素添加“激活”态样式。
|
313
171
|
|
314
|
-
在页面跳转前的二次确认提示信息,支持传入函数形式。
|
315
|
-
|
316
|
-
```tsx
|
317
|
-
<Prompt
|
318
|
-
message={(location, action) => {
|
319
|
-
if (action === 'POP') {
|
320
|
-
console.log("Backing up...")
|
321
|
-
}
|
322
|
-
|
323
|
-
return location.pathname.startsWith("/app")
|
324
|
-
? true
|
325
|
-
: `Are you sure you want to go to ${location.pathname}?`
|
326
|
-
}}
|
327
|
-
/>
|
328
|
-
```
|
329
172
|
|
330
|
-
|
331
|
-
|
332
|
-
类型:`boolean`
|
333
|
-
|
334
|
-
当 `when` 为 `true` 时,才会在页面跳转前展示二次确认提示。
|
335
|
-
|
336
|
-
|
337
|
-
### Route
|
173
|
+
### Outlet
|
338
174
|
|
339
175
|
```ts
|
340
|
-
interface
|
341
|
-
|
342
|
-
Params extends { [K: string]: string | undefined } = ExtractRouteParams<Path, string>
|
343
|
-
> {
|
344
|
-
location?: H.Location;
|
345
|
-
component?: React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any>;
|
346
|
-
render?: (props: RouteComponentProps<Params>) => React.ReactNode;
|
347
|
-
children?: ((props: RouteChildrenProps<Params>) => React.ReactNode) | React.ReactNode;
|
348
|
-
path?: Path | Path[];
|
349
|
-
exact?: boolean;
|
350
|
-
sensitive?: boolean;
|
351
|
-
strict?: boolean;
|
176
|
+
interface OutletProps {
|
177
|
+
context?: unknown;
|
352
178
|
}
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
any
|
357
|
-
> {}
|
179
|
+
declare function Outlet(
|
180
|
+
props: OutletProps
|
181
|
+
): React.ReactElement | null;
|
358
182
|
```
|
359
183
|
|
360
|
-
|
361
|
-
|
362
|
-
#### component
|
363
|
-
|
364
|
-
类型:`React.ComponentType`
|
365
|
-
|
366
|
-
当路由匹配成功,会渲染传入 `component` 的组件。
|
184
|
+
`<Outlet>` 用于嵌套路由场景。在父路由的元素中使用,代表待渲染的子路由的元素在父组件中的位置。
|
367
185
|
|
368
186
|
```tsx
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
187
|
+
function Dashboard() {
|
188
|
+
return (
|
189
|
+
<div>
|
190
|
+
<h1>Dashboard</h1>
|
191
|
+
|
192
|
+
{/* This element will render either <DashboardMessages> when the URL is
|
193
|
+
"/messages", <DashboardTasks> at "/tasks", or null if it is "/"
|
194
|
+
*/}
|
195
|
+
<Outlet />
|
196
|
+
</div>
|
197
|
+
);
|
375
198
|
}
|
376
199
|
|
377
200
|
function App() {
|
378
|
-
return
|
201
|
+
return (
|
202
|
+
<Routes>
|
203
|
+
<Route path="/" element={<Dashboard />}>
|
204
|
+
<Route
|
205
|
+
path="messages"
|
206
|
+
element={<DashboardMessages />}
|
207
|
+
/>
|
208
|
+
<Route path="tasks" element={<DashboardTasks />} />
|
209
|
+
</Route>
|
210
|
+
</Routes>
|
211
|
+
);
|
379
212
|
}
|
380
213
|
```
|
381
214
|
|
382
|
-
|
383
|
-
如果 component 是一个 inline function,如 `<Route path="/user/:username" component={() => 'hello'} />`,因为每次 rerender 的时候,component 都会是一个新的 `type`,所以 component 组件会先 unmount,再 mount。我们需要避免这种写法,或者使用 render 代替 component。
|
384
|
-
:::
|
385
|
-
|
386
|
-
#### render
|
387
|
-
|
388
|
-
类型:`(props: RouteComponentProps<Params>) => React.ReactNode`
|
389
|
-
|
390
|
-
允许使用 `inline function` 进行渲染,同时不会有 `component` remounting 的问题。
|
391
|
-
|
392
|
-
```tsx
|
393
|
-
import React from "react";
|
394
|
-
import { Route } from "@modern-js/runtime/router";
|
215
|
+
### Route
|
395
216
|
|
396
|
-
|
397
|
-
|
217
|
+
```ts
|
218
|
+
interface RouteObject {
|
219
|
+
path?: string;
|
220
|
+
index?: boolean;
|
221
|
+
children?: React.ReactNode;
|
222
|
+
caseSensitive?: boolean;
|
223
|
+
id?: string;
|
224
|
+
loader?: LoaderFunction;
|
225
|
+
action?: ActionFunction;
|
226
|
+
element?: React.ReactNode | null;
|
227
|
+
errorElement?: React.ReactNode | null;
|
228
|
+
handle?: RouteObject["handle"];
|
229
|
+
shouldRevalidate?: ShouldRevalidateFunction;
|
398
230
|
}
|
399
231
|
```
|
400
232
|
|
401
|
-
|
402
|
-
component 的优先级高于 render。
|
403
|
-
:::
|
233
|
+
`Route` 用于定义路由。一个 `Route` 对象,将 URL 片段和组件、数据获取关联起来。
|
404
234
|
|
405
|
-
|
235
|
+
`Route` 可以作为普通对象,传给创建 router 的函数的参数:
|
406
236
|
|
407
|
-
|
237
|
+
```ts
|
238
|
+
const router = createBrowserRouter([
|
239
|
+
{
|
240
|
+
// it renders this element
|
241
|
+
element: <Team />,
|
242
|
+
|
243
|
+
// when the URL matches this segment
|
244
|
+
path: "teams/:teamId",
|
245
|
+
|
246
|
+
// with this data loaded before rendering
|
247
|
+
loader: async ({ request, params }) => {
|
248
|
+
return fetch(
|
249
|
+
`/fake/api/teams/${params.teamId}.json`,
|
250
|
+
{ signal: request.signal }
|
251
|
+
);
|
252
|
+
},
|
253
|
+
|
254
|
+
// performing this mutation when data is submitted to it
|
255
|
+
action: async ({ request }) => {
|
256
|
+
return updateFakeTeam(await request.formData());
|
257
|
+
},
|
258
|
+
|
259
|
+
// and renders this element in case something went wrong
|
260
|
+
errorElement: <ErrorBoundary />,
|
261
|
+
},
|
262
|
+
]);
|
263
|
+
```
|
408
264
|
|
409
|
-
|
265
|
+
`Route` 还可以作为组件使用,通过 `createRoutesFromElements` 转换后,再传给创建 router 的函数的参数:
|
410
266
|
|
411
|
-
```
|
412
|
-
|
413
|
-
|
267
|
+
```ts
|
268
|
+
const router = createBrowserRouter(
|
269
|
+
createRoutesFromElements(
|
414
270
|
<Route
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
271
|
+
element={<Team />}
|
272
|
+
path="teams/:teamId"
|
273
|
+
loader={async ({ params }) => {
|
274
|
+
return fetch(
|
275
|
+
`/fake/api/teams/${params.teamId}.json`
|
276
|
+
);
|
277
|
+
}}
|
278
|
+
action={async ({ request }) => {
|
279
|
+
return updateFakeTeam(await request.formData());
|
280
|
+
}}
|
281
|
+
errorElement={<ErrorBoundary />}
|
421
282
|
/>
|
422
|
-
)
|
423
|
-
|
424
|
-
|
425
|
-
function App() {
|
426
|
-
return <ul>
|
427
|
-
<ListItemLink to="/somewhere" />
|
428
|
-
<ListItemLink to="/somewhere-else" />
|
429
|
-
</ul>;
|
430
|
-
}
|
283
|
+
)
|
284
|
+
);
|
431
285
|
```
|
432
286
|
|
433
|
-
|
434
|
-
|
435
|
-
类型:`string | string[]`
|
436
|
-
|
437
|
-
符合 [path-to-regexp@^1.7.0](https://github.com/pillarjs/path-to-regexp/tree/v1.7.0) 匹配规则的 url 字符串或数组。
|
438
|
-
|
439
|
-
```tsx
|
440
|
-
<Route path="/users/:id">
|
441
|
-
<User />
|
442
|
-
</Route>
|
443
|
-
|
444
|
-
<Route path={["/users/:id", "/profile/:id"]}>
|
445
|
-
<User />
|
446
|
-
</Route>
|
447
|
-
```
|
448
|
-
|
449
|
-
#### exact
|
450
|
-
|
451
|
-
类型:`boolean`
|
452
|
-
|
453
|
-
默认值:`false`
|
454
|
-
|
455
|
-
当 `exact` 值为 `true` 时,会进行准确匹配。
|
456
|
-
|
457
|
-
| path | location.pathname | exact | matches? |
|
458
|
-
| - | - | - | - |
|
459
|
-
| /one | /one/two | true | no |
|
460
|
-
| /one | /one/two | false | yes |
|
461
|
-
|
462
|
-
|
463
|
-
#### strict
|
464
|
-
|
465
|
-
类型:`boolean`
|
466
|
-
|
467
|
-
默认值:`false`
|
468
|
-
|
469
|
-
当 `strict` 值为 `true` 时,会进行严格匹配。若 `path` 以 '/' 结尾,那么 `location.pathname` 也需要以 '/' 结尾,才能匹配。
|
470
|
-
|
471
|
-
| path | location.pathname | matches? |
|
472
|
-
| - | - | - |
|
473
|
-
| /one/ | /one | no |
|
474
|
-
| /one/ | /one/ | yes |
|
475
|
-
| /one/| /one/two | yes |
|
476
|
-
|
477
|
-
#### sensitive
|
478
|
-
|
479
|
-
类型:`boolean`
|
480
|
-
|
481
|
-
默认值:`false`
|
482
|
-
|
483
|
-
当 `sensitive` 设置为 `true`,则 path 大小写不敏感。
|
484
|
-
|
485
|
-
#### location
|
486
|
-
|
487
|
-
类型:`object`
|
488
|
-
|
489
|
-
## 更多底层 API
|
287
|
+
## 更多 API
|
490
288
|
|
491
|
-
|
289
|
+
若想要了解完整的路由 API 信息,可至 [react-router 官网](https://reactrouter.com/web/guides/start) 查看。
|
492
290
|
|