Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About @vue/compat, axios is introduced globally, axios.post cannot be accessed properly #4872

Closed
lqfxz520 opened this issue Oct 28, 2021 · 6 comments

Comments

@lqfxz520
Copy link

Version

3.2.20

Reproduction link

github.com

Steps to reproduce

npm run serve

What is expected?

In the App.vue file,log this.$axios.post is undefined

What is actually happening?

In the App.vue file,log this.$axios.post is valid

@posva
Copy link
Member

posva commented Oct 28, 2021

If you check axios after importing it, you will see it's a function and that it doesn't have a post property. Vue is properly exposing the same thing.


Remember to use the forum or the Discord chat to ask questions!

@posva posva closed this as completed Oct 28, 2021
@lqfxz520
Copy link
Author

lqfxz520 commented Oct 28, 2021

@posva When I comment out '@vue/compat', this.$axios has a post property,What accounts for this result?

import { defineConfig } from "vite";
import createVuePlugin from "@vitejs/plugin-vue";

export default defineConfig({
  plugins: [
    createVuePlugin({
      template: {
        compilerOptions: {
          compatConfig: {
            MODE: 2,
          },
        },
      },
    }),
  ],
  resolve: {
    alias: {
      // vue: "@vue/compat",
    },
  },
  server: {
    port: 8081,
  },
});

@LinusBorg
Copy link
Member

Duplicate of #4403

@LinusBorg LinusBorg marked this as a duplicate of #4403 Oct 28, 2021
@LinusBorg
Copy link
Member

A short explanation: In Vue 2, we added global properties by extending the prototype. To mimic this with globalProperties, we bind functions provided as global properties to the component proxy instance. but calling fn.bind() creates a new function that is missing any additional properties that were defined on the original.

@LinusBorg
Copy link
Member

Workaround/hack:

axios.bind = () => axios
app.config.globalProperties.$axios = axios

@lqfxz520
Copy link
Author

A short explanation: In Vue 2, we added global properties by extending the prototype. To mimic this with globalProperties, we bind functions provided as global properties to the component proxy instance. but calling fn.bind() creates a new function that is missing any additional properties that were defined on the original.

Thank you for your help, I have been puzzled by this problem for a long time.Now it's time to happily continue playing

@github-actions github-actions bot locked and limited conversation to collaborators Oct 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants