uni-app x导航区域跳转

一、概述

上一篇文章,实现了商品数据展示,接下要做的是,导航区域几个icon的跳转。

点击不同的图标,跳转到不同的页面,使用uni.navigateTo实现。

效果如下:

二、实现跳转

需要在pages目录,创建好跳转的页面,比如:goods,contact,pics,videos,结构如下:

复制代码
./
├── cart
│   └── cart.uvue
├── contact
│   └── contact.uvue
├── goods
│   └── goods.uvue
├── index
│   └── index.uvue
├── login
│   └── login.uvue
├── member
│   └── member.uvue
├── news
│   └── news.uvue
├── pics
│   └── pics.uvue
└── videos
    └── videos.uvue

新创建好的uvue文件,写一个标题文字即可,例如:goods.uvue

复制代码
<template>
    <view>
        商品中心
    </view>
</template>

<script>
    export default {
        data() {
            return {

            }
        },
        methods: {

        }
    }
</script>

<style>

</style>

接下来,需要对导航区域4个图标做跳转。可以为每一个图标,绑定点击事件

复制代码
<view class="nav_item">
    <view class="iconfont icon-ziyuan" @click="nav_item_click('/pages/goods/goods')"></view>
    <text>网上超市</text>
</view>

跳转到指定的url页面

复制代码
// 导航点击的处理函数
nav_item_click(path) {
    console.log("path", path)
    uni.navigateTo({
        url: path
    })
}

但是这么做的话,需要写很多代码,比较麻烦。有没有更简单的一点的呢?答案是有的。

就是将需要跳转的图标,写到一个列表里面,然后for循环。 每一个图标,绑定点击事件。

index.uvue完整代码如下:

复制代码
<template>
    <view class="home">
        <!-- 轮播区域 -->
        <up-swiper :list="swiper" keyName="img" indicator indicatorMode="dot" circular></up-swiper>
        <!-- 导航区域 -->
        <view class="nav">
            <view class="nav_item" v-for="(item,index) in navs" :key="index" @click="nav_item_click(item.path)">
                <view :class="item.icon"></view>
                <text>{{item.title}}</text>
            </view>
        </view>
        <!-- 推荐商品 -->
        <view class="hot_goods">
            <view class="tit">推荐商品</view>
            <view class="goods_list">
                <view class="goods_item" v-for="item in goods" :key="item.id">
                    <image :src="item.img_url" mode=""></image>
                    <view class="price">
                        <text>¥{{item.sell_price}}</text>
                        <text>¥{{item.market_price}}</text>
                    </view>
                    <view class="name">
                        {{item.title}}
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: 'Hello',
                swiper: [],
                goods: [],
                navs: [
                    {
                        icon: 'iconfont icon-ziyuan',
                        title: '网上超市',
                        path: '/pages/goods/goods'
                    },
                    {
                        icon: 'iconfont icon-guanyuwomen',
                        title: '联系我们',
                        path: '/pages/contact/contact'
                    },
                    {
                        icon: 'iconfont icon-tupian',
                        title: '社区图片',
                        path: '/pages/pics/pics'
                    },
                    {
                        icon: 'iconfont icon-shipin',
                        title: '直播中心',
                        path: '/pages/videos/videos'
                    }
                ]
            }
        },
        onLoad() {
            this.get_swiper()
            this.get_goods()
        },
        methods: {
            // 获取轮播图的数据
            async get_swiper() {
                try {
                    const res = await this.$http.get('/api/getlunbo', {})
                    // console.log("res", res)
                    this.swiper = res.message
                } catch (err : any) {
                    // console.error('获取轮播图失败', err)
                    uni.showToast({
                        title: '获取轮播图失败' + err.statusCode,
                    });
                }
            },
            // 获取热门商品列表数据
            async get_goods() {
                try {
                    const res = await this.$http.get('/api/getgoods?pageindex=1', {})
                    // console.log("res", res)
                    this.goods = res.message
                } catch (err : any) {
                    // console.error('获取轮播图失败', err)
                    uni.showToast({
                        title: '获取热门商品列表失败' + err.statusCode,
                    });
                }
            },
            // 导航点击的处理函数
            nav_item_click(path) {
                console.log("path", path)
                uni.navigateTo({
                    url: path
                })
            }
        }
    }
</script>

<style lang="scss">
    .home {
        swiper {
            width: 750rpx;
            height: 380rpx;

            image: {
                height: 100%;
                width: 100%;
            }
        }

        .nav {
            display: flex;
            flex-direction: row; //横向排列
            justify-content: space-around; //平均分布在一行

            .nav_item {
                text-align: center;

                view {
                    width: 120rpx;
                    height: 120rpx;
                    background: $shop-color;
                    border-radius: 60rpx;
                    margin: 10px auto;
                    line-height: 120rpx;
                    color: white;
                    font-size: 50rpx;
                    text-align: center;
                }

                .icon-tupian {
                    font-size: 45rpx;
                }

                text {
                    font-size: 30rpx;
                }
            }
        }

        .hot_goods {
            background: #eee;
            overflow: hidden;
            margin-top: 10px;

            .tit {
                height: 50px;
                line-height: 50px;
                color: $shop-color;
                text-align: center;
                letter-spacing: 20px;
                background: #fff;
                margin: 7rpx 0;
            }

            .goods_list {
                padding: 0 15rpx;
                display: flex;
                flex-direction: row; //横向排列
                flex-wrap: wrap;
                justify-content: space-between;

                .goods_item {
                    background: #fff;
                    width: 355rpx;
                    margin: 10rpx 0;
                    padding: 15rpx;
                    box-sizing: border-box;

                    image {
                        width: 80%;
                        height: 150px;
                        display: block;
                        margin: auto; //图片居中
                    }

                    .price {
                        display: flex;
                        flex-direction: row;
                        color: $shop-color;
                        font-size: 36rpx;
                        // margin-top: 15rpx;
                        margin: 20rpx 0 5rpx 0;

                        text:nth-child(2) {
                            color: #ccc;
                            font-size: 28rpx;
                            margin-top: 5rpx;
                            margin-left: 17rpx;
                            text-decoration-line: line-through;
                        }
                    }

                    .name {
                        font-size: 38rpx;
                        line-height: 50rpx;
                        padding-bottom: 15rpx;
                        padding-top: 10rpx;
                    }
                }
            }
        }
    }
</style>

重新编译运行,就是文章开头的效果了。

相关推荐
z落落10 小时前
C#WinForm 窗体切换与窗体传值(登录跳转案例)+WinForm 窗体传值(从上往下传、从下往上传)
开发语言·windows·c#
Dontla10 小时前
git bash打开Claude code报错:Claude Code on Windows requires git-bash.(别把git装其他位置,严格按照默认安装)找不到claude code
windows·git·bash
comcoo11 小时前
电脑自动干活不用值守!OpenClaw 本地部署完整实操流程
windows·开源·github·open claw部署·open claw部署包
蓝鸟197412 小时前
Windows Route 内外网双通原理+实战详解(彻底解决双网卡互斥断网)
windows·网络运维·电脑网络配置·路由冲突解决·route命令·内外网同时上网·windows路由 双网卡双通
2601_9618454213 小时前
法考真题及答案解析|历年真题|资料已整理
linux·windows·ubuntu·macos·centos·gnu
coolwaterld15 小时前
windows下删文件,找出“到底是谁占用
windows
caimouse16 小时前
Reactos 第6章 进程间通信(续)
windows
触底反弹17 小时前
拷个 .exe 到新电脑就跑不起来?你缺的不是文件,是对链接的理解
c++·windows·操作系统
W优化大师17 小时前
Windows 更新待处理弹窗一直不消失怎么解决,C 盘空间和后台任务该如何排查
windows·系统优化·磁盘清理·windows11·c盘·系统更新
Geek_Vison17 小时前
2026 跨端框架横评:FinClip、Taro、uni-app、Remax、mPaaS 五款工具技术+业务双维度测评
小程序·uni-app·taro·mpaas·小程序容器