首页 > 其他分享 >vue2 lodash函数27 节流的函数、防抖的函数、操作数组的一系列函数、操作对象的一系列函数(对象的深拷贝、浅拷贝)

vue2 lodash函数27 节流的函数、防抖的函数、操作数组的一系列函数、操作对象的一系列函数(对象的深拷贝、浅拷贝)

时间:2023-01-03 16:57:10浏览次数:41  
标签:防抖 return 函数 window import 拷贝 true fn

App.vue

<keep-alive include="Home">
    <router-view />
</keep-alive>

router

  index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/Home'
// 安装VueRouter为vue的插件
Vue.use(VueRouter)
// 创建路由实例
const router = new VueRouter({
  // 路由数组的规则
  routes: [
    { path: '/', component: Home, meta: { isRecord: true, top: 0 } },
    { path: '/user', component: () => import('../views/User/User.vue') }
  ],
  scrollBehavior(to, from, savedPosition) {
    // return 期望滚动到哪个的位置
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: to.meta.top || 0 }
    }
  }
})

export default router

Home

<template>
    <div class="home-container">
        <van-nav-bar title="今日头条" fixed />
        <!-- disabled禁用属性  根据finished的状态禁用 -->
        <van-pull-refresh v-model="isLoading" @refresh="onRefresh" :disabled="finished">
            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
                <articleinfo v-for="item in artlist" :key="item.id" :title="item.title" :author="item.aut_name"
                    :comment="item.comm_count" :pubdate="item.pubdate" :cover="item.cover"></articleinfo>
            </van-list>
        </van-pull-refresh>
    </div>
</template>

<script>
// api接口
import { ArticleListAPI } from '@/api/articleAPI.js'
// 组件
import articleinfo from '@/components/articleinfo.vue'
// lodash 提供了许多函数 节流的函数、防抖的函数、操作数组的一系列函数、操作对象的一系列函数(对象的深拷贝、浅拷贝)
import _ from 'lodash'
let fn = null

export default {
    name: 'Home',
    components: {
        articleinfo
    },
    data() {
        return {
            // 页码值
            page: 1,
            // 显示多少页
            limit: 10,
            // 文章的数组
            artlist: [],
            // 是否加载下一页数据,如果loading为true则不会触发事件
            loading: true,
            // 是否加载完数据,没有更多数据则把finished改为true
            finished: false,
            // 是否正常下拉刷新
            isLoading: false
        }
    },
    activated() {
        fn = this.recordTopHandler()
        window.addEventListener('scroll', fn)
    },
    deactivated() {
        window.removeEventListener('scroll', fn)
    },
    methods: {
        async ArticleList(isRefresh) {
            const { data: res } = await ArticleListAPI(this.page, this.limit)
            this.isLoading = false
            if (isRefresh) {
                // 下拉刷新更多则[新数据,旧数据]
                this.artlist = [...res, ...this.artlist]
            } else {
                // 上拉加载更多则[旧数据,新数据]
                this.artlist = [...this.artlist, ...res]
                this.loading = false
            }
            if (res.length === 0) {
                this.finished = true
            }
        },
        // 上拉加载更多
        onl oad() {
            this.page++
            this.ArticleList()
        },
        // 下拉刷新
        onRefresh() {
            this.page++
            this.ArticleList(true)
        },
        recordTopHandler() {
            return _.debounce(
                () => {
                    this.$route.meta.top = window.scrollY
                },
                50,
                { trailing: true }
            )
        }
    },
    created() {
        this.ArticleList()
    }
}
</script>

主要部分:

import _ from 'lodash'
let fn = null


activated() {
        fn = this.recordTopHandler()
        window.addEventListener('scroll', fn)
    },
    deactivated() {
        window.removeEventListener('scroll', fn)
    },


methods: {
        recordTopHandler() {
            return _.debounce(
                () => {
                    this.$route.meta.top = window.scrollY
                },
                50,
                { trailing: true }
            )
        }
    },

  

标签:防抖,return,函数,window,import,拷贝,true,fn
From: https://www.cnblogs.com/wencaiguagua/p/17022716.html

相关文章

  • mysql中查询结果只保留数字函数
    CREATEDEFINER=`root`@`%`FUNCTION`getNum`(strvarchar(255))RETURNSvarchar(255)CHARSETutf8mb4BEGINDECLAREstrlengthINTDEFAULT0;DECLAREtempvarchar......
  • C++_函数概览
    函数常规函数主函数空函数递归函数内联函数inline函数模板:模板是基于用户为模板参数提供的参数在编译时生成普通类型或函数的构造模板函数类的成员函......
  • 消息服务 + Serverless 函数计算如何助力企业降本提效?
    作者:柳下背景介绍消息队列服务(下文均以MessageService命名)作为云计算PaaS领域的基础设施之一,其高并发、削峰填谷的特性愈发受到开发者关注。MessageService对上承接......
  • 1、尾递归及优化 ,例:斐波那契数列 2、递归转循环,蹦床函数
    1、函数调用自身,即为递归,在return时调用自身,即为尾递归;递归非常消耗内存,其原因是需要同时保存成成百上千的调用帧,这容易发生栈溢出错误;但是尾递归只存在一个调用帧,所以永......
  • 消息服务 + Serverless 函数计算如何助力企业降本提效?
    作者:柳下背景介绍消息队列服务(下文均以MessageService命名)作为云计算PaaS领域的基础设施之一,其高并发、削峰填谷的特性愈发受到开发者关注。MessageService对上承......
  • java8中常用函数式接口Supplier<T>、Consumer<T>、Function<T,R>、Predicate<T>使用示
    场景函数式接口(FunctionalInterface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。而java中的函数式编程体现就是Lambda,所以函数式接口就是可以适用......
  • c语言--进程相关函数
    1.获取进程ID头文件:#include<unistd.h>pid_tgetpid(void):获取进程IDpid_tgetppid(void):获取父进程IDpid_tgetuid(void):获取用户IDpid_tgeteuid(void):获取有效......
  • 函数的平移
    把函数y=2x的图像上的所有点(),就可以得到y=2x−3−1的图像A,向右平移3个单位长度,再向下平移1个单位长度B,向总平移3个单位长度,再向下平移1个单位长度C,向右平移3个单位长......
  • JavaScript 深拷贝
    在进行赋值之前,为指针类型的数据成员另辟了一个独立的内存空间,实现真正内容上的拷贝。这种拷贝称为深拷贝。————百度百科源码⚠还有部分类型不支持typescript:/......
  • 函数和递归
    函数是什么库函数自定义函数函数参数函数调用函数的嵌套调用和链式访问函数的声明和定义函数递归函数是什么?维基百科定义:子程序在计算机科学中,子程序是一个大型程序中的部分......