布局分析
- 水平方向
- 页面左侧定宽 右侧 自然撑开
- 垂直方向
- 头尾 定高 中间自然撑开
height:calc(100vh - XXrpx)
内容减去顶部导航的高度
- 头尾 定高 中间自然撑开
- 左侧滚动
overflow: auto
显示滚动条
- 右侧滚动
```vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<a name="b7W3d"></a>
### 数据分析及处理
> 数据是一次性返回全部的条例,将数组拆分成两部分
> 左侧的标题数据 右侧的商品数组
根据当前项的下标动态添加选中类<br />样式使用伪元素
```vue
<template>
<view>
<Search></Search>
<view class="content">
<view class="menu">
<view :class="['menu-item', current === index? 'active' : '']" v-for="(item,index) in menus" :key="item">
{{item}}
</view>
</view>
<view class="goods">
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
// 当前选中项的下标
current: 0,
// 菜单数据
menus:[],
}
},
methods: {
async getCategories() {
const res = await this.$u.get('/categories');
console.log(res);
this.menus = res.message.map(item => item.cat_name)
},
handleActive(index) {
this.current = index
}
},
onLoad() {
this.getCategories()
}
}
</script>
<style lang="scss">
.content{
height: calc(100vh - 64rpx);
display: flex;
.menu{
width: 182rpx;
overflow: auto;
.menu-item{
font-size: 28rpx;
height: 72rpx;
display: flex;
justify-content: center;
align-items: center;
}
.active{
position: relative;
color: #EA4350;
&::before{
content: "";
position: absolute;
width: 3px;
height: 80%;
top: 50%;
transform: translateY(-50%);
left: 0;
background-color: #eb4450;
}
}
}
.goods{
flex: 1;
overflow: auto;
}
}
</style>
将商品数据取出来,根据数据结构两层循环渲染
- uniapp中规定
- 如果在 data中定义过变量
- 全局变量 可以在任意地方 this.xx 方式来使用
- 这个变量编译到小程序的时候,会被存放在小程序data - 在标签中直接使用
- 如果没有在data中定义 变量 而是在 其他地方直接 写 this.xxx
- this.xxx 也是全局变量
- 不会把这个数据 编译到小程序data中, 在标签中 无法使用它!!
如果只在uniapp中使用 页面级别的全局变量 直接
this.xxx
即可使用 不需要在data中定义 在data中定义的变量一定要在标签中直接使用 在data中定义的数据都要在标签中体现出来 否则 就是不好的代码 影响性能小程序体验会特卡!!!
- 如果在 data中定义过变量
<template>
<view>
<view class="content">
<view class="goods">
<view
class="goods-item"
v-for="item in goods"
:key="item.cat_id">
<!-- 标题 -->
<view class="goods-title">
{{ item.cat_name }}
</view>
<!-- 商品 -->
<view class="goods-modle">
<navigator url=""
v-for="good in item.children"
:key="good.cat_id">
<!-- 图片 -->
<u-image
mode="widthFix"
width="100%"
:src="good.cat_icon">
</u-image>
<!-- 文字 -->
<text>{{ good.cat_name }}</text>
</navigator>
</view>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
data () {
return {
// 当前选中项的下标
current: 0,
// 商品数据
goods: []
}
},
methods: {
async getCategories () {
const res = await this.$u.get('/categories')
console.log(res)
/**
* 如果变量没由使用在页面中 可以不在data中定义 直接使用
* 这样性能会更好一点
*/
this.categories = res.message
// 下标0 这里为默认
this.goods = this.categories[0].children
},
handleActive (index) {
this.current = index
// 根据下标切换不同的商品内容
this.goods = this.categories[this.current].children
}
},
onLoad () {
this.getCategories()
}
}
</script>
<style lang="scss">
.goods {
flex: 1;
overflow: auto;
.goods-item {
.goods-title {
font-size: 26rpx;
height: 72rpx;
display: flex;
justify-content: center;
align-items: center;
&::before {
content: '/';
color: #ccc;
margin-right: 20rpx;
}
&::after {
content: '/';
color: #ccc;
margin-left: 20rpx;
}
}
.goods-modle {
display: flex;
flex-wrap: wrap;
navigator {
width: 33.33%;
padding: 15rpx;
text-align: center;
}
}
}
}
}
</style>