【发布时间】:2020-01-13 11:41:08
【问题描述】:
我为我的 Vue2.js 项目创建了一个简单的插件,以获取表单中我的选择字段的一些列表。该插件应该进行一些 axios 调用并返回响应。
const ListSelector = {
install (Vue){
Vue.mixin({
methods: {
getSubscriberType: function() {
this.$http
.get('/web/admin/contract/subscribers')
.then(function(response) { return response.data })
},
//other methods (they do not work the same...)...
}
})
}
}
export default ListSelector
我在 main.js 中注册了插件
import ListSelector from './backend/selectable'
Vue.use(ListSelector)
现在,如果我尝试在组件中调用插件方法,我会发现它是未定义的
<template>
...
<b-form-select v-model="form.type" :options="options" id="type" required></b-form-select>
...
</template>
<script>
export default {
data(){
return {
options: {}
}
},
mounted(){
this.options = this.getSubscriberType()
}
}
<script>
我知道this.getSubscriberType() 未定义
编辑:我实际上看到该函数已被触发(我在其中添加了一个警报...但如果我在组件中执行console.log(this.getSubscriberType()),我会得到未定义...
【问题讨论】:
-
console.log(this.getSubscriberType)没有括号会发生什么 -
我得到类似函数 [{native code}]
标签: javascript vuejs2