What to do if vue3 doesn't have this
In vue3, there is no this in the new combination API, so if we need to use itthis
what to do?
Solution
getCurrentInstance method gets the instance of the current component and then passes thectx
orproxy
Attributes get the current context so we can use router and vuex in setup
import { getCurrentInstance } from "vue"; export default { setup() { let { proxy } = getCurrentInstance(); proxy.$axios(...) proxy.$router(...) } }
but
However, it is not recommended to use it. If you want to use router and vuex, it is recommended to use it like this:
import { computed } from 'vue' import { useStore } from 'vuex' import { useRoute, useRouter } from 'vue-router' export default { setup () { const store = useStore() const route = useRoute() const router = useRouter() return { // Access state in the computed function count: computed(() => ), // Access getter in the computed function double: computed(() => ) // Use mutation increment: () => ('increment'), // Use action asyncIncrement: () => ('asyncIncrement') } } }
Don’t rely on the getCurrentInstance method to obtain component instances to complete some main functions, otherwise an error will be reported after the project is packaged.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.