简体   繁体   English

vuejs3:无法全局导入 axios

[英]vuejs3 : can't import axios globally

I can import and use axios in a component:我可以在组件中导入和使用 axios:

import axios from 'axios'
export default function usePostLogin() {
 const login = async (url , data , callback) => {
    axios.post(url, data)
// etc

I want to import axios globally.我想全局导入 axios。

In main.js :在 main.js 中:

import axios from 'axios'

const app = createApp(App)
app.component('FontAwesome', FontAwesomeIcon)
app.use(createPinia()).use(router).use(axios)
app.mount('#app')

But then I get an error : error 'axios' is not defined no-undef但后来我得到一个错误: error 'axios' is not defined no-undef

You can not add axios with use in Vue3, in main.js add it to the global property:您不能在 Vue3 中use添加 axios,在 main.js 中将其添加到全局属性中:

app.config.globalProperties.axios = axios

and in component use it like:并在组件中使用它:

this.axios

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM