In the production environment, the $store in CTX under getcurrentinstance cannot be obtained, or main.js As defined in app.config.globalProperties value
Version
3.0.4
Steps to reproduce
code: import { reactive, toRefs, ref, getCurrentInstance } from 'vue' const { ctx } = getCurrentInstance() console.log(ctx.$store)
打包之后上面会输出未定义 After packaging, the above output is undefined
What is expected?
Should be able to print the defined values normally
What is actually happening?
Output undefined
You should do it like this:
const { proxy } = getCurrentInstance()
console.log(proxy.$store)
It is not recommended to use const {ctx} = getCurrentInstance()
because the production environment and development environment get ctx
inconsistent.
Thank you. I think the official document should provide a description of this. Or explain the API that getcurrentinstance can use. Avoid low-level problems
您应该这样做:
const { proxy } = getCurrentInstance () 控制台。日志(代理。$ store )
不建议使用,
const {ctx} = getCurrentInstance()
因为生产环境和开发环境ctx
不一致。
Thank you. I think the official document should provide a description of this. Or explain the API that getcurrentinstance can use. Avoid low-level problems
Because this approach is not recommended, it is not explained in the documentation. The recommended approach is:https://v3.vuejs.org/api/application-config.html#globalproperties
app.config.globalProperties.foo = 'bar'
app.component('child-component', {
mounted() {
console.log(this.foo) // 'bar'
}
})