项目初始化
- 创建项目
- 安装依赖
-
创建页面
首页
index
- 分类
category
- 购物车
cart
- 我的
my
pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "首页"
}
},
{
"path": "pages/category/category",
"style": {
"navigationBarTitleText": "分类"
}
},
{
"path": "pages/cart/cart",
"style": {
"navigationBarTitleText": "购物车"
}
},
{
"path": "pages/my/my",
"style": {
"navigationBarTitleText": "我的"
}
}
],
tabbar
配置下方 tabbar 在uniapp中查找
"globalStyle": {
"navigationBarTextStyle": "white",
"navigationBarTitleText": "黑马优购",
"navigationBarBackgroundColor": "#eb4450",
"backgroundColor": "#F8F8F8"
},
"tabBar": {
"color": "#7A7E83",
"selectedColor": "#eb4450",
"borderStyle": "black",
"backgroundColor": "#fff",
"list": [
{
"pagePath": "pages/index/index",
"iconPath": "static/icons/home.png",
"selectedIconPath": "static/icons/home-o.png",
"text": "首页"
},
{
"pagePath": "pages/category/category",
"iconPath": "static/icons/category.png",
"selectedIconPath": "static/icons/category-o.png",
"text": "分类"
},
{
"pagePath": "pages/cart/cart",
"iconPath": "static/icons/cart.png",
"selectedIconPath": "static/icons/cart-o.png",
"text": "购物车"
},
{
"pagePath": "pages/my/my",
"iconPath": "static/icons/my.png",
"selectedIconPath": "static/icons/my-o.png",
"text": "我的"
}
]
}
拦截器
使用
uview
的拦截器 统一封装接口的基地址 方便后期统一修修改 发送清秀的时候 自动显示和关闭的加载效果 封装后不能使用uniapi的方式
const install = (Vue, vm) => {
Vue.prototype.$u.http.setConfig({
// 基地址
baseUrl: 'https://api-hmugo-web.itheima.net/api/public/v1',
// 超时显示的文字
loadingText: '努力加载中~',
// 显示文字的事件 在xxx毫秒后
loadingTime: 800
});
}
export default {
install
}
// 此为main.js本身已有内容
const app = new Vue({
...App
})
// http拦截器,此为需要加入的内容,如果不是写在common目录,请自行修改引入路径
import httpInterceptor from '@/common/http.interceptor.js'
// 这里需要写在最后,是为了等Vue创建对象完成,引入"app"对象(也即页面的"this"实例)
Vue.use(httpInterceptor, app)
// 这里也是原有的
app.$mount()
async getSwiperList() {
const result = await this.$u.get('/home/swiperdata')
console.log(result);
this.swiperList = result.message
}
首页
搜索栏
search组件 使用uview 组件库中的 顶部的搜索栏在多个页面都有,这里封装成一个组件
<template>
<view class="search">
<u-search
placeholder="搜索"
shape="square"
:show-action="false"
input-align="center"
bg-color="#fff"
></u-search>
</view>
</template>
<script>
export default {
}
</script>
<style>
.search{
padding: 10px;
background-color: #eb4450;
}
</style>
轮播图
uniapi获取轮播图数据,uview轮播图组件来渲染
<template>
<view>
<Search></Search>
<u-swiper :list="swiperList" name="image_src" height="340rpx"></u-swiper>
</view>
</template>
<script>
export default {
onLoad() {
this.getSwiperList()
},
data() {
return {
swiperList: []
}
},
methods: {
async getSwiperList() {
const [err,result] = await uni.request({
url:'https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata'
})
console.log(result);
this.swiperList = result.data.message
}
},
}
</script>
<style>
</style>
导航栏
当父盒子添加了flex布局后 子项 默认宽高 由内容撑开 高度变成父项的高度
这里使用了内置样式 u-flex
、u-flex-1
、u-p-15
详见:https://v1.uviewui.com/components/common.html
<view class="u-flex">
<navigator
v-for="item in catitems"
:key="item.name" class="u-flex-1 u-p-15">
<u-image
mode="widthFix"
:src="item.image_src">
</u-image>
</navigator>
</view>
onLoad() {
// 获取导航栏数据
this.getCatitems()
},
data() {
return {
// 导航栏数据
catitems:[]
}
},
methods: {
// 获取导航栏数据
async getCatitems() {
const result = await this.$u.get('/home/catitems')
// console.log(result);
this.catitems = result.message
}
},
楼层
数据分析,
<template>
<view>
<!-- 楼层 -->
<view v-for="item in floordata" :key="item.floor_title.name">
<!-- 标题 -->
<u-image
:src="item.floor_title.image_src"
mode="widthFix"
>
</u-image>
<!-- 内容 -->
<view class="floor-content">
<navigator
v-for="product in item.product_list"
:key="product.name"
:url="product.navigator_url"
>
<u-image
:src="product.image_src"
width="100%"
height="100%"
mode="scaleToFill"
></u-image>
</navigator>
</view>
</view>
</template>
<script>
export default {
onLoad () {
// 获取楼层数据
this.getFloordata()
},
data () {
return {
// 楼层数据
floordata: []
}
},
methods: {
// 获取楼层数据
async getFloordata () {
const result = await this.$u.get('/home/floordata')
// console.log(result);
this.floordata = result.message
}
},
}
</script>
<style scoped lang="scss">
.floor-content{
// 清除浮动
overflow: hidden;
navigator{
float: left;
margin: 0 10rpx 10rpx;
// 宽度三分之一 750rpx / 3 = 250rpx
width: 250rpx;
}
navigator:first-child{
width: 232rpx;
height: 386rpx;
}
navigator:nth-last-child(-n+4) {
width: 233rpx;
height: 188rpx;
}
navigator:nth-last-child(-n+2) {
margin-top: 5rpx;
}
}
</style>