-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
144 additions
and
84 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,24 @@ | ||
# 数据 | ||
|
||
数据管理使用详情,参考[plugin-model](https://umijs.org/zh-CN/plugins/plugin-model)。 | ||
|
||
核心思路就是利用 `hooks` 来做 model,是有别于 `redux` 的另一种数据管理手段。 | ||
|
||
我们依旧以登录页面为例讲解。 | ||
|
||
`src/pages/o/login/index.tsx`: | ||
|
||
```typescript | ||
// 业务逻辑被提取到了 src/models/useLoginModel.ts 中 | ||
// 本组件只用到了 initBackground | ||
const { initBackground } = useModel('useLoginModel', m => pick(m, 'initBackground')) | ||
|
||
// 然后组件挂载后,执行该方法,我们就得到了一个酷炫的背景 | ||
useEffect(() => { | ||
initBackground() | ||
}, [initBackground]) | ||
``` | ||
|
||
至于这种新的风格,基于 `hooks` 的 model 如何编写? 那真的简单的一批,就是纯纯的 自定义 `hooks`。 | ||
|
||
打开 `src/models/useLoginModel.ts` 看看就知道了 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
# 国际化 | ||
|
||
国际化使用详情,参考[plugin-locale](https://umijs.org/zh-CN/plugins/plugin-locale)。 | ||
|
||
唯一要注意的是页面的 title。我们依旧以登录页面(`/src/pages/o/login/index.tsx`)为例: | ||
|
||
```typescript | ||
function Login() { | ||
... | ||
... | ||
} | ||
|
||
// title 在应用运行时,会被 src/locales/ 下对应语言文件里的内容替换 | ||
Login.title = 'LOGIN_TITLE' | ||
Login.layout = 'BLANK' | ||
|
||
export default Login | ||
``` | ||
|
||
请看: | ||
|
||
<img :src="$withBase('/locale.gif')" alt="locale"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,23 @@ | ||
import React from 'react' | ||
import BlankResolver from './Blank' | ||
import ProLayoutResolver from './ProLayout' | ||
import { ILayoutProps, ILayoutResolver } from '@/types' | ||
import { isNotEmpty } from '@/helpers/object' | ||
|
||
import Blank from './Blank' | ||
import ProLayout from './ProLayout' | ||
import { isEmpty, isNotEmpty } from '@/helpers/object' | ||
|
||
import { IERoute, ILayoutResolver, ILayoutProps } from '@/types' | ||
|
||
const BlankResolver: ILayoutResolver = { | ||
is(route?: IERoute): boolean { | ||
return isEmpty(route) || route!.layout === 'BLANK' || route?.path === '/' | ||
}, | ||
get({ routes, children, route, canAccess }: ILayoutProps) { | ||
return ( | ||
<Blank route={route} canAccess={canAccess}> | ||
{children} | ||
</Blank> | ||
) | ||
} | ||
} | ||
|
||
const ProLayoutResolver: ILayoutResolver = { | ||
is(route?: IERoute): boolean { | ||
return isEmpty(route) || route!.layout === 'PRO_LAYOUT' | ||
}, | ||
get({ routes, children, route, canAccess }: ILayoutProps) { | ||
return ( | ||
<ProLayout routes={routes!} route={route} canAccess={canAccess}> | ||
{children} | ||
</ProLayout> | ||
) | ||
} | ||
} | ||
|
||
export const OPEN_LAYOUTS = [BlankResolver] | ||
export const AUTH_REQUIRED_LAYOUTS = [ProLayoutResolver, BlankResolver] | ||
const OPEN_LAYOUTS = [BlankResolver] | ||
const AUTH_REQUIRED_LAYOUTS = [ProLayoutResolver, BlankResolver] | ||
|
||
export function resolveOpenPage({ routes, children, route, canAccess }: ILayoutProps) { | ||
const layout = OPEN_LAYOUTS.find(r => r.is(route)) | ||
if (isNotEmpty<ILayoutResolver>(layout)) { | ||
return layout.get({ routes: routes!, children, route, canAccess }) | ||
const resolver = OPEN_LAYOUTS.find(r => r.is(route)) | ||
if (isNotEmpty<ILayoutResolver>(resolver)) { | ||
return resolver.get({ routes: routes!, children, route, canAccess }) | ||
} | ||
throw new Error(`no proper layout found for ${route!.path}, please check your code`) | ||
} | ||
|
||
export function resolveAuthRequiredPage({ routes, children, route, canAccess }: ILayoutProps) { | ||
const layout = AUTH_REQUIRED_LAYOUTS.find(r => r.is(route)) | ||
if (isNotEmpty<ILayoutResolver>(layout)) { | ||
return layout.get({ routes: routes!, children, route, canAccess }) | ||
const resolver = AUTH_REQUIRED_LAYOUTS.find(r => r.is(route)) | ||
if (isNotEmpty<ILayoutResolver>(resolver)) { | ||
return resolver.get({ routes: routes!, children, route, canAccess }) | ||
} | ||
throw new Error(`no proper layout found for ${route!.path}, please check your code`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters