vue.js - 如何从组件创建 Vue.prototype?
问题描述
请帮助,如何从组件创建 Vue.prototype,
当调用 this.$authen.check() 响应 this.$store ... 未定义。
我如何使用 Vue.extend 附加 vue global
谢谢你。
对话.vue
<template>
</template>
<script>
export default {
name: 'AuthenDialog',
data: () => ({
state: 0,
}),
methods: {
check() {
console.log(this.$store) // is undefined
},
}
// when i use this it work!
/*beforeCreate() {
Vue.authen = Vue.prototype.$authen = this
}*/
}
</script>
@/plugins/AuthenDialog.js
import Vue from 'vue'
import AuthenDialog from './Dialog'
AuthenDialog.install = (Vue, options) => {
Vue.component(AuthenDialog.name, AuthenDialog)
Vue.authen = Vue.prototype.$authen = new Vue.extend(AuthenDialog)
}
Vue.use(AuthenDialog)
解决方案
尝试在 created 钩子上定义一个数据属性作为存储:
{
data: () => ({
store: null,
}),
methods: {
check() {
console.log(store)
},
},
created() {
this.state = this.$store
}
}
并像这样安装插件
AuthenDialog.install = (Vue, options) => {
Object.defineProperty(Vue.prototype, '$authen', new Vue.extend(AuthenDialog))
}
推荐阅读
- python - 如何使用正则表达式查询传递参数
- macos - 如何识别触控板上的两个手指平移?
- php - 跳过特定订阅产品的 WooCommerce 购物车
- c# - 如何在 nuget 中包含 dll 作为参考,当 nuget 添加到项目时,它也会被复制为参考
- c - 将数字从 1 乘到 N 的 C 程序
- git - 如何解决git不推送所有文件
- scala - 如何使用 transformWith() 忽略失败的期货?
- python - 在窗口的下部绘制 PyQt5
- javascript - 使用 docx 节点库添加更多图像以生成 word 文档时出现内存空间问题
- python - 如何查看一个值是否在熊猫的 1 列中有 2 个匹配项