微信小程序继承

1、首先编写基类Base

class Base{
    
    
    page = 1
    count = 4
    data = []
    //是否有更多数据
    hasMoreData = true

    /**
     * 重置属性值
     */
    reset() {
    
    
        this.page = 1
        this.count = 4
        this.data = []
        this.hasMoreData = true
        return this //返回当前的实例对象
    }
}

export default Base

2、继承基类
先前的Service类

import Http from "../utils/http";
import Base from "./base";

class Service extends Base{
    
    

    /**
     *
     * @param category_id 分类ID 可以为null
     * @param type 类型 可以为null
     */
    async getServiceList(category_id = null, type = null) {
    
    
        //避免重复加载
        if (!this.hasMoreData) {
    
    
            return this.data
        }
        //发起网络请求
        //统一网络响应处理,统一网络处理
        const serviceList = await Http.request({
    
    
            url: "v1/service/list", data: {
    
    
                page: this.page,
                count: this.count,
                category_id: category_id || '', //判断category_id是否为null,如果为null,则赋值''
                type: type || ''
            }
        })
        //合并数据
        this.data = this.data.concat(serviceList.data)
        //判断是否有更多数据
        this.hasMoreData = !(this.page === serviceList.last_page)
        this.page++
        return this.data
    }

    static getServiceById(serviceId) {
    
    
        return Http.request({
    
    
            url: `v1/service/${
      
      serviceId}`
        })
    }

}

export default Service

服务列表类

import Http from "../utils/http";
import Base from "./base";

class Rating extends Base {
    
    

    async getServiceRatingList(service_id) {
    
    
        if (!this.hasMoreData) {
    
    
            return this.data
        }
        const ratingList = await Http.request({
    
    
            url: 'v1/rating/service',
            data: {
    
    
                service_id: service_id,
                page: this.page,
                count: this.count
            }
        })
        this.data = this.data.concat(ratingList.data)
        this.hasMoreData = !(this.page === ratingList.last_page)
        this.page++
        return this.data
    }
}

export default Rating

猜你喜欢

转载自blog.csdn.net/xiaoduzi1991/article/details/125296443