vue3+ts全局注册方法

使用 provide 和 inject 注册全局

mian.ts中注册

// main.js

import { createApp, provide } from 'vue';
import App from './App.vue';

const app = createApp(App);

// 创建一个全局函数
function globalFunction() {
  console.log('Hello, I am a global function!');
}

// 在应用程序的根组件中使用 provide 将函数提供给所有子组件
app.provide('globalFunction', globalFunction);

app.mount('#app');

在组件中使用

<!-- ChildComponent.vue -->

<template>
  <div>
    <button @click="callGlobalFunction">Call Global Function</button>
  </div>
</template>

<script>
import { inject } from 'vue';

export default {
  setup() {
    // 使用 inject 获取全局函数
    const globalFunction = inject('globalFunction');

    // 在组件中调用全局函数
    const callGlobalFunction = () => {
      globalFunction();
    };

    return {
      callGlobalFunction
    };
  }
};
</script>

还有vue 中的 getCurrentInstance的使用

但是不推荐

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3中使用TypeScript结合Pinia来实现全局loading的方式如下: 首先,我们需要安装vue-router、pinia和axios,可以使用以下命令进行安装: ``` npm install vue-router@next pinia axios ``` 接下来我们创建一个store模块来管理全局的loading状态。在src/store目录下创建一个名为loading.ts的文件,代码如下: ```typescript import { store } from 'pinia'; export const useLoadingStore = store('loading'); export const loadingStore = useLoadingStore({ state: () => ({ isLoading: false, }), actions: { setLoading(loading: boolean) { this.isLoading = loading; }, }, }); ``` 然后在src/main.ts文件中注册pinia和创建一个全局的loading插件,代码如下: ```typescript import { createApp } from 'vue'; import { createPinia } from 'pinia'; import App from './App.vue'; import router from './router'; import axios from 'axios'; import { loadingStore } from './store/loading'; const app = createApp(App); const pinia = createPinia(); app.use(pinia); // 创建全局loading插件 app.config.globalProperties.$loading = { show() { loadingStore.setLoading(true); }, hide() { loadingStore.setLoading(false); }, }; // axios拦截器 axios.interceptors.request.use(function (config) { loadingStore.setLoading(true); return config; }, function (error) { return Promise.reject(error); }); axios.interceptors.response.use(function (response) { loadingStore.setLoading(false); return response; }, function (error) { loadingStore.setLoading(false); return Promise.reject(error); }); app.use(router); app.mount('#app'); ``` 最后,在需要使用loading的地方,可以通过以下方式来调用全局的loading状态: ```typescript import { defineComponent } from 'vue'; import { loadingStore } from './store/loading'; export default defineComponent({ methods: { fetchData() { loadingStore.setLoading(true); // 发起异步请求 // ... loadingStore.setLoading(false); }, }, }); ``` 以上就是使用Vue3和TypeScript结合Pinia实现全局loading的方法。我们首先在store模块中定义了一个loading状态,并提供了相应的方法来控制loading的显示和隐藏。然后在main.ts中创建了全局的loading插件,并通过axios的拦截器来控制loading的显示和隐藏。最后,在需要使用loading的地方调用相应的方法即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

梦想家加一

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值