当前位置:   article > 正文

react-7 组件库 Ant Design Mobile(移动端)_antd-mobile-icons

antd-mobile-icons

 1.安装组件库

npm install --save antd-mobile

常用组件
tabbar 底部导航

Swiper 轮播图(走马灯)

NavBar(顶部返回累) 配合 Dialog,Toast

InfiniteScroll 无限滚动(实现下拉刷新)

Skeleton 骨架屏:用图形表示内容占位

SideBar 侧边导航

2. 按需导入
我们可以根据项目需要导入需要用到的组件, 该组件的样式会自动导入.

eg:

import { Button } from 'antd-mobile';

下载组件库,icon:特殊一点,导入使用

  1. //下载
  2. npm install --save antd-mobile-icons
  3. //导入使用
  4. import { AntOutline } from 'antd-mobile-icons'

常用组件:

封装: tabbar

  1. import React, { Component } from 'react';
  2. import './MyTabbar.scss'
  3. import { Badge, TabBar } from 'antd-mobile'
  4. import { AppOutline, AppstoreOutline, MessageFill, MessageOutline, UserOutline } from 'antd-mobile-icons'
  5. import { withRouter } from 'react-router-dom';
  6. interface Props {
  7. history:any,
  8. match:any,
  9. location:any,
  10. }
  11. interface State {
  12. tabs: Array<any>
  13. }
  14. class MyTabbar extends Component<Props, State> {
  15. constructor(props: Props) {
  16. super(props)
  17. this.state = {
  18. tabs: [
  19. {
  20. key: '/index/home',
  21. title: '首页',
  22. icon: <AppOutline />,
  23. },
  24. {
  25. key: '/index/mypage',
  26. title: '分类',
  27. icon: <AppstoreOutline />,
  28. },
  29. {
  30. key: '/index/mycate',
  31. title: '购物车',
  32. icon: (active: boolean) =>
  33. active ? <MessageFill /> : <MessageOutline />,
  34. },
  35. {
  36. key: '/index/my',
  37. title: '我的',
  38. icon: <UserOutline />,
  39. },
  40. ]
  41. }
  42. }
  43. handleChange(key: string) {
  44. this.props.history.push(key)
  45. }
  46. render() {
  47. return (
  48. <TabBar className='tabbar' onChange={(key) => { this.handleChange(key) }}>
  49. {
  50. this.state.tabs.map(item => (
  51. <TabBar.Item key={item.key} icon={item.icon} title={item.title} />
  52. ))
  53. }
  54. </TabBar>
  55. );
  56. }
  57. }
  58. export default withRouter(MyTabbar);
  1. .tabbar{
  2. position: fixed;
  3. bottom: 0;
  4. .adm-tab-bar-wrap{
  5. width: 100%;
  6. }
  7. }

 封装:swiper

拿到数据,通过组件传递给组件内数组。父向子

 接收使用

  1. import React, { Component } from 'react';
  2. import './index.scss'
  3. import { Swiper } from 'antd-mobile'
  4. interface Props {
  5. swiperlist: Array<any>
  6. }
  7. class MySwiper extends Component<Props> {
  8. render() {
  9. return (
  10. < Swiper className='myswiper' allowTouchMove={true} autoplay autoplayInterval={1000} loop >
  11. {
  12. this.props.swiperlist.map((item, index) => {
  13. return (
  14. <Swiper.Item key={index}>
  15. <img src={item.img} alt="" key={index} />
  16. </Swiper.Item>
  17. )
  18. })
  19. }
  20. </Swiper>
  21. )
  22. }
  23. }
  24. export default MySwiper;
  1. .myswiper{
  2. height: 200px;
  3. img{
  4. //height: 200px;
  5. width: 100%;
  6. }
  7. }

navbar顶部返回:配合 Dialog,Toast

  1. import React, { Component } from 'react';
  2. import './index.scss'
  3. import { NavBar,Dialog, Toast } from 'antd-mobile'
  4. interface Props {
  5. match: any,
  6. location: any
  7. }
  8. class Detail extends Component<Props> {
  9. back = () =>{
  10. // Toast.clear();
  11. // Toast.show({
  12. // content: '点击了返回',
  13. // duration: 1000,
  14. // })
  15. Dialog.clear() //关闭所有打开的对话框
  16. Dialog.confirm({
  17. content: '确定删除吗?',
  18. title:'警告',
  19. }).then((res)=>{
  20. if (res) {
  21. console.log('点击了确定,执行删除');
  22. } else {
  23. console.log('点击了取消');
  24. }
  25. })
  26. }
  27. render() {
  28. // 获取动态路由参数
  29. // console.log(this.props.match.params.id);
  30. // 获取固定路由参数,query,刷新页面参数会丢失
  31. // console.log(this.props.location.query.id);
  32. // 获取固定路由参数,state
  33. console.log(this.props.location.state.proid); //商品信息获取了
  34. return (
  35. <div className='detail'>
  36. <NavBar back='返回' onBack={() => { this.back()}}>
  37. 商品详情
  38. </NavBar>
  39. </div>
  40. );
  41. }
  42. }
  43. export default Detail;

下拉加载:无限滚动:需要放在列表下边

  1. //下拉加载
  2. import { InfiniteScroll } from 'antd-mobile'
  3. //骨架屏
  4. import { Skeleton } from 'antd-mobile'
  5. { // 骨架屏
  6. this.state.recommendlist.length == 0 &&
  7. <>
  8. <Skeleton.Title animated />
  9. <Skeleton.Paragraph lineCount={5} animated />
  10. </>
  11. }
  12. {/* 下拉加载:hasMore 是否有更多内容,当hasMore为true时,列表滚动到底部会自动执行loadMore对应的回调函数 */}
  13. <InfiniteScroll loadMore={this.loadMore} hasMore={this.state.hasMore} />

loadMore是个异步函数:::

 代码如下:::

  1. import React, { Component } from 'react';
  2. import './index.scss'
  3. import { pro_recommendlist } from '@/api/index'
  4. import { withRouter } from 'react-router-dom';
  5. // 导入 无限加载 和 骨架屏组件
  6. import { Skeleton } from 'antd-mobile'
  7. import { InfiniteScroll } from 'antd-mobile'
  8. interface Props {
  9. history: any //表示可选的值
  10. match: any,
  11. location: any
  12. }
  13. interface State {
  14. recommendlist: Array<any>
  15. hasMore: any,
  16. count: any,
  17. }
  18. class List extends Component<Props, State> {
  19. constructor(Props: Props) {
  20. super(Props)
  21. this.state = {
  22. // list: []
  23. recommendlist: [],
  24. count: 0,
  25. hasMore: true,
  26. }
  27. }
  28. // async componentDidMount() {
  29. // var res = await pro_recommendlist({ count: 1, limitNum: 10 })
  30. // // console.log(res.data.data);
  31. // if (res.data.code == 200) {
  32. // this.setState({ recommendlist: res.data.data })
  33. // }
  34. // }
  35. handleClick(e: any, proid: any) {
  36. this.props.history.push({ pathname: '/detail', state: { proid } })
  37. // console.log(proid);
  38. }
  39. // 滑动加载
  40. loadMore = async () => {
  41. // 请求下一条数据
  42. var res = await pro_recommendlist({ count: this.state.count + 1 })
  43. if (res.data.data.length < 12) {
  44. // console.log(res);
  45. this.setState({
  46. hasMore: false,//数据加载完成,设置false})
  47. recommendlist: [...this.state.recommendlist, ...res.data.data],
  48. count: this.state.count + 1
  49. })
  50. } else {
  51. this.setState({
  52. recommendlist: [...this.state.recommendlist, ...res.data.data],
  53. count: this.state.count + 1
  54. })
  55. }
  56. }
  57. render() {
  58. return (
  59. <ul className="list_ul">
  60. {
  61. this.state.recommendlist.map((item, index) => {
  62. return (
  63. <li key={index} onClick={(e) => { this.handleClick(e, item.proid) }}>
  64. <img src={item.img1} alt="" />
  65. <p className='clear_ellipsis'>{item.proname}</p>
  66. <p className='money'><span >{item.originprice}</span></p>
  67. </li>
  68. )
  69. })
  70. }
  71. { // 骨架屏
  72. this.state.recommendlist.length == 0 &&
  73. <>
  74. <Skeleton.Title animated />
  75. <Skeleton.Paragraph lineCount={5} animated />
  76. </>
  77. }
  78. {/* hasMore 是否有更多内容,当hasMore为true时,列表滚动到底部会自动执行loadMore对应的回调函数 */}
  79. <InfiniteScroll loadMore={this.loadMore} hasMore={this.state.hasMore} />
  80. </ul>
  81. );
  82. }
  83. }
  84. export default withRouter(List);

 骨架屏(两种写法)先导入,再使用

  1. //骨架屏
  2. import { Skeleton } from 'antd-mobile'
  3. {
  4. this.state.recommendlist.length == 0 &&
  5. <>
  6. <Skeleton.Title animated />
  7. <Skeleton.Paragraph lineCount={5} animated />
  8. </>
  9. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/93260?site
推荐阅读
相关标签
  

闽ICP备14008679号