vue3+ts自定义插件

使用:

<template lang="html">
  <div></div>
</template>
<script lang="ts" setup>
import { ref, reactive, getCurrentInstance } from "vue";
const instance = getCurrentInstance();
console.log(instance?.proxy, "instance?.proxy?");
instance?.proxy?._loading.show();
setTimeout(() => {
  instance?.proxy?._loading.hide();
}, 5000);
</script>
<style lang="scss"></style>

全局组件:

loading/index.ts

import { createVNode,VNode,render, type App } from "vue";
import   Loading  from  "./index.vue"
export default {
    install(app: App) {
        const Vnode: VNode = createVNode(Loading);
        render(Vnode, document.body);
        app.config.globalProperties._loading = {
            show: Vnode.component?.exposed?.show,
            hide:Vnode.component?.exposed?.hide
        }
        
    console.log(app,'222',Vnode.component?.exposed)
    }
}

loading/index.vue

<template>
  <div v-if="isShow" class="loading">loading...</div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
const isShow = ref<boolean>(false);
const show = () => (isShow.value = true);
const hide = () => (isShow.value = false);
defineExpose({
  hide,
  show,
  isShow,
});
</script>

<style>
.loading {
  background: black;
  opacity: 0.8;
  font-size: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  color: white;
  position: absolute;
  width: 100vw;
}
</style>

在main.ts中控制

import Loading from "./components/loading";

export const app = createApp(App);
import { myUse } from "./myUse";
myUse(Loading)
// app.use(Loading);

//不提示可以设置属性
type Lod = {
  show: () => void,
  hide:()=>void
}
declare module "@vue/runtime-core" {
  export interface ComponentCustomProperties{
    _loading:Lod
  }
}

myUser.ts

import { App } from "vue";
import  {app} from  "./main"
interface Use{
    install:(app:App,...options:any[])=>void
}


const installList = new Set();

export function myUse<T extends Use>(plugin: T, ...options: any[]) {
    if (installList.has(plugin)) {
        console.error("注册过了",plugin)
    } else {
        plugin.install(app,...options);
        installList.add(plugin)
    }
     
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3+TypeScript的代码规范可以参考以下几点: 1. 使用强类型:在Vue3中,使用TypeScript可以为代码添加类型注解,提供更好的代码提示和类型检查。尽量避免使用any类型,而是明确定义变量、函数和组件的类型。 2. 使用ESLint和Prettier:配置ESLint和Prettier来保持一致的代码风格和规范。可以使用官方提供的eslint-plugin-vue插件来处理Vue特定的规则。 3. 使用单文件组件(SFC):将Vue组件的模板、样式和逻辑封装到单个文件中,提高代码的可维护性和可读性。 4. 组件命名规范:使用有意义且符合驼峰命名规范的组件名。例如,首字母大写的驼峰命名法(PascalCase)。 5. 组件结构化:将组件按照功能模块划分,并保持单一职责原则。将重复或共享的逻辑提取为mixin或自定义指令。 6. Props类型定义:为组件的props定义明确的类型,并使用接口或类型别名来描述props的结构。 7. 组件通信:使用Vue3提供的Composition API来管理组件之间的通信。通过provide/inject或ref/reactive等API来实现组件之间的数据共享和状态管理。 8. 异步处理:使用async/await或Promise来处理异步操作,避免使用回调函数。 9. 错误处理:合理处理错误,可以使用try/catch块来捕获异常,并提供友好的错误提示。 10. 代码注释:为代码添加必要的注释,以便他人理解和维护。注释应该清晰、简洁,并与代码保持同步更新。 以上是一些常见的Vue3+TypeScript代码规范,根据具体项目需求和团队约定,也可以进行适当的调整和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值