实现页面
确认需求
- 顶部提示栏
- 搜索框
- 搜索提示
normalize.css:处理不同浏览器的默认样式
安装
- npm i normalize.css
使用
src\App.vue <style scoped> @import 'normalize.css'; #app { /** 让字体抗锯齿,看起来更清晰 */ -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } </style>
sass: CSS 预处理器
安装
- npm i sass -D
使用
<style lang="scss" scoped> .home-top { background: linear-gradient(to right, rgb(53, 200, 250), rgb(31, 175, 243)); color: white; .top { display: flex; align-items: center; // 垂直居中 padding: 10px 10px 0 10px; line-height: 15px; font-size: 15px; font-weight: bold; .location-icon { width: 24px; height: 24px; } .location { flex: 1; } .shopcart-icon { width: 24px; height: 24px; } .comments-icon { width: 28px; height: 24px; margin-left: 10px; } } .search-recomment { display: flex; padding: 0 10px 10px; .tag { font-size: 12px; border-radius: 10px; background: rgb(242, 242, 242, 0.3); padding: 2px 8px; margin-right: 10px; } } } </style>
如何使用图片
配置
- 在vite.config.ts配置文件中配置
import path from 'path' const resolve = (dir: string) => path.join(__dirname, dir) export default defineConfig({ resolve: { alias: { '@': resolve('src') } } })
使用
<template> <div class="home-top"> <div class="top"> <img class="location-icon" src="@/assets/imgs/index_page/location.png" /> <div class="location">幸福小区(东南门)</div> <img class="shopcart-icon" src="@/assets/imgs/index_page/shopcart.png" /> <img class="comments-icon" src="@/assets/imgs/index_page/comments.png" /> </div> <VanSearch shape="round" background="linear-gradient(to right, rgb(53,200,250), rgb(31,175,243))" placeholder="世界茶饮 35减2" > <template #right-icon> <div>搜索</div> </template> </VanSearch> <div class="search-recomment"> <div v-for="v in recomments" :key="v.value" class="tag">{{ v.label }}</div> </div> </div> </template>
封装 TheTop 组件
创建组件
- 在src\views\tabs\home目录下创建components文件夹
- 在文件夹中创建TheTop.vue组件
<script setup lang="ts"> import type { ISearchRecomment } from '@/types' interface IProps { recomments: ISearchRecomment[] } defineProps<IProps>() </script> <template> <div class="home-top"> <div class="top"> <img class="location-icon" src="@/assets/imgs/index_page/location.png" /> <div class="location">幸福小区(东南门)</div> <img class="shopcart-icon" src="@/assets/imgs/index_page/shopcart.png" /> <img class="comments-icon" src="@/assets/imgs/index_page/comments.png" /> </div> <VanSearch shape="round" background="linear-gradient(to right, rgb(53,200,250), rgb(31,175,243))" placeholder="世界茶饮 35减2" > <template #right-icon> <div>搜索</div> </template> </VanSearch> <div class="search-recomment"> <div v-for="v in recomments" :key="v.value" class="tag">{{ v.label }}</div> </div> </div> </template> <style lang="scss" scoped> .home-top { background: linear-gradient(to right, rgb(53, 200, 250), rgb(31, 175, 243)); color: white; .top { display: flex; align-items: center; // 垂直居中 padding: 10px 10px 0 10px; line-height: 15px; font-size: 15px; font-weight: bold; .location-icon { width: 24px; height: 24px; } .location { flex: 1; } .shopcart-icon { width: 24px; height: 24px; } .comments-icon { width: 28px; height: 24px; margin-left: 10px; } } .search-recomment { display: flex; padding: 0 10px 10px; .tag { font-size: 12px; border-radius: 10px; background: rgb(242, 242, 242, 0.3); padding: 2px 8px; margin-right: 10px; } } } </style> <style lang="scss"> .home-top { .van-field__right-icon { margin-right: 0; } } </style>
使用组件
在src\views\tabs\home\HomeView.vue文件中使用
<script setup lang="ts"> // 导入组件 import TheTop from './components/TheTop.vue' const recomments = [ { value: 1, label: '牛腩' }, { value: 2, label: '红烧肉' }, { value: 3, label: '宫保鸡丁' } ] </script> <template> <div class="home-page"> // 引用组件 <TheTop :recomments="recomments" /> </div> </template> <style> .test { font-size: 39px; } </style>
使用 VanSearch 组件
配置
- 在src\main.ts注册引入组件
// 全局注册引入组件 import { Tabbar, TabbarItem, Search } from 'vant' app.use(Tabbar) app.use(TabbarItem) app.use(Search)
使用
<template> <div class="home-top"> <div class="top"> <img class="location-icon" src="@/assets/imgs/index_page/location.png" /> <div class="location">幸福小区(东南门)</div> <img class="shopcart-icon" src="@/assets/imgs/index_page/shopcart.png" /> <img class="comments-icon" src="@/assets/imgs/index_page/comments.png" /> </div> <VanSearch shape="round" background="linear-gradient(to right, rgb(53,200,250), rgb(31,175,243))" placeholder="世界茶饮 35减2" > <template #right-icon> <div>搜索</div> </template> </VanSearch> <div class="search-recomment"> <div v-for="v in recomments" :key="v.value" class="tag">{{ v.label }}</div> </div> </div> </template>
使用 defineProps
定义
defineProps
函数允许我们定义一个组件的属性。这个函数可以在组件内部使用,用于声明组件所接收的属性,并指定它们的类型、默认值等信息。使用
<script setup lang="ts"> import type { ISearchRecomment } from '@/types' interface IProps { recomments: ISearchRecomment[] } defineProps<IProps>() </script> <template> <div class="home-top"> <div class="top"> <img class="location-icon" src="@/assets/imgs/index_page/location.png" /> <div class="location">幸福小区(东南门)</div> <img class="shopcart-icon" src="@/assets/imgs/index_page/shopcart.png" /> <img class="comments-icon" src="@/assets/imgs/index_page/comments.png" /> </div> <VanSearch shape="round" background="linear-gradient(to right, rgb(53,200,250), rgb(31,175,243))" placeholder="世界茶饮 35减2" > <template #right-icon> <div>搜索</div> </template> </VanSearch> <div class="search-recomment"> <div v-for="v in recomments" :key="v.value" class="tag">{{ v.label }}</div> </div> </div> </template>
定义组件的 Props
定义
- 在Vue中,通过定义组件的
props
选项来声明组件的属性。属性是从父组件传递给子组件的数据,通过属性可以实现父子组件之间的通信。- 在Vue 3中,可以使用
defineProps
函数来定义组件的属性。- 无论是使用
defineProps
函数还是直接在props
选项中定义属性,都可以通过父组件传递数据给子组件的方式来使用这些属性。使用
- 例如,在父组件中使用子组件并传递属性:
<script setup lang="ts"> import TheTop from './components/TheTop.vue' const recomments = [ { value: 1, label: '牛腩' }, { value: 2, label: '红烧肉' }, { value: 3, label: '宫保鸡丁' } ] </script> <template> <div class="home-page"> <TheTop :recomments="recomments" /> </div> </template> <style> .test { font-size: 39px; } </style>
- 在子组件中,我们可以通过
props/defineProps
对象来访问这些属性,并且引入:<script setup lang="ts"> import type { ISearchRecomment } from '@/types' interface IProps { recomments: ISearchRecomment[] } defineProps<IProps>() </script> <template> <div class="home-top"> <div class="top"> <img class="location-icon" src="@/assets/imgs/index_page/location.png" /> <div class="location">幸福小区(东南门)</div> <img class="shopcart-icon" src="@/assets/imgs/index_page/shopcart.png" /> <img class="comments-icon" src="@/assets/imgs/index_page/comments.png" /> </div> <VanSearch shape="round" background="linear-gradient(to right, rgb(53,200,250), rgb(31,175,243))" placeholder="世界茶饮 35减2" > <template #right-icon> <div>搜索</div> </template> </VanSearch> <div class="search-recomment"> <div v-for="v in recomments" :key="v.value" class="tag">{{ v.label }}</div> </div> </div> </template>
声明 Props 的类型文件
声明
- 使用
defineProps<
IProps>()
来声明组件的属性。这会将IProps中定义的属性作为组件的属性,并提供类型检查和类型提示。使用
<script setup lang="ts"> import type { ISearchRecomment } from '@/types' interface IProps { recomments: ISearchRecomment[] } defineProps<IProps>() </script>