vue3+ts中如何引入全局工具类

文章介绍了在Vue3项目中如何封装工具类并全局引用。首先在utils2目录下的common.ts文件封装工具方法,然后在main.ts中导入并挂载到vue的原型上。接着,通过组合式API的getCurrentInstance获取组件实例,从而在任何需要的地方使用$untils调用工具类的方法。
摘要由CSDN通过智能技术生成

步骤一:封装好要使用的工具类,记得要通过export default导出工具类中定义的方法,可以新建一个文件夹utils2,在该文件夹下创建ts文件 如:common.ts , 在common.ts 里面封装一个要使用工具。路径为src/utils2/common.ts

步骤二:在main.ts 中导入工具类

import untils from '@/untils2/common'

步骤三:在 main.ts 中将工具类挂载到vue的原型上

const setupAll = async () => {
  const app = createApp(App)
  app.config.globalProperties.$untils = untils
}

步骤四:在需要使用工具类的地方 进行引入,引入步骤如下

// Vue3组合式API写法中的 getCurrentInstance 函数可以创造一个vue实例
import { getCurrentInstance } from 'vue'

//这里从getCurrentInstance()获取到的组件实例中解构出proxy ,proxy可以访问到组件实例的属性或方法
const { proxy } = getCurrentInstance()

/**最后再从proxy中解构出$untils,此处能够结构出$untils,是因为我们在进行步骤二、三的时已经在main.ts中全局引入了$untils
*/
const { $untils } = proxy

步骤五:接下来就是使用$untils,通过 $untils.xxx 调用已经封装好多方法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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的地方调用相应的方法即可。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值