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

news/2024/7/15 18:24:51 标签: react.js, vue.js, 前端

 1.安装组件库

npm install --save antd-mobile

常用组件
tabbar 底部导航

Swiper 轮播图(走马灯)

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

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

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

SideBar 侧边导航

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

eg:

import { Button } from 'antd-mobile';

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

//下载
npm install --save antd-mobile-icons

//导入使用
import { AntOutline } from 'antd-mobile-icons'

常用组件:

封装: tabbar

import React, { Component } from 'react';
import './MyTabbar.scss'
import { Badge, TabBar } from 'antd-mobile'
import { AppOutline, AppstoreOutline, MessageFill, MessageOutline, UserOutline } from 'antd-mobile-icons'
import { withRouter } from 'react-router-dom';
interface Props {
    history:any,
    match:any,
    location:any,
}
interface State {
    tabs: Array<any>
}

class MyTabbar extends Component<Props, State> {
    constructor(props: Props) {
        super(props)
        this.state = {
            tabs: [
                {
                    key: '/index/home',
                    title: '首页',
                    icon: <AppOutline />,
                },
                {
                    key: '/index/mypage',
                    title: '分类',
                    icon: <AppstoreOutline />,
                },
                {
                    key: '/index/mycate',
                    title: '购物车',
                    icon: (active: boolean) =>
                        active ? <MessageFill /> : <MessageOutline />,
                },
                {
                    key: '/index/my',
                    title: '我的',
                    icon: <UserOutline />,
                },
            ]
        }
    }
    handleChange(key: string) {
        this.props.history.push(key)
    }
    render() {
        return (
            <TabBar className='tabbar' onChange={(key) => { this.handleChange(key) }}>
                {
                    this.state.tabs.map(item => (
                        <TabBar.Item key={item.key} icon={item.icon} title={item.title} />
                    ))
                }
            </TabBar>
        );
    }
}

export default withRouter(MyTabbar);
.tabbar{
    position: fixed;
    bottom: 0;
    .adm-tab-bar-wrap{
        width: 100%;
    }
}

 封装:swiper

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

 接收使用

import React, { Component } from 'react';
import './index.scss'
import { Swiper } from 'antd-mobile'

interface Props {
    swiperlist: Array<any>
}
class MySwiper extends Component<Props> {
    render() {

        return (
            < Swiper className='myswiper' allowTouchMove={true} autoplay autoplayInterval={1000} loop >
                {
                    this.props.swiperlist.map((item, index) => {
                        return (
                            <Swiper.Item key={index}>
                                <img src={item.img} alt="" key={index} />
                            </Swiper.Item>
                        )
                    })
                }
            </Swiper>
        )
    }
}

export default MySwiper;
.myswiper{
    height: 200px;
    img{
        //height: 200px;
        width: 100%;
    }
}

navbar顶部返回:配合 Dialog,Toast

import React, { Component } from 'react';
import './index.scss'
import { NavBar,Dialog, Toast } from 'antd-mobile'
interface Props {
    match: any,
    location: any
}
class Detail extends Component<Props> {
    back = () =>{
        // Toast.clear();
        // Toast.show({
        //     content: '点击了返回',
        //     duration: 1000,
        // })
        Dialog.clear()    //关闭所有打开的对话框
        Dialog.confirm({
            content: '确定删除吗?',
            title:'警告',
        }).then((res)=>{
            if (res) {
                console.log('点击了确定,执行删除');
            } else {
                console.log('点击了取消');
            }
        })
    }
    render() {
        // 获取动态路由参数
        // console.log(this.props.match.params.id);
        // 获取固定路由参数,query,刷新页面参数会丢失
        // console.log(this.props.location.query.id);
        // 获取固定路由参数,state
        console.log(this.props.location.state.proid); //商品信息获取了
        return (
            <div className='detail'>
                <NavBar back='返回' onBack={() => { this.back()}}>
                    商品详情
                </NavBar>
            </div>
        );
    }
}
export default Detail;

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

//下拉加载
import { InfiniteScroll } from 'antd-mobile'
//骨架屏
import { Skeleton } from 'antd-mobile'



{   // 骨架屏
    this.state.recommendlist.length == 0 &&
        <>
            <Skeleton.Title animated />
            <Skeleton.Paragraph lineCount={5} animated />
        </>
}



{/* 下拉加载:hasMore 是否有更多内容,当hasMore为true时,列表滚动到底部会自动执行loadMore对应的回调函数 */}
<InfiniteScroll loadMore={this.loadMore} hasMore={this.state.hasMore} />

loadMore是个异步函数:::

 代码如下:::

import React, { Component } from 'react';
import './index.scss'
import { pro_recommendlist } from '@/api/index'
import { withRouter } from 'react-router-dom';

// 导入 无限加载 和 骨架屏组件
import { Skeleton } from 'antd-mobile'
import { InfiniteScroll } from 'antd-mobile'
interface Props {
    history: any //表示可选的值
    match: any,
    location: any
}
interface State {
    recommendlist: Array<any>
    hasMore: any,
    count: any,
}
class List extends Component<Props, State> {
    constructor(Props: Props) {
        super(Props)
        this.state = {
            // list: []
            recommendlist: [],
            count: 0,
            hasMore: true,
        }
    }
    // async componentDidMount() {
    //     var res = await pro_recommendlist({ count: 1, limitNum: 10 })
    //     // console.log(res.data.data);
    //     if (res.data.code == 200) {
    //         this.setState({ recommendlist: res.data.data })
    //     }
    // }
    handleClick(e: any, proid: any) {
        this.props.history.push({ pathname: '/detail', state: { proid } })
        // console.log(proid);
    }
    // 滑动加载
    loadMore = async () => {
        // 请求下一条数据
        var res = await pro_recommendlist({ count: this.state.count + 1 })

        if (res.data.data.length < 12) {
            // console.log(res);
            this.setState({
                hasMore: false,//数据加载完成,设置false})
                recommendlist: [...this.state.recommendlist, ...res.data.data],
                count: this.state.count + 1
            })
        } else {
            this.setState({
                recommendlist: [...this.state.recommendlist, ...res.data.data],
                count: this.state.count + 1
            })
        }
    }
    render() {
        return (
            <ul className="list_ul">
                {
                    this.state.recommendlist.map((item, index) => {
                        return (
                            <li key={index} onClick={(e) => { this.handleClick(e, item.proid) }}>
                                <img src={item.img1} alt="" />
                                <p className='clear_ellipsis'>{item.proname}</p>
                                <p className='money'>¥<span >{item.originprice}</span></p>
                            </li>
                        )
                    })
                }
                {   // 骨架屏
                    this.state.recommendlist.length == 0 &&
                        <>
                            <Skeleton.Title animated />
                            <Skeleton.Paragraph lineCount={5} animated />
                        </>
                }
                {/* hasMore 是否有更多内容,当hasMore为true时,列表滚动到底部会自动执行loadMore对应的回调函数 */}

                <InfiniteScroll loadMore={this.loadMore} hasMore={this.state.hasMore} />
            </ul>
        );
    }
}

export default withRouter(List);

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

//骨架屏
import { Skeleton } from 'antd-mobile'


{
    this.state.recommendlist.length == 0 &&
    <>
        <Skeleton.Title animated />
        <Skeleton.Paragraph lineCount={5} animated />
    </>
}
                


http://www.niftyadmin.cn/n/247800.html

相关文章

环境变量的初始

目录 &#xff1a; 1.引出环境变量 2. 简单使用一下环境变量 3.环境变量的概念 4.本地变量 5.环境变量的相关命令 6.环境变量获取和操作的方式 7. getenv&#xff08;最常用的程序获取环境变量的方式&#xff09; 8.环境变量的全局属性 -------------------------------------…

《选择》比努力更重要——C语言

目录 前言: 1.语句 2.选择语句 2.1小栗子 2.2选择结构 3.误导性else 3.1写法上的可读性和代码的稳健性&#xff1a; 3.2一些练习 4.switch选择语句 4.1嵌套的switch ❤博主CSDN:啊苏要学习 ▶专栏分类&#xff1a;C语言◀ C语言的学习&#xff0c;是为我们今后学习其…

如何获取苹果设备的UDID(iPhone/iPad UDID查询方法)

方法一、通过电脑连接苹果手机后查询 1、在电脑上下载并安装爱思助手&#xff0c;安装完成后将电脑和苹果手机使用苹果数据线连接起来&#xff1b; ​ 编辑切换为居中 添加图片注释&#xff0c;不超过 140 字&#xff08;可选&#xff09; 然后启动爱思助手这个软件&#xf…

硬“核”数字员工:中国核能行业协会携手实在智能,成功举办核能行业数字化实战培训

导语&#xff1a; 近期&#xff0c;中国核能行业协会携手实在智能&#xff0c;成功举办核能行业数字化实战培训&#xff0c;通过理论学习和参与实践&#xff0c;学员们亲手打造出一个个硬“核”数字员工。 十多张表格快速切换&#xff0c;无数串数字在面前跳动&#xff0c;上岗…

公司流程 企业所得税 个人所得税

公司注册流程&#xff1a; 1.成立执照申请公司名称都在工商网上进行 各位需要把公司的名称、经营项目范围、出资者信息、登记地址资料、投资情况等在网站上提交&#xff0c;等待审核通过。 2.工商网站上申报材料核准后&#xff0c;就能到现场获取执照 一般公司一定要刻公司印章…

N9344C安捷伦频谱分析仪

附加功能: 频率范围:1 MHz至20 GHz最高20 GHz的内部前置放大器内置GPS接收器和GPS天线内置跟踪发生器-144 dBm典型显示平均噪声水平(DANL)1.3 dB幅度精度1 GHz时11 dBm典型三阶交调截点(TOI)< 0.95 s sweep time for 20 GHz full span通道扫描仪可同时测量多达20个通道具有…

vue3.0 新增和更改的一些新的api和功能

前言 vue3.0新增更改了很多api,我在使用过程中慢慢总结下来&#xff0c;记录一下&#xff0c;方便自己学习。 这篇博文会不断更新的 一&#xff0c;支持在<style>标签中使用v-bind动态获取组件中的值 之前再vue2.0中想要动态操作某个dom的样式&#xff0c;就需要用到ref…

指纹浏览器科普:管理上百个社媒账号?窗口同步?RPA自动化?

做跨境电商的朋友想必都对指纹浏览器不陌生&#xff0c;&#xff0c;但现在指纹浏览器品牌众多&#xff0c;就会让很多朋友犯了选择困难症。市面上知名度较高的指纹浏览器&#xff0c;像AdsPower、multilogin、dolphin等等。因此&#xff0c;东哥今天就出一期指纹浏览器测评&am…