首页 > 其他分享 >vue封装一个加载过程

vue封装一个加载过程

时间:2023-11-12 17:56:30浏览次数:37  
标签:flex vue 封装 height width axios news left 加载

app.vue
<template>
  <div class="main">
    <div class="box" v-isLoging="isLoged">
      <ul>
        <li v-for="item in list" :key="item.id" class="news">
          <div class="left">
            <div class="title">{{ item.title }}</div>
            <div class="info">
              <span>{{ item.source }}</span>
              <span>{{ item.time }}</span>
            </div>
          </div>

          <div class="right">
            <img :src="item.img" alt="">
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
// 安装axios =>  yarn add axios
import axios from 'axios'

// 接口地址:http://hmajax.itheima.net/api/news
// 请求方式:get
export default {
  data () {
    return {
      list: [],
      isLoged:false
    }
  },
  async created () {
    // 1. 发送请求获取数据
    const res = await axios.get('http://hmajax.itheima.net/api/news')
    
    setTimeout(() => {
      // 2. 更新到 list 中
      this.list = res.data.data,
      this.isLoged=true
    }, 2000)
  },
  directives:{
    isLoging:{
      inserted(el,binding){
        if(!binding.value){
          el.classList.add('loading')
        }
      },
      update(el,binding){
        if(binding.value){
          el.classList.remove('loading')
        }
      }
    }
  }
}
</script>

<style>
/* 伪类 - 蒙层效果 */
.loading:before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: #fff url('./loading.gif') no-repeat center;
}

/* .box2 {
  width: 400px;
  height: 400px;
  border: 2px solid #000;
  position: relative;
} */

.box {
  width: 800px;
  min-height: 500px;
  border: 3px solid orange;
  border-radius: 5px;
  position: relative;
}
.news {
  display: flex;
  height: 120px;
  width: 600px;
  margin: 0 auto;
  padding: 20px 0;
  cursor: pointer;
}
.news .left {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding-right: 10px;
}
.news .left .title {
  font-size: 20px;
}
.news .left .info {
  color: #999999;
}
.news .left .info span {
  margin-right: 20px;
}
.news .right {
  width: 160px;
  height: 120px;
}
.news .right img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

使用v-loging即可实现在加载数据的时候出现加载动

 

标签:flex,vue,封装,height,width,axios,news,left,加载
From: https://www.cnblogs.com/wllovelmbforever/p/17826966.html

相关文章

  • vue项目部署添加时间
    constfs=require('fs');constpath=require('path');constHtmlWebpackPlugin=require('html-webpack-plugin');classBuildTimePlugin{ apply(compiler){  constbuildTime=+newDate()  compiler.hooks.beforeCompile......
  • vue无法正确使用mastache渲染实例
    问题:<script>varvm=newvue({el:"#app",data:{message:"hello,vue!"}});</script>这段代码中,`vm`是通过`newVue()`创建的一个Vue实例。但是在HTML文件中,`<divid="app">`元......
  • Vue中的插槽
    插槽-默认插槽作用:让组件内部的一些结构支持自定义需求:要在页面中显示一个对话框,封装成一个组件,但是组件的内容部分,不是写死的,希望能使用时自定义。插槽的基本语法组件内需要定制的结构部分,改用<slot></slot>占位使用组件时,在组件中传入结构替换slot,例如<组件名>xxxxxx</组......
  • Vue 实验
    项目初始化#前提:包管理器安装vue-clivuecreate项目名称Vue2实验目的了解Vue2的组件实现机制数据绑定机制双向绑定:input单向绑定父组件→子组件:父组件传入的参数组件→用户:页面内部参数属性监听机制:被监听的参数实验内容实现一个简单的组件,体现......
  • vue自定义指令
    app.vue<template><divclass=""><!--自定义指令全局<inputv-focustype="text"name=""id=""><br>自定义指令局部<inputv-focus2type="text"name=""id="&......
  • Vue自定义指令
    自定义指令根据自定义的指令,可以封装一些dom操作,扩展额外的功能全局注册-语法全局注册是在min.js文件中去定义的Vue.directive('指令名',{//inserted:钩子是一个自定义指令的生命周期钩子函数之一。它会在被绑定的元素插入到父节点时调用。"inserted"(el){......
  • vue项目配置国际化
    1、安装vue-i18n+js-cookie插件npminstallvue-i18n-Snpminstalljs-cookie--save即在package.json中dependencies节点添加vue-i18n"vue-i18n":"7.3.2",2、配置多语言文件src目录下创建lang目录,存放国际化文件此处包含三个文件,分别是index.jszh.jsen.js//index.jsim......
  • 一个操作系统的设计与实现——第5章 加载内核
    一直以来,我们都在使用汇编语言对MBR编程,但对于操作系统这样的复杂程序来说,使用汇编语言是比较困难的。本章将实现操作系统内核的加载与进入。5.1读硬盘的实现原理操作系统存储于硬盘中,现在需要将其读出至内存。想要读硬盘,就需要依次进行以下操作:设定读取的扇区数设定起始扇......
  • Vue中的异步更新和 $nextTick
    场景引入需求:当用户点击编辑按钮后,显示一个弹窗,该弹窗有一个文本框,使得文本框自动聚焦看似代码如下:this.isShowEdit=true;//显示输入框this.$refs.inp.focus();//获取焦点代码看似没有问题,显示文本框后,让文本框聚焦,但是在vue中却不能实现,这是由于vue是异步更新Dom的t......
  • vue2.0,把vform666、workFlow开源组件集成到vue-admin-template框架上心得体会
    以上三个都是vue2版本的开源项目,有的已经有vue3版本了,我把他们集成到一起,是出于练习的目的,也是消磨时间。vue-admin-template是一个很基础简洁的后台管理系统框架;vform666是可以用作表单低代码开发的组件项目;workFlow是模仿钉钉的工作流的组件项目,这三个项目在gitee上都能搜索到,......