把index.js改成index.tsx,重新启动项目
import React from ‘react’;
import {getHeroList}from ‘…/…/services/heros’
export default () => {
// fetch(‘/api/herolist’).then(res=>{
// //console.log(res)
// return res.json()
// }).then(res=>{
// console.log(res)
// })
// fetch(‘/api/heros’).then(res=>{
// //console.log(res)
// return res.json()
// }).then(res=>{
// console.log(res)
// })
getHeroList().then((res)=>{
console.log(res)
})
return (
<>
mall新页面
</>
);
};
数据请求后放到状态管理model
全局数据放到全局model,单页面数据放到页面文件夹model(方便识别管理)
这里在model文件夹下面新建mall.ts
import {Effect,Reducer} from ‘umi’
import{getHeroList}from ‘…/services/heros’
const HerosModel:any={
//命名空间
namespace:‘heros’,
//状态
state:{
herolist:[],
},
effects:{
*fetchHeroList(_,{call,put}){
const res= yield call(getHeroList)
//console.log(res)
yield put({
type:‘saveHerolist’,
payload:res
})
}
},
reducers:{
saveHerolist(state:any,action:any){
return{
…state,
herolist:action.payload
}
}
}
}
export default HerosModel
然后修改mall/index.tsx
import React, { useEffect } from ‘react’;
import { getHeroList } from ‘…/…/services/heros’
import { connect, Dispatch } from ‘umi’
const HeroMall = (props: any) => {
console.log(props)
useEffect(() => {
let { dispatch } = props
dispatch({
type: ‘heros/fetchHeroList’,
})
}, [])
// fetch(‘/api/herolist’).then(res=>{
// //console.log(res)
// return res.json()
// }).then(res=>{
// console.log(res)
// })
// fetch(‘/api/heros’).then(res=>{
// //console.log(res)
// return res.json()
// }).then(res=>{
// console.log(res)
// })
// getHeroList().then((res)=>{
// console.log(res)
// })
return (
<>
mall新页面
</>
);
}
export default connect(({ heros }: any) => ({ heros }))(HeroMall)
这里注意规范的import和export
另外connect的两个括号第一个放(返回值为(对象)的函数)形如({ heros }: any) => ({ heros })
,不然取不到state,第二个放组件
import React, { useEffect } from ‘react’;
// import { getHeroList } from ‘…/…/services/heros’
import { connect, Dispatch } from ‘umi’
const HeroMall = (props: any) => {
console.log(props)
useEffect(() => {
let { dispatch } = props
dispatch({
type: ‘heros/fetchHeroList’,
})
}, [])
// fetch(‘/api/herolist’).then(res=>{
// //console.log(res)
// return res.json()
// }).then(res=>{
// console.log(res)
// })
// fetch(‘/api/heros’).then(res=>{
// //console.log(res)
// return res.json()
// }).then(res=>{
// console.log(res)
// })
// getHeroList().then((res)=>{
// console.log(res)
// })
let { heros } = props
let herolist = heros && heros.herolist && heros.herolist.herolist
console.log(herolist)
return (
<>
mall--王者荣耀英雄
{herolist&&herolist.map((item:any,index:number)=>{
return(
- {item.cname}{'->'}{item.title}
)
})}
</>
);
}
export default connect(({ heros }: any) => ({ heros }))(HeroMall)
之后引入个Table做好看一点
import React, { useEffect } from ‘react’;
// import { getHeroList } from ‘…/…/services/heros’
import { connect, Dispatch } from ‘umi’
import { Table, Tag } from ‘antd’;
const HeroMall = (props: any) => {
console.log(props)
useEffect(() => {
let { dispatch } = props
dispatch({
type: ‘heros/fetchHeroList’,
})//触发model中的effect,带上命名空间
}, [])
// fetch(‘/api/herolist’).then(res=>{
// //console.log(res)
// return res.json()
// }).then(res=>{
// console.log(res)
// })
// fetch(‘/api/heros’).then(res=>{
// //console.log(res)
// return res.json()
// }).then(res=>{
// console.log(res)
// })
// getHeroList().then((res)=>{
// console.log(res)
// })
let { heros } = props
let herolist = heros && heros.herolist && heros.herolist.herolist
console.log(herolist)
// ename: 105
// cname: “廉颇”
// title: “正义爆轰”
// new_type: 0
// hero_type: 3
// skin_name: “正义爆轰|地狱岩魂”
const columns = [
{
title: ‘Id’,
dataIndex: ‘ename’,
key: ‘ename’,
render: text => {text},
},
{
title: ‘英雄’,
dataIndex: ‘cname’,
key: ‘cname’,
},
{
title: ‘皮肤’,
dataIndex: ‘title’,
key: ‘title’,
},
{
title: ‘Tags’,
key: ‘skin_name’,
dataIndex: ‘skin_name’,
render: (tags:string) => (
<>
{tags&&tags.split(‘|’).map(tag => {
let color = tag.length > 3 ? ‘geekblue’ : ‘green’;
return (
{tag}
);
})}
</>
),
},
];
return (
<>
mall--王者荣耀英雄
{/*
{herolist && herolist.map((item: any, index: number) => {
return (
- {item.cname}{'->'}{item.title}
)
})}
*/}</>
);
}
export default connect(({ heros }: any) => ({ heros }))(HeroMall)
页面内数据管理
在页面文件夹下面使用_mock.ts、model.ts、services.ts就可以了 建立方法和全局一样,路径要自己配置
import React, { useEffect } from ‘react’; // import { getHeroList } from ‘…/…/services/heros’
import { connect, Dispatch } from ‘umi’;
import { Table, Tag } from ‘antd’;
import HeroTableSearch from ‘./HeroTableSearch’;
const HeroMall = (props: any) => {
console.log(props);
useEffect(() => {
let { dispatch } = props;
dispatch({
type: ‘mall/fetchHeroList’,
}); //触发model中的effect,带上命名空间
}, []); // fetch(‘/api/herolist’).then(res=>{
// //console.log(res)
// return res.json()
// }).then(res=>{
// console.log(res)
// })
// fetch(‘/api/heros’).then(res=>{
// //console.log(res)
// return res.json()
// }).then(res=>{
// console.log(res)
// })
// getHeroList().then((res)=>{
// console.log(res)
// })
let { mall } = props;
let herolist = mall && mall.herolist && mall.herolist.herolist;
console.log(herolist); // ename: 105
// cname: “廉颇”
// title: “正义爆轰”
// new_type: 0
// hero_type: 3
// skin_name: “正义爆轰|地狱岩魂”
const columns = [
{
title: ‘Id’,
dataIndex: ‘ename’,
key: ‘ename’,
render: text => {text},
},
{
title: ‘英雄’,
dataIndex: ‘cname’,
key: ‘cname’,
},
{
title: ‘皮肤’,
dataIndex: ‘title’,
key: ‘title’,
},
{
title: ‘Tags’,
key: ‘skin_name’,
dataIndex: ‘skin_name’,
render: (tags: string) => (
<>
{tags &&
tags.split(‘|’).map(tag => {
let color = tag.length > 3 ? ‘geekblue’ : ‘green’;
return (
{tag}
);
})}
</>
),
},
];
return (
<>
mall--王者荣耀英雄
{/*
{herolist && herolist.map((item: any, index: number) => {
return (
- {item.cname}{'->'}{item.title}
)
})}
*/}</>
);
};
export default connect(({ mall }: any) => ({
mall,
}))(HeroMall);
顺便修改connect
加个搜索功能
用umi的控制加个高级搜索
表单组件代码修改
import styles from “./index.less”;
import React, { useState } from “react”;
import { Form, Row, Col, Input, Button } from “antd”;
import { DownOutlined, UpOutlined } from “@ant-design/icons”;
const AdvancedSearchForm = () => {
const [form] = Form.useForm();
const getFields = () => {
const children = [];
children.push(
(
<Form.Item
name=“hero”
label=‘英雄’
rules={[{
required:true,
message:‘Input something!’
}
]}
</Form.Item>
)
)
children.push(
(
<Form.Item
name=“skin”
label=‘皮肤’
rules={[{
required:true,
message:‘Input something!’
}
]}
</Form.Item>
)
)
return children;
};
const onFinish = (values:any) => {
console.log("Received values of form: ", values);
};
return (
form={form}
name=“advanced_search”
className=“ant-advanced-search-form”
onFinish={onFinish}
{getFields()}
Search
<Button
style={{ marginLeft: 8 }}
onClick={() => {
form.resetFields();
}}
Clear
);
};
export default () => (
{/*
Search Result List*/});
把搜索功能完善
组件
import styles from “./index.less”;
import React, { useState } from “react”;
import { Form, Row, Col, Input, Button } from “antd”;
import { connect, Dispatch } from ‘umi’;
// import {getHeroListBySearch} from ‘…/service’
const AdvancedSearchForm = (props:any) => {
const [form] = Form.useForm();
const getFields = () => {
const children = [];
children.push(
(
<Form.Item
name=“hero”
label=‘英雄’
// rules={[{
// required:true,
// message:‘Input something!’
// }
//]}
</Form.Item>
)
)
children.push(
(
<Form.Item
name=“skin”
label=‘皮肤’
// rules={[{
// required:true,
// message:‘Input something!’
// }
// ]}
</Form.Item>
)
)
return children;
};
const onFinish = async(values:any) => {
console.log("Received values of form: ", values);
props.dispatch({
type: ‘mall/getHeroListBySearch’,
payload:values,
})
};
return (
form={form}
name=“advanced_search”
className=“ant-advanced-search-form”
onFinish={onFinish}
{getFields()}
Search
<Button
style={{ marginLeft: 8 }}
onClick={() => {
form.resetFields();
}}
Clear
);
};
export default connect((mall:any)=>({mall}))((props:any) =>(
{/*
Search Result List*/} ));
模型
import {Effect,Reducer} from ‘umi’
import{getHeroList,getHeroListBySearch}from ‘./service’
const HerosModel:any={
//命名空间
namespace:‘mall’,
//状态
state:{
herolist:[],
},
effects:{
*fetchHeroList(_,{call,put}:any){
const res= yield call(getHeroList)
//console.log(res)
yield put({
type:‘saveHerolist’,
payload:res
})
},
*getHeroListBySearch({payload}:any,{call,put}:any){
const res= yield call(getHeroListBySearch,payload)
//console.log(res)
yield put({
type:‘saveHerolist’,
payload:res
})
}
,
},
reducers:{
saveHerolist(state:any,action:any){
return{
…state,
herolist:action.payload
}
}
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)
最后
本人分享一下这次字节跳动、美团、头条等大厂的面试真题涉及到的知识点,以及我个人的学习方法、学习路线等,当然也整理了一些学习文档资料出来是附赠给大家的。知识点涉及比较全面,包括但不限于前端基础,HTML,CSS,JavaScript,Vue,ES6,HTTP,浏览器,算法等等
详细大厂面试题答案、学习笔记、学习视频等资料领取,点击资料领取直通车免费领取!
前端视频资料:
list’,payload:res
})
},
*getHeroListBySearch({payload}:any,{call,put}:any){
const res= yield call(getHeroListBySearch,payload)
//console.log(res)
yield put({
type:‘saveHerolist’,
payload:res
})
}
,
},
reducers:{
saveHerolist(state:any,action:any){
return{
…state,
herolist:action.payload
}
}
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-Z3SIHC44-1713470188175)]
[外链图片转存中…(img-0cm18YTf-1713470188175)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
[外链图片转存中…(img-5r48EOpt-1713470188175)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)
[外链图片转存中…(img-um4pPzcn-1713470188176)]
最后
本人分享一下这次字节跳动、美团、头条等大厂的面试真题涉及到的知识点,以及我个人的学习方法、学习路线等,当然也整理了一些学习文档资料出来是附赠给大家的。知识点涉及比较全面,包括但不限于前端基础,HTML,CSS,JavaScript,Vue,ES6,HTTP,浏览器,算法等等
详细大厂面试题答案、学习笔记、学习视频等资料领取,点击资料领取直通车免费领取!
[外链图片转存中…(img-FfweDL2m-1713470188176)]
前端视频资料: