• 前端开发总结的一些技巧和实用方法(1)


    1.快速从url获取参数

    1. //http://www.xxxx.com?code=123&id=1
    2. let code = new URL(location.href).searchParams.get('code')

    Copy

    2.滚动触底分页加载数据

    1. data(){
    2. return {
    3. loading: false,
    4. page:1,
    5. limit:10,
    6. threadList:[]
    7. }
    8. },
    9. mounted(){
    10. window.addEventListener('scroll', this.handleScroll) // 监听页面滚动
    11. },
    12. methods:{
    13. handleScroll() {
    14. let _this = this;
    15. let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
    16. let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
    17. let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
    18. this.scrollTop = scrollTop;
    19. //触底分页
    20. if(scrollTop + windowHeight >= scrollHeight-20 && !this.threadForm.more){
    21. console.log("分页");
    22. this.getThreadList();
    23. },
    24. getThreadList(){
    25. if(this.loading) return
    26. this.loading = true;
    27. fetch(`/api/getList?page=${this.page}&limit=&{this.limit}`).then(response => response.json()).then(data => {
    28. this.loading = false;
    29. var list = data.data;
    30. var showStatus = list.length < this.limit;
    31. //接口返回数量小于分页条数为true
    32. this.page++;
    33. this.threadList = this.threadList.concat(list);
    34. this.page = this.page;
    35. })
    36. }
    37. }

    Copy

    3.组装扁平化数组

    1. function arrayToTree(items) {
    2. const result = []; // 存放结果集
    3. const itemMap = {}; //
    4. for (const item of items) {
    5. const id = item.id;
    6. const pid = item.pid;
    7. if (!itemMap[id]) {
    8. itemMap[id] = {
    9. children: [],
    10. }
    11. }
    12. itemMap[id] = {
    13. ...item,
    14. children: itemMap[id]['children']
    15. }
    16. const treeItem = itemMap[id];
    17. if (pid === 0) {
    18. result.push(treeItem);
    19. } else {
    20. if (!itemMap[pid]) {
    21. itemMap[pid] = {
    22. children: [],
    23. }
    24. }
    25. itemMap[pid].children.push(treeItem)
    26. }
    27. }
    28. return result;
    29. }

    Copy

    4.合并数据

    1. const a = [1,2,3];
    2. const b = [1,5,6];
    3. const c = [...new Set([...a,...b])];//[1,2,3,5,6]
    4. const obj1 = {
    5. a:1,
    6. }
    7. const obj2 = {
    8. b:1,
    9. }
    10. const obj = {...obj1,...obj2};//{a:1,b:1}

    Copy

    5.简化if判断条件

    1. //不好的方法
    2. if(type == 1 || type == 2 || type == 3 ||type ==4 ||{}
    3. //建议的方法
    4. const condition = [1,2,3,4];
    5. if( condition.includes(type) ){
    6. //...
    7. }
    8. //字符串是否包含另一个字符串
    9. let str = "I Love javascript"
    10. str.includes("javaScript");

    Copy

    6.前端列表模糊搜索

    1. //模糊搜索
    2. /*添加防抖*/
    3. setValue: Debounce(function (e) {
    4. this.newSearch();
    5. }),
    6. newSearch() {
    7. //this.searchKeyword为输入框的关键词,用列表的数据去匹配输入框的关键词进行筛选
    8. this.searchData.data = this.tableData.data.filter((item) => item.pro_name.indexOf(this.searchKeyword) > -1);
    9. },

    Copy

    7.输入框非空的判断

    1. //错误方式
    2. if(value !== null && value !== undefined && value !== '')
    3. //建议方式
    4. if((value??'') !== '')

    Copy

    8.深克隆

    1. /深克隆
    2. deepClone(obj) {
    3. let newObj = Array.isArray(obj) ? [] : {}
    4. if (obj && typeof obj === "object") {
    5. for (let key in obj) {
    6. if (obj.hasOwnProperty(key)) {
    7. newObj[key] = (obj && typeof obj[key] === 'object') ? this.deepClone(obj[key]) : obj[key];
    8. }
    9. }
    10. }
    11. return newObj
    12. },

    Copy

    9.轮训定时器

    1. createSetInterval(order_id) {
    2. this.stopSetInterval();
    3. //this.timer = '';
    4. this.timer = setInterval(() => {
    5. this.getOrderResult(order_id);
    6. }, 2000);
    7. },
    8. stopSetInterval() {
    9. if (this.timer) {
    10. clearInterval(this.timer);
    11. this.timer = null;
    12. }
    13. },
    14. getOrderResult(id){
    15. //接口响应成功以后销毁定时器
    16. fetch('/api/xxx').then(response => response.json()).then(data => {
    17. if (data.status == 200) {
    18. this.stopSetInterval();
    19. }
    20. })
    21. }

    Copy

    10.对象验证方式

    如果我们有一个这样的对象:

    1. const parent = {
    2. child: {
    3. child1: {
    4. child2: {
    5. key: 10
    6. }
    7. }
    8. }
    9. }

    Copy

    很多时候我们会这样去写,避免某一层级不存在导致报错:

    parent && parent.child && parent.child.child1 && parent.child.child1.child2

    Copy

    这样代码看起来就会很臃肿,可以使用JavaScript的可选链运算符:

    parent?.child?.child1?.child2

    这样实现和效果和上面的一大长串是一样的。​

    可选链运算符同样适用于数组:

    1. const array = [1, 2, 3];
    2. array?.[5]

    Copy

    可选链运算符允许我们读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效。在引用为空(null 或者 undefined) 的情况下不会引起错误,该表达式短路返回值是 undefined。与函数调用一起使用时,如果给定的函数不存在,则返回 undefined。

  • 相关阅读:
    k8s基础概论总结&原理
    862. 和至少为 K 的最短子数组
    ArcGIS计算图斑四至坐标原来这么简单!可不要在走弯路哦
    Salesforce-Visualforce-6.静态资源(Static Resources)
    给我5分钟,手把手带你学会定时任务!
    模糊控制器实现对某个对象追踪输入
    XCKU5P-2FFVB676E 赛灵思FPGA可编程逻辑芯片器件 XILINX
    【译】宣布在 Visual Studio 17.10 预览2中为 ARM64 架构提供 SSDT
    FF300R08W2P2B11A 汽车用EasyPACK 模块 2 个独立式
    千亿级大数据如何存储的?
  • 原文地址:https://blog.csdn.net/luxiaol/article/details/134402734