vue3 h函数 进阶用法

vue3 h函数 进阶用法

默认我们放到body中就可以了 如果想放在某个div下 把div传过来就好了
props:就相当于父传子的数据

import { render, h } from 'vue';
import showLoading from '../components/showLoading/src/index.vue';

export const showLoading = (props: any,dom) => {
  const handleDestroy = () => {
    // 从 body 上移除组件
    render(null, dom || document.body);
  };
  const vnode = h(showLoading, { ...props, destroy: handleDestroy });
  // 使用 render 函数将 vnode 渲染为真实DOM并挂载到 body 上
  render(vnode, dom || document.body);
};
export const hideLoading = (props: any) => {
  render(null, dom || document.body);
};

使用:

import { showLoading, hideLoading } from './lib/showLoading';
showLoading({
  message: '加载中...',
  color: '#fff',
  size: '30',
})//关闭
hideLoading()

下面是部分的页面代码

<template>
  <div class="xxx-loading-mark" v-if="isVisable">
    <xxx-loading :color="color || '#fff'" :type="type" :size="size" :textSize="textSize" :textColor="textColor" :vertical="vertical">
      {{ message }}
    </xxx-loading>
  </div>
</template>
<script setup lang="ts">
import xxxLoading from '../../xxxLoading';

import { onMounted, defineProps, ref } from 'vue';
const props = defineProps({
  /** 颜色 */
  color: {
    type: String,
    defalut: '#fff',
  },
  /** 	loading文案 */
  message: {
    type: [String, Number],
    defalut: '',
  },
  /** 	是否设置自动关闭时间 */
  duration: {
    type: Number,
    defalut: 3000,
  },
  destroy: {
    type: Function,
  },
});
const isVisable = ref(true);
// 控制显示处理
onMounted(() => {
  if (props.duration) {
    setTimeout(() => {
      isVisable.value = false;
      props.destroy && props.destroy();
    }, props.duration);
  }
});
</script>
<script lang="ts">
export default {
  name: 'ShowLoading',
};
</script>

<style lang="scss" scoped>
.xxx-loading-mark {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.3);
  color: #fff;
}
</style>

注:由于之前是搭建的组件库 示例代码当作参考 其实组件就跟我们平常写组件一样就可以了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 3中,使用`createApp`函数来创建Vue实例,而不是使用`new Vue()`。`createApp`函数接受一个根组件作为参数,并返回一个应用实例。下面是一个示例代码: ```javascript import { createApp } from 'vue'; import App from './App.vue'; const app = createApp(App); app.mount('#app'); ``` 在这个示例中,我们首先导入`createApp`函数和根组件`App`。然后,我们使用`createApp`函数创建一个应用实例,并将根组件作为参数传递给它。最后,我们使用`mount`方法将应用实例挂载到指定的DOM元素上。 需要注意的是,Vue 3中的`createApp`函数返回的是一个应用实例,而不是Vue构造函数本身。因此,我们不再需要使用`new`关键字来创建Vue实例。 #### 引用[.reference_title] - *1* [【vue3系列】vue3快速入门教程](https://blog.csdn.net/qq_42445025/article/details/120749719)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Vue进阶—new Vue()时发生了什么](https://blog.csdn.net/weixin_43974265/article/details/114181405)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【vue】关于new Vue()与Vue.use()](https://blog.csdn.net/zhangank/article/details/116742961)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值