plugins - Vuejs,插件,找不到全局方法/属性

标签 plugins vue.js

我在这里做错了什么?我安装了一个插件,并设置了一个全局属性,但是在我的方法中我无法访问该属性。

我的根 Vue 组件的 main.js

import Vue from 'vue'
...
Vue.use({
  install (Vue) {
    let googleAuth = null
    console.log('called!')
    Vue.auth = {
      setAuth (auth) {
        googleAuth = auth
      },
      isAuthenticated () {
        return googleAuth !== null && googleAuth.isSignedIn.get()
      },
      currentUser () {
        return googleAuth.currentUser.get()
      },
      currentUserProfile () {
        return this.currentUser().getBasicProfile()
      },
      getIdToken () {
        return this.currentUser().getAuthResponse().id_token
      }
    }
  }
})

来自根 Vue 组件的 App.vue

<template></template>
<script>
  export default {
    name: 'Trudit',
    data () {
      return {
        title: 'Trudit'
      }
    },
    methods: {
      onSignInSuccess (googleUser) {
        console.log(this)
        this.auth.setAuth(googleUser)   //<-  this.auth === undefined
      }
    }
  }
</script>

错误

Uncaught TypeError: Cannot read property 'setAuth' of undefined at VueComponent.onSignInSuccess (eval at 57 (0.83049e6….hot-update.js:13), :17:16) at boundFn (eval at (app.js:630), :126:14) at H_. (cb=gapi.loaded_0:285) at cb=gapi.loaded_0:175 at h.r2 (cb=gapi.loaded_0:78) at xs (cb=gapi.loaded_0:81) at Wq (cb=gapi.loaded_0:81) at _.C.uea (cb=gapi.loaded_0:80) at Ap (cb=gapi.loaded_0:74)

最佳答案

正如@Srinivas Damam 所指出的,我需要使用原型(prototype)来设置它。

Vue.prototype.auth 修复了它

关于plugins - Vuejs,插件,找不到全局方法/属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43156014/

相关文章:

javascript - Vue.js 图像 v-for 绑定(bind)

plugins - 如何为我的 OCaml 程序创建 GTK 插件 (cmxs)

maven - 如果指定了 "-DskipTests"或 "-Dmaven.test.skip=true",如何跳过 Maven 插件执行?

plugins - 如何使用模块而不是插件插入播放生命周期?

vim - 在 VIM 中运行 conque 时不断弹出警告

vue.js - 如何从 NuxtJS 中的 fetch 或 asyncData 钩子(Hook)访问 vuex 存储?

javascript - 将数据属性的副本传递给 Vue JS 中的函数

javascript - 如何在 Vuetify 组件的内联 CSS 中使用来自 Vue 组件的数据

wordpress - 如何导出整个 Wordpress 网站?

javascript - 在 .vue 组件内嵌套内联模板