H5W3
当前位置:H5W3 > 其他技术问题 > 正文

Vue中定义全局$api数据接口时报错

main.js中引入:

import api from './api'
Vue.use(api);

api目录中的index.js文件:

import apiList from './api'

const install = Vue => {
  if (install.installed) return
  install.installed = true

  Object.defineProperties(Vue.prototype, {
    $api: {
      get () {
        console.log('$api')
        return apiList
      }
    }
  })
}

export default {
  install
}

提示的错误:

Unhandled promise rejection TypeError: Cannot read property '$api' of undefined
    at user.actions.GetUserInfo (eval at <anonymous> (http://localhost:9528/app.js:2224:1), <anonymous>:66:15)
    at new Promise (eval at <anonymous> (http://localhost:9528/app.js:2510:1), <anonymous>:177:7)
    at GetUserInfo (eval at <anonymous> (http://localhost:9528/app.js:2224:1), <anonymous>:64:14)
    at Array.wrappedActionHandler (eval at <anonymous> (http://localhost:9528/app.js:1058:1), <anonymous>:603:15)
    at Store.dispatch (eval at <anonymous> (http://localhost:9528/app.js:1058:1), <anonymous>:346:15)
    at Store.boundDispatch [as dispatch] (eval at <anonymous> (http://localhost:9528/app.js:1058:1), <anonymous>:271:21)
    at eval (eval at <anonymous> (http://localhost:9528/app.js:1735:1), <anonymous>:86:65)
    at History.confirmTransition.iterator (eval at <anonymous> (http://localhost:9528/app.js:3680:1), <anonymous>:1735:7)
    at step (eval at <anonymous> (http://localhost:9528/app.js:3680:1), <anonymous>:1618:9)
    at runQueue (eval at <anonymous> (http://localhost:9528/app.js:3680:1), <anonymous>:1626:3)

回答:

目录结构发来看一下,文件引用是不是对了

回答:

谢谢各位,问题找到了,
是我把this.$api用在了module中,this对象指向不对的问题。

本文地址:H5W3 » Vue中定义全局$api数据接口时报错

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址