首页 > 其他分享 >vue3项目-小兔鲜儿笔记-02-首页模块01

vue3项目-小兔鲜儿笔记-02-首页模块01

时间:2022-08-28 01:33:41浏览次数:54  
标签:02 category 01 const name 鲜儿 分类 import id

1.less自动化导入

安装一个vue-cli插件,自动导入less文件

vue add style-resources-loader

2. 头部分类导航组件渲染

  • 实现头部一级分类和二级分类的渲染

基本步骤:

  1. 定义一个常量数据和后台保持一致,这样不请求后台就能展示一级分类,不至于白屏

  2. 定义接口函数

  3. 在pinia的category模块,基于常量定义list,定义修改分类列表函数,定义获取数据函数

  4. 在Layout组件调用actions获取数据,在头部导航组件渲染

// 一级分类
export const topCategory = [
  '居家',
  '美食',
  '服饰',
  '母婴',
  '个护',
  '严选',
  '数码',
  '运动',
  '杂货'
]
import { defineStore } from 'pinia'
import { topCategory } from '@/api/constants'
import { findAllCategories } from '@/api/category'

const useCategoryStore = defineStore('category', {
  state: () => {
    return {
      // 保证初始化时就有分类数据,不至于白屏等待请求
      list: topCategory.map((category, index) => {
        return { id: `${index}`, name: category }
      })
    }
  },
  actions: {
    setList(payload) {
      this.list = payload
    },
    async getList() {
      const { result } = await findAllCategories()
      // 给每个数据加上控制二级分类显示隐藏的数据
      result.forEach((category) => {
        category.open = false
      })
      this.setList(result)
      // 添加开启关闭标志,分类当鼠标移入时开启,当鼠标点击后关闭
    },
    // 修改当前一级分类下的二级分类显示窗口open为true
    show(item) {
      const category = this.list.find((category) => category.id === item.id)
      category.open = true
    },
    // 修改当前一级分类下的二级分类显示窗口open为false
    hide(item) {
      const category = this.list.find((category) => category.id === item.id)
      category.open = false
    }
  }
})

 

3. 头部分类导航交互

  • 由于是单页面路由跳转不会刷新页面,由css控制的hover并不能在路由跳转的时候关闭二级分类弹窗

实现逻辑:

  1. 配置路由支持分类跳转

  2. 鼠标进入一级分类就展示对应的二级分类弹窗

  3. 点击一级分类或者二级分类,或者离开一级分类和二级分类,都要隐藏二级分类弹窗

const routes = [
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/views/Layout.vue'),
    children: [
      {
        path: '/',
        name: 'Home',
        component: () => import('@/views/home/index.vue')
      },
      {
        path: '/category/:id',
        name: 'TopCategory',
        component: () => import('@/views/category/index.vue')
      },
      {
        path: '/category/sub/:id',
        name: 'SubCategory',
        component: () => import('@/views/category/sub.vue')
      }
    ]
  }
]
    <template v-for="category in list" :key="category.id">
      <!-- 控制layer层的显示和隐藏 -->
      <li
        @mouseenter="show(category)"
        @mouseleave="hide(category)"
        @click="hide(category)"
      >
        <router-link :to="`/category/${category.id}`">
          {{ category.name }}
        </router-link>
        <div class="layer" :class="{ active: category.open === true }">
          <ul>
            <template
              v-for="subCategory in category.children"
              :key="subCategory.id"
            >
              <li>
                <router-link :to="`/category/sub/${subCategory.id}`">
                  <img :src="subCategory.picture" alt="" />
                  <p>{{ subCategory.name }}</p>
                </router-link>
              </li>
            </template>
          </ul>
        </div>
      </li>
    </template>

    // css 控制二级分类显示隐藏的时候,跳转页面不会自动隐藏窗口
    // 需要通过数据来进行控制
    const show = (category) => {
      categoryStore.show(category)
    }
    const hide = (category) => {
      categoryStore.hide(category)
    }
    
      // > .layer {
      //   height: 132px;
      //   opacity: 1;
      // }
    
    .active {
      height: 132px;
      opacity: 1;
    }

 

4. 吸顶头部-使用dom实现

在页面滚动到80px以上时,显示吸顶头部组件

<div class="app-header-sticky" :class="{ show: y >= 80 }">
    <!--  当卷去距离大于80时才显示container,否则会导致两个头部都显示,这是为了吸顶头部内容不遮住不吸顶的头部  -->
    <div class="container" v-show="y >= 80">
      <!--   logo   -->
      <h1 class="logo">
        <router-link to="/">小兔鲜儿</router-link>
      </h1>
      <!--   导航   -->
      <app-header-nav />
      <!--   其他   -->
      <ul class="other">
        <li>
          <a href="#">品牌</a>
        </li>
        <li>
          <a href="#">专题</a>
        </li>
      </ul>
    </div>

    // 监听滚动距离:document.documentElement.scrollTop
    // 使用dom的写法
    const y = ref(0)
    onMounted(() => {
      document.addEventListener('scroll', function (e) {
        y.value = e.target.documentElement.scrollTop
      })
    })

 

5. 吸顶头部组件-使用vueuse/core完成

安装@vueuse/core

npm install @vueuse/core

使用@vueuse/core的useWindowScroll函数

 

// 使用vueuse/core
const { y } = useWindowScroll()

标签:02,category,01,const,name,鲜儿,分类,import,id
From: https://www.cnblogs.com/jzhFlash/p/16631855.html

相关文章

  • 《GB28007-2011》PDF下载
    《GB28007-2011儿童家具通用技术条件》PDF下载《GB28007-2011》简介本标准规定了儿童家具的术语和定义、一般要求、安全要求、警示标识、试验方法、检验规则及标志、使......
  • ansible 001 ansible介绍 原理
    ansible自动化运维ansible部署应用程序(在操作系统层面之上)系统初始化过程主机名,yun源,网络,服务,时间同步,内核参数(可以在pxe这里完成)ansible可以方便100多台......
  • 《GB28008-2011》PDF下载
    《GB28008-2011玻璃家具安全技术要求》PDF下载《GB28008-2011》简介本标准规定了玻璃家具的术语和定义、分类、要求、试验方法、检验规则、标志、使用说明、包装、运输......
  • NOI2022 游记
    NOI2022游记目录NOI2022游记08120820082108220823082408250826感觉还是应该写点游记什么的东西,不然可能到明年我高二退役的时候发现信竞相关的回忆都忘光光了。开始写......
  • 使用小乌龟来更新代码-02
    小乌龟更新代码使用的是pull右击项目文件,TortoiseGit--->pull来更新代码,从远程仓库拉取最新的代码,拉取后。  点击OK 然后点击PulledDiff,点击Showlog看看当前版......
  • 长城杯2022 known_phi
    InvolvedKnowledge已知phi,n分解nDSAK共享攻击DescriptionfromCrypto.Util.numberimportgetPrime,bytes_to_long,inverse,long_to_bytesfromCrypto.P......
  • 1012 The Best Rank (25分)
    ToevaluatetheperformanceofourfirstyearCSmajoredstudents,weconsidertheirgradesofthreecoursesonly: C -CProgrammingLanguage, M -Mathemat......
  • re | [GXYCTF2019]minecraft
    [GXYCTF2019]minecraft闲得无聊,找个题练练手。提示是使用了boost库的hash,给了exe和dll,都是64位的,dll导出了一个函数,就是加密和对比用的。基本格局如下:主函数:dll:d......
  • 2022-08-27 第四小组 王星苹 学习心得
    学习心得今天主要学习了在html里面用Vue库,也是一个js文件,这个也是相当于写好的东西可以直接用。Vue.js的核心是一个采用简洁的模板语法来声明式地将数据渲染进DOM的系统。......
  • NOI2022 VP寄
    Day-?由于我特别菜,去年NOIP寄成了158,今年省选遇上疫情,分数线提到了210,所以省选寄了,NOI2022D类梦也寄了。8月26日晚上拿到了两天的pdf和day1的数据,准备VP......