在vue项目新增loading

有时候在elementui项目中一个Loading并不满足需求。所以要额外增加其他的Loading来展示

1 在App.vue中是增加一个transition标签用来过渡

<transition name="load">
    <div v-if="loadShow" class="load">
        <img src="/static/images/load.png"/>
    </div>
</transition>

name是自己命名的class的名称,用来写动画样式,如果不写name  则默认是v

对应样式名称如下:写样式的时候,v的地方要改fade.

如:

.load-enter-active, .load-leave-active {

  transition: opacity .5s;

}

2 加载条样式

.load{
  	position: fixed;
  	z-index: 9000;
  	top: 0;
  	left: 0;
  	right: 0;
  	bottom: 0;
  	width: 100%;
  	height: 100%;
  	background: rgba(0,0,0,0.5);
  }
  .load img{
  	width: 50px;
  	height: 50px;
  	left: 50%;
  	top: 50%;
  	position: absolute;
  	margin-top: -50px;
  	margin-left: -50px;
  	animation: circle 1.5s infinite;
  }
  @keyframes circle{
  	from{
  		transform: rotate(0);
  	}
  	to{
  		transform: rotate(360deg);
  	}
  }
  .load-enter-active, .load-leave-active{
  	transition: opacity .5;
  }

3 状态设置

使用vuex来对loadShow状态进行管理, 更改loadShow的值在App.vue无法检测到状态的变更,需要setInterval()不断获取vuex中loadShow的状态。

所以这里使用$EventBus 事件总线 在main.js中初始化

Vue.prototype.$EventBus = new Vue();

在使用到的地方发送事件

this.$EventBus.$emit('loadShow',true)

在App.vue中接收事件

this.$EventBus.$on('loadShow', (flag)=>{
    this.loadShow= flag
})

Vue 2 项目中使用 Loading 组件可以通过以下步骤实现: 1. 首先,你需要创建一个名为 Loading 的组件。可以在你的项目中创建一个新的 .vue 文件来定义这个组件。这个组件可以包含一个加载动画或者其他你想要展示的内容。 2. 在你的组件中,你可以使用 Vue 的数据绑定来控制 Loading 组件的显示与隐藏。可以在组件的 data 中添加一个布尔类型的变量,比如 `isLoading`,默认值为 `false`。 3. 在需要显示 Loading 组件的地方,可以使用 `v-if` 指令来判断 `isLoading` 是否为 `true`,如果是则显示 Loading 组件。例如: ```html <template> <div> <button @click="showLoading">显示Loading</button> <loading v-if="isLoading"></loading> </div> </template> ``` 4. 在组件的方法中,你可以定义一个函数来改变 `isLoading` 的值,从而控制 Loading 组件的显示与隐藏。例如: ```javascript <script> import Loading from './Loading.vue'; export default { components: { Loading, }, data() { return { isLoading: false, }; }, methods: { showLoading() { this.isLoading = true; // 模拟异步操作 setTimeout(() => { this.isLoading = false; }, 3000); }, }, }; </script> ``` 在上面的示例中,点击按钮会调用 `showLoading` 方法,在方法中将 `isLoading` 设置为 `true`,然后通过 `setTimeout` 模拟一个异步操作,3 秒后将 `isLoading` 设置为 `false`,从而隐藏 Loading 组件。 这样就可以在你的 Vue 2 项目中使用 Loading 组件了。当需要展示加载状态时,通过改变 `isLoading` 的值来显示 Loading 组件,加载完成后再将其隐藏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值