传给list-card。//这里的@change可是系统的哈。......_uniapp 选项卡">
当前位置:   article > 正文

uniapp实现选项卡绑定切换、懒加载_uniapp 选项卡

uniapp 选项卡

一、选项卡切换

目录

一、选项卡切换

1.用swiper

 2.绑定事件 用 change事件

绑定change

目标组件接收

子组件发送

index里面传参数

tab里面改

怎么实现 点击tab——>滑动?

1.tab 传给index

2.index传给list

二、从数据库中拿数据()

别看“二、”这么多哈 都是虚的  目的就是 把云函数获取的数组 传给list-card list引用后 传给list-item(看看那个云数组有几条?好显示) ->list-card

1.创建新云函数 

 1.创建数据库引用

2.对哪个表? article

3.field content:false 不要content

4.return 

2.在api中导出

3.list.vue组件中引用这个api

1.methods:里面写函数

2.组件生命周期调用

效果:

 4.发送给 这个组件

list-item接收:props中接收->

5.list-item组件处理它:

2.item传给list-card

云函数获取的数组 随便展开一条,发现里面有mode

使用:

三、实现标签分类

1.在云函数里接收参数

2.用聚合

3.改参数哈  完成懒加载!


1.用swiper

  1. <swiper style="height: 100%;">
  2. <swiper-item class="home-swiper">
  3. <view>我是小狗</view>
  4. <view>我是小狗</view>
  5. <view>我是小狗</view>
  6. <list-card></list-card>
  7. </swiper-item>
  8. </swiper>

效果:可以左右滑动

 2.绑定事件 用 change事件

绑定change

//这里的@change可是系统的哈

<swiper @change="change">

目标组件接收

@自己定义的名=“用啥处理”

  1. <list :tab="tabList" @change="change"></list>
  2. <!-- //index界面里接受 发来的current(当前选项卡的下标数) -->

这里的@change是自己定义的哈 可不是系统的 是因为咱 在定义子组件发送时间名的时候 设置的 

子组件发送

this.$emit('change', current)

  1. change(e)
  2. {
  3. console.log('嘘嘘嘘',e);
  4. const {current}=e.detail;//取到 current这个数据
  5. this.$emit('change', current)
  6. }

index里面传参数

	<tab :list="tabList" :tabIndex="tabIndex"  @tab="tab"></tab>

tab里面改

首先props接收index 传来的参数 然后在watch里监听它

  1. watch:{
  2. tabIndex(a,b){//监听哪个就写哪个 把props里面的属性写成函数就行啦
  3. console.log('汪汪汪',a,b)//a是改变后的数据 b是改变前的数据
  4. this.activeIndex=a;
  5. }

activeindex 你自己定义的 你一变 那啥也变

这样就是实现了 滑动 ——>上面的tab跟着变

怎么实现 点击tab——>滑动?

首先 获取tab点击时 的下标传递给 list (list里面写了swiper swiper里有个current   :current="activeIndex" 会自动跳到咱想要的界面)

1.tab 传给index

发送:

  1. this.$emit('tab', {
  2. data: item,
  3. index: index
  4. })

接收:

@tab="tab"

2.index传给list

index定义一个activeIndex

	<list :tab="tabList" @change="change" :activeIndex="activeIndex"></list>

list用 props接收一个activeIndex

  1. activeIndex:{
  2. type:Number,
  3. default:0
  4. }

接收后直接

<swiper @change="change" :current="activeIndex">

完成了双向滑动啦

二、从数据库中拿数据()

别看“二、”这么多哈 都是虚的  目的就是 把云函数获取的数组 传给list-card list引用后 传给list-item(看看那个云数组有几条?好显示) ->list-card

1.创建新云函数 

  1. 'use strict';
  2. const db=uniCloud.database()//1.创建引用
  3. exports.main = async (event, context) => {
  4. //event为客户端上传的参数
  5. const list =await db.collection('article') //2.创建
  6. .field({//指定返回的字段
  7. content:false,//TRUE 表示只返回content
  8. }).get()
  9. //返回数据给客户端
  10. return {
  11. code: 200,
  12. msg: '数据请求成功',
  13. data: list.data
  14. }
  15. };

 1.创建数据库引用

2.对哪个表? article

3.field content:false 不要content

4.return 

2.在api中导出

(原理不在此赘述)

  1. export const get_list = (data) => {
  2. return $http({
  3. url: 'get_list',
  4. data
  5. })
  6. }

3.list.vue组件中引用这个api

1.methods:里面写函数

  1. getList()
  2. {
  3. this.$api.get_list().then(res=>{
  4. console.log('小狗狗',res);//验证对不对 res
  5. this.list=res.data
  6. const {data}=res;//这俩一个效果
  7. console.log('小白白',data);
  8. })
  9. //console.log('小狗狗');

2.组件生命周期调用

  1. created(){
  2. this.getList()
  3. },

效果:

 4.发送给<list-item :list="list"></list-item> 这个组件

list中发送: :list="list"

list-item接收:props中接收->

  1. props: {//获取 list引用api中数据库返回的数组
  2. list: {
  3. type: Array,
  4. default () {
  5. return []
  6. }
  7. },
<list-item :list="list"></list-item>

onLoad 在页面 ,created 组件

5.list-item组件处理它:

1.用咱拿到的数组 v-for="item in list" (看这里的item 它是list数组中的一个哈 鸟小 五脏俱全

<list-card mode="base" :item="item" v-for="item in list" :key="item._id"></list-card>

但这样只是拿到了几个 并没有真正把数据整到界面上 因为咱那个界面是 list-card里面写的界面内容--->传给list-card

2.item传给list-card

云函数获取的数组 随便展开一条,发现里面有mode

接收:props: {
            item: {
                type: Object,
                default () {
                    return {}
                }
            }
        },

使用:

先复制一个 一共仨 一个道理 这里只copy了一个哈

  1. <view v-if="item.mode === 'base'" class="listcard">
  2. <view class="listcard-image">
  3. <image :src="item.cover[0]" mode="aspectFill"></image>
  4. </view>
  5. <view class="listcard-content">
  6. <view class="listcard-content__title">
  7. <text>{{item.title}}</text>
  8. </view>
  9. <view class="listcard-content__des">
  10. <view class="listcard-content__des-label">
  11. <view class="listcard-content__des-label-item">{{item.classify}}</view>
  12. </view>
  13. <view class="listcard-content__des-browse">{{item.browse_count}}浏览</view>
  14. </view>
  15. </view>
  16. </view>

这样就可以啦!!!

三、实现标签分类

很明显哈 这里面需要根据标签名传参数 所以云函数得能接受参数

1.在云函数里接收参数

  1. const {
  2. name,
  3. } = event//等同 var name=event.name

2.用聚合

  1. const list =await db.collection('article') //2.创建
  2. .aggregate()//获取聚合操作实例
  3. .match({
  4. classify:'前端开发'
  5. })//筛选出classify是前端开发的
  6. .project({
  7. content:0
  8. })//类似.field
  9. .end()

返回的结果全是前端开发的

3.改参数哈  完成懒加载!

所谓懒加载就是,存到数组里面 加载过的不用每次都加载

  1. watch: {
  2. tab(newVal) {
  3. if (newVal.length === 0) return
  4. this.listCatchData = {}
  5. this.getList(this.activeIndex)
  6. }
  7. },
  8. methods: {
  9. getList(current) {
  10. this.$api.get_list({
  11. name: this.tab[current].name,
  12. }).then(res => {
  13. console.log(res);
  14. const {
  15. data
  16. } = res
  17. this.$set(this.listCatchData, current, data)
  18. })
  19. },
  20. change(e) {
  21. console.log('嘘嘘嘘', e);
  22. const {
  23. current
  24. } = e.detail; //取到 current这个数据
  25. this.$emit('change', current)
  26. console.log("呆呆:", this.tab[current].name)
  27. console.log("笨笨:", current)
  28. this.getList(current)
  29. },

 ok 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/312421
推荐阅读
相关标签
  

闽ICP备14008679号