Vue3.0项目——打造企业级音乐App(二)图片懒加载、v-loading指令的开发和优化

简介: Vue3.0项目——打造企业级音乐App(二)图片懒加载、v-loading指令的开发和优化

项目演示

vue3.0-music

图片懒加载


631206a603814698bfbcd9327beaedfc.pngmain.js 文件

  • 安装 vue3-lazy,在 main.js 文件中导入并使用
  • 传入两个参数,一个是 lazyPlugin,一个是要加载的图片的相对地址
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import lazyPlugin from 'vue3-lazy'
// 引入全局样式文件
import '@/assets/scss/index.scss'
createApp(App).use(store).use(router).use(lazyPlugin, {
    loading: require('@/assets/images/default.png')
}).mount('#app')

recommend.vue 组件

  • 修改 :src 为 v-lazy,实现图片的懒加载
  <div class="icon">
      <img width="60" height="60" v-lazy="item.pic">
    </div>

效果图如下:

1687165598588.png

./components/base/loading/loading.vue 组件

  • 该组件定义加载中的图片和文字显示
<template>
  <div class="loading">
    <div class="loading-content">
      <img width="24" height="24" src="./loading.gif">
      <p class="desc">{{title}}</p>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'loading',
    data() {
      return {
        title: '正在载入...'
      }
    },
    methods: {
      setTitle(title) {
        this.title = title
      }
    }
  }
</script>
<style lang="scss" scoped>...</script>

./components/base/loading/directive.js 文件

  • 该文件自定义 loading 指令
  • 挂载和更新的时候做出相应变化
  • 仅适用于 absolute | fixed | relative 的定位(后面还会优化)
// 自定义指令 loading
import { createApp } from 'vue'
import Loading from './loading'
const loadingDirective = {
    mounted(el, binding) {
        const app = createApp(Loading)
        const instance = app.mount(document.createElement('div'))
        el.instance = instance
        if (binding.value) {
            append(el)
        }
    },
    // 更新的时候,loading 为 true,则还是执行 append(el),为 false,执行 remove(el)
    updated (el, binding) {
        if (binding.value !== binding.oldValue) {
            binding.value ? append(el) : remove(el)
        }
    }
}
function append(el) {
    el.appendChild(el.instance.$el)
}
function remove(el) {
    el.removeChild(el.instance.$el)
}
export default loadingDirective

main.js 文件

  • 导入并全局使用 loadingDirective
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import lazyPlugin from 'vue3-lazy'
import loadingDirective from './components/base/loading/directive'
import '@/assets/scss/index.scss'
createApp(App).use(store).use(router).use(lazyPlugin, {
    loading: require('@/assets/images/default.png')
}).directive('loading', loadingDirective).mount('#app')

./views/recommend.vue 组件

  • v-loading 绑定加载中
<div class="recommend" v-loading="loading">
...
  <h1 class="list-title" v-show="!loading">热门歌单推荐</h1>
</div>

afd312cafd014007af54098686f363bd.jpg

v-loading 自定义指令开发的优化

./components/base/loading/directive.js 文件

  • 当没有设置 position 为 absolute | fixed | relative 其中一个时,自动添加 position: relative
// 自定义指令 loading
import { createApp } from 'vue'
import Loading from './loading'
import { addClass, removeClass } from '@/assets/js/dom'
// g-relative 是在 base.sass 中定义好的 position: relative
const relativeCls = 'g-relative'
const loadingDirective = {
    mounted(el, binding) {
        const app = createApp(Loading)
        const instance = app.mount(document.createElement('div'))
        el.instance = instance
        // 拿到动态参数
        const title = binding.arg
        if (typeof title !== 'undefined') {
            instance.setTitle(title)
        }
        if (binding.value) {
            append(el)
        }
    },
    // 更新的时候,loading 为 true,则还是执行 append(el),为 false,执行 remove(el)
    updated (el, binding) {
        const title = binding.arg
        if (typeof title !== 'undefined') {
            el.instance.setTitle(title)
        }
        if (binding.value !== binding.oldValue) {
            binding.value ? append(el) : remove(el)
        }
    }
}
function append(el) {
    // 获取元素当前样式
    const style = getComputedStyle(el)
    // 如果样式不属于以下三种之一,则给 el 添加需要的定位
    if (['absolute', 'fixed', 'relative'].indexOf(style.position) === -1) {
        // 添加样式
        addClass(el, relativeCls)
    }
    el.appendChild(el.instance.$el)
}
function remove(el) {
    removeClass(el, relativeCls)
    el.removeChild(el.instance.$el)
}
export default loadingDirective

./src/js/dom.js 文件

  • 如果没有 absolute | fixed | relative,则添加样式
  • binding.value 为 false 时,移出样式
export function addClass(el, className) {
  if (!el.classList.contains(className)) {
    el.classList.add(className)
  }
}
export function removeClass(el, className) {
  el.classList.remove(className)
}

recommend.vue 组件

  • 动态获取参数
<div class="recommend" v-loading:[loadingText]="loading">
  ...
</div>
data() {
  return {
    loadingText: '正在载入...'
  }
}

afd312cafd014007af54098686f363bd.jpg

至此,推荐页面的基本开发先告一段落,接下来将进行歌手页面的开发

不积跬步无以至千里 不积小流无以成江海

点个关注不迷路,持续更新中…





相关文章
|
5天前
|
JSON 数据可视化 数据库
vue3+threejs+koa可视化项目——实现登录注册(第三步)
vue3+threejs+koa可视化项目——实现登录注册(第三步)
26 5
|
5天前
|
数据可视化 前端开发 JavaScript
vue3+threejs可视化项目——引入threejs加载钢铁侠模型(第二步)
vue3+threejs可视化项目——引入threejs加载钢铁侠模型(第二步)
42 3
|
5天前
|
JavaScript 数据可视化 算法
vue3+threejs可视化项目——搭建vue3+ts+antd路由布局(第一步)
vue3+threejs可视化项目——搭建vue3+ts+antd路由布局(第一步)
26 6
|
1天前
|
前端开发 Android开发 开发者
【Flutter前端技术开发专栏】Flutter中的混合应用(Hybrid Apps)开发
【4月更文挑战第30天】本文探讨了使用Flutter开发混合应用的方法。混合应用结合Web技术和原生容器,提供快速开发和低成本维护。Flutter,一款现代前端框架,以其插件系统和高性能渲染引擎支持混合应用开发。通过创建Flutter项目、添加平台代码、使用WebView、处理平台间通信以及发布应用,开发者可构建跨平台混合应用。虽然混合应用有性能和用户体验的局限,但Flutter的跨平台兼容性和丰富的插件生态降低了开发成本。开发者应根据项目需求权衡选择。
【Flutter前端技术开发专栏】Flutter中的混合应用(Hybrid Apps)开发
|
5天前
|
JSON 数据可视化 前端开发
vue3+threejs+koa可视化项目——模型文件上传(第四步)
vue3+threejs+koa可视化项目——模型文件上传(第四步)
15 7
|
5天前
|
JavaScript 安全 前端开发
Vue 项目中的权限管理:让页面也学会说“你无权访问!
Vue 项目中的权限管理:让页面也学会说“你无权访问!
15 3
|
7天前
|
JavaScript 前端开发 安全
【Vue】内置指令真的很常用!
【Vue】内置指令真的很常用!
|
7天前
|
资源调度 JavaScript Linux
VueCLI:Vue项目脚手架与构建工具技术详解
【4月更文挑战第24天】VueCLI是Vue.js官方的项目脚手架,简化创建与配置,提供丰富的插件系统,支持全生命周期功能,如代码编译、打包部署。它具有易于配置、跨平台支持等优势。通过安装、创建项目、运行及构建命令,开发者能快速搭建Vue应用。VueCLI允许自定义配置(vue.config.js)和安装各类插件,如vue-router、vuex,以适应不同项目需求,提高开发效率。
|
8天前
|
JavaScript
Vue 如何新建一个项目(如何安装依赖)
Vue 如何新建一个项目(如何安装依赖)
11 0
|
15天前
|
JavaScript
vue3+vite项目配置ESlint
vue3+vite项目配置ESlint
15 0