Skip to content

Commit dde1df4

Browse files
committed
这ts真难配啊
1 parent a08ec49 commit dde1df4

File tree

14 files changed

+65
-99
lines changed

14 files changed

+65
-99
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: node_js
22
node_js:
3-
- '10'
4-
- '8'
3+
- "10"
4+
- "8"
55
script:
6-
- yarn dll
76
- yarn build

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
"@types/axios": "^0.14.0",
2929
"@types/react": "^16.9.5",
3030
"@types/react-dom": "^16.9.1",
31+
"@types/react-loadable": "^5.5.1",
32+
"@types/react-router-dom": "^5.1.0",
3133
"antd": "^3.23.6",
3234
"axios": "^0.19.0",
3335
"core-js": "^3.2.1",

src/container/routers/index.js renamed to src/container/routers/index.tsx

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,30 @@ import "./index.less";
2424
const Home = Loadable({
2525
loader: () => import(/* webpackChunkName:'home' */ "../home"),
2626
loading: Loading, // 自定义的Loading动画组件
27-
timeout: 10000 // 可以设置一个超时时间(s)来应对网络慢的情况(在Loading动画组件中可以配置error信息)
27+
timeout: 10000, // 可以设置一个超时时间(s)来应对网络慢的情况(在Loading动画组件中可以配置error信息)
2828
});
2929
const Test = Loadable({
3030
loader: () => import(/* webpackChunkName:'test' */ "../test"),
31-
loading: Loading
31+
loading: Loading,
3232
});
3333
const TestClass = Loadable({
3434
loader: () => import(/* webpackChunkName:'testclass' */ "../testclass"),
35-
loading: Loading
35+
loading: Loading,
3636
});
3737
const Features = Loadable({
3838
loader: () => import(/* webpackChunkName:'features' */ "../features"),
39-
loading: Loading
39+
loading: Loading,
4040
});
4141
const NotFound = Loadable({
4242
loader: () => import(/* webpackChunkName:'notfound' */ "../notfound"),
43-
loading: Loading
43+
loading: Loading,
4444
});
4545

4646
const history = createHistory(); // 实例化history对象
4747

4848
/** 组件 **/
49-
function RootRouterContainer(props) {
49+
function RootRouterContainer(props: any) {
50+
console.log("父级参数:", props);
5051
// 在组件加载完毕后触发
5152
useEffect(() => {
5253
// 可以手动在此预加载指定的模块:
@@ -57,7 +58,7 @@ function RootRouterContainer(props) {
5758
}, []);
5859

5960
/** 简单权限控制 **/
60-
function onEnter(Component, props) {
61+
function onEnter(Component: any, props: any) {
6162
// 例子:如果没有登录,直接跳转至login页
6263
// if (sessionStorage.getItem('userInfo')) {
6364
// return <Component {...props} />;
@@ -77,22 +78,10 @@ function RootRouterContainer(props) {
7778
<div className="boss">
7879
<Switch>
7980
<Redirect exact from="/" to="/home" />
80-
<Route
81-
path="/home"
82-
render={props => onEnter(Home, props)}
83-
/>
84-
<Route
85-
path="/features"
86-
render={props => onEnter(Features, props)}
87-
/>
88-
<Route
89-
path="/test"
90-
render={props => onEnter(Test, props)}
91-
/>
92-
<Route
93-
path="/testclass"
94-
render={props => onEnter(TestClass, props)}
95-
/>
81+
<Route path="/home" render={props => onEnter(Home, props)} />
82+
<Route path="/features" render={props => onEnter(Features, props)} />
83+
<Route path="/test" render={props => onEnter(Test, props)} />
84+
<Route path="/testclass" render={props => onEnter(TestClass, props)} />
9685
<Route component={NotFound} />
9786
</Switch>
9887
<Menu />
@@ -110,6 +99,6 @@ function RootRouterContainer(props) {
11099
export default connect(
111100
state => ({}),
112101
dispatch => ({
113-
actions: {}
114-
})
102+
actions: {},
103+
}),
115104
)(RootRouterContainer);

src/global.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
declare const axios: any;

src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/** APP入口 **/
22
import "core-js/stable";
33
import "regenerator-runtime/runtime";
4-
import React from "react";
5-
import ReactDOM from "react-dom";
4+
import * as React from "react";
5+
import * as ReactDOM from "react-dom";
66

77
import * as serviceWorker from "./serviceWorker";
88
import Root from "./root";

src/models/app.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1+
import { prototype } from "stream";
2+
13
/**
24
* 基本Model app.js, 在src/store/index.js中被挂载到store上,命名为app
35
* 可用于存放通用信息,比如用户数据、角色、权限、省市区等通用数据
46
* **/
7+
interface IParams {
8+
id?: string | number;
9+
[propName: string]: any;
10+
}
11+
512
export default {
613
/** store数据 **/
714
state: {
8-
userinfo: null // 用户信息
15+
userinfo: null, // 用户信息
916
},
1017
/** reducers **/
1118
reducers: {
12-
setUserInfo(state, payload) {
19+
setUserInfo(state: object, payload: object) {
1320
return Object.assign({}, state, {
14-
userinfo: payload
21+
userinfo: payload,
1522
});
16-
}
23+
},
1724
},
1825
/** actions 可以是一个对象,也可以是一个函数,函数的第1个参数自动被注入dispatch **/
1926
effects: {
2027
// 模拟获取用户信息
21-
async getUserinfo(params = {}) {
28+
async getUserinfo(params: IParams = {}) {
2229
const user = { id: params.id, username: "admin" };
2330
this.setUserInfo(user);
2431
return user;
25-
}
26-
}
32+
},
33+
},
2734
};

src/models/test.js renamed to src/models/test.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,39 @@
66
import { message } from "antd";
77
import Server from "../util/server"; // 自己封装的异步请求方法
88

9+
interface IParams {
10+
id?: string | number;
11+
[propName: string]: any;
12+
}
13+
914
export default {
1015
/** store数据 **/
1116
state: {
1217
count: 0, // 测试数字
13-
fetchvalue: [] // 异步请求的测试数据
18+
fetchvalue: [], // 异步请求的测试数据
1419
},
1520

1621
/** reducers **/
1722
reducers: {
18-
setCount(state, payload) {
23+
setCount(state: object, payload: number) {
1924
return Object.assign({}, state, {
20-
count: payload
25+
count: payload,
2126
});
2227
},
23-
setFetchValue(state, payload) {
28+
setFetchValue(state: object, payload: Array<any>) {
2429
return Object.assign({}, state, {
25-
fetchvalue: payload
30+
fetchvalue: payload,
2631
});
27-
}
32+
},
2833
},
2934
/** actions **/
30-
effects: dispatch => ({
35+
effects: (dispatch: Function) => ({
3136
// 测试 - 数字加1
32-
onTestAdd(params) {
37+
onTestAdd(params: number) {
3338
this.setCount(params + 1); // 这里会指向上面reducers中的setCount
3439
},
3540
// 测试 - 异步请求
36-
async serverFetch(params = {}) {
41+
async serverFetch(params: IParams = {}) {
3742
try {
3843
const res = await Server.newServer("url.ajax", params, "post");
3944
if (res && res.data.status === 200) {
@@ -43,6 +48,6 @@ export default {
4348
} catch (e) {
4449
message.error("网络错误", 1);
4550
}
46-
}
47-
})
51+
},
52+
}),
4853
};

src/root/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* */
66

77
/** 所需的各种插件 **/
8-
import React from "react";
8+
import * as React from "react";
99
import { Provider } from "react-redux";
1010
import store from "../store";
1111
import Routers from "../container/routers";

src/store/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ import test from "../models/test";
77
export default init({
88
models: {
99
app, // 这里的命名很重要,即这个模块的名字
10-
test
11-
}
10+
test,
11+
},
1212
});

src/util/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import axios, { Method } from "axios";
77

88
export default class ApiService {
9-
static newServer(url: string, bodyObj: object = {}, method: Method = "post") {
9+
static newServer(url: string, bodyObj: object = {}, method: Method = "post"): Promise<any> {
1010
return axios({
1111
url,
1212
method,

0 commit comments

Comments
 (0)