当前位置:首页 > VUE

vue实现天猫导航

2026-01-22 13:06:48VUE

Vue 实现天猫导航

项目结构

使用 Vue CLI 创建项目,安装必要依赖如 vue-routeraxios。目录结构建议如下:

src/
├── components/
│   ├── Header.vue          # 顶部导航
│   ├── MainNav.vue         # 主分类导航
│   ├── SubNav.vue          # 二级分类导航
│   └── SearchBar.vue       # 搜索框组件
├── views/
│   ├── Home.vue            # 首页
│   └── Category.vue        # 分类页
├── router/
│   └── index.js            # 路由配置
└── App.vue                 # 根组件

顶部导航实现

顶部导航包含用户登录、店铺入口等信息,使用 Flex 布局实现:

<template>
  <div class="top-header">
    <div class="left">
      <span>天猫首页</span>
      <span>喵,请登录</span>
      <span>免费注册</span>
    </div>
    <div class="right">
      <span>我的订单</span>
      <span>购物车</span>
    </div>
  </div>
</template>

<style scoped>
.top-header {
  display: flex;
  justify-content: space-between;
  background: #f2f2f2;
  padding: 5px 20px;
}
</style>

主分类导航

主分类采用横向滚动式设计,数据通过 v-for 动态渲染:

vue实现天猫导航

<template>
  <div class="main-nav">
    <div v-for="item in navList" :key="item.id" class="nav-item">
      <img :src="item.icon" />
      <span>{{ item.name }}</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      navList: [
        { id: 1, name: '女装', icon: 'icon-path' },
        { id: 2, name: '男装', icon: 'icon-path' }
      ]
    }
  }
}
</script>

<style scoped>
.main-nav {
  display: flex;
  overflow-x: auto;
  white-space: nowrap;
}
.nav-item {
  padding: 0 15px;
  text-align: center;
}
</style>

搜索框组件

集成搜索建议功能,使用 v-model 绑定输入值:

<template>
  <div class="search-box">
    <input 
      v-model="keyword" 
      @keyup.enter="search" 
      placeholder="搜索商品" 
    />
    <button @click="search">搜索</button>
    <ul v-if="suggestions.length" class="suggestions">
      <li v-for="(item, index) in suggestions" :key="index">
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      keyword: '',
      suggestions: []
    }
  },
  methods: {
    async search() {
      if (this.keyword.trim()) {
        const res = await axios.get('/api/search?s=' + this.keyword)
        this.suggestions = res.data.suggestions
      }
    }
  }
}
</script>

路由配置

配置分类页面的动态路由:

vue实现天猫导航

// router/index.js
{
  path: '/category/:id',
  name: 'Category',
  component: () => import('../views/Category.vue')
}

交互优化

  1. 主分类导航添加鼠标悬停效果:

    .nav-item:hover {
    color: #FF0036;
    transform: translateY(-2px);
    }
  2. 搜索框防抖处理:

    import _ from 'lodash'
    export default {
    watch: {
     keyword: _.debounce(function(newVal) {
       this.fetchSuggestions(newVal)
     }, 300)
    }
    }

数据获取

通过 Axios 获取导航数据:

async created() {
  try {
    const res = await axios.get('/api/nav')
    this.navList = res.data
  } catch (error) {
    console.error(error)
  }
}

注意事项

  1. 移动端需添加 touch 事件支持
  2. 图标建议使用 SVG 雪碧图或 iconfont
  3. 二级分类建议使用 CSS 过渡动画
  4. 路由切换时保持导航状态可用 Vuex 管理

标签: vue
分享给朋友:

相关文章

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue实现继承语法糖

vue实现继承语法糖

在Vue中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const BaseC…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogres…

vue实现drag

vue实现drag

Vue 实现拖拽功能的方法 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来完成。以下是两种常见方法的实现方式。 使用 HTML5 拖拽…

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue 实现闪烁

vue 实现闪烁

Vue 实现闪烁效果的方法 方法一:使用 CSS 动画 在 Vue 组件中定义一个闪烁的 CSS 动画,通过绑定 class 或 style 来控制元素的闪烁效果。 <template&…