简体   繁体   English

无法从js访问Vue

[英]Can't access Vue from js

I'm trying to create an App with Vue, and Vue-ressource Actually i need to use ressource to made auth system by an API call. 我正在尝试使用Vue创建一个App,而Vue-ressource实际上我需要使用ressource通过API调用来创建auth系统。 But in my Auth.js (that i import into my login.vue) console said he can't read $http of undefined. 但在我的Auth.js(我导入我的login.vue)控制台说他无法读取未定义的$ http。 So apparantly i can't reach 'this' (so vue). 所以显然我无法达到'这个'(所以vue)。 Did i missed something ? 我错过了什么吗? Or its just a bad use ? 或者它只是一个坏用途?

Thank you all 谢谢你们

actually my main.js : 实际上是我的main.js:

import Vue from 'vue'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
Vue.use(VueRouter)
Vue.use(VueResource)

import App from './components/App.vue'

import Login from './components/Login.vue'
import Home from './components/Containers.vue'


function requireAuth (to, from, next) {
  if (!auth.loggedIn()) {
    next({
      path: '/',
      query: { redirect: to.fullPath }
    })
  } else {
    next()
  }
}

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/home', name: 'home', component: Home, beforeEnter: requireAuth },
    { path: '/', component: Login },
    { path: '/logout',
      beforeEnter (to, from, next) {
        auth.logout()
        next('/')
      }}
  ]
})

new Vue({
  el: '#app',
  router,
  render: h => h(App)
})

the login.vue login.vue

import auth from '../utils/auth'

export default {
  data () {
    return {
      email: '',
      pass: '',
      error: false
    }
  },
  methods: {
    login () {
      auth.login(this.email, this.pass, loggedIn => {
        if (!loggedIn) {
          this.error = true
        } else {
          console.log('good')
          this.$router.replace('/home')
        }
      })
    }
  }
}

and my auth.js where the vue-ressource post is made : 和我的auth.js所在的vue-ressource帖子:

export default {
  login (email, pass, cb) {
    cb = arguments[arguments.length - 1]
    if (localStorage.token) {
      if (cb) cb(true)
      this.onChange(true)
      return
    }
    pretendRequest(email, pass, (res) => {
      if (res.authenticated) {
        localStorage.token = res.token
        if (cb) cb(true)
        this.onChange(true)
      } else {
        if (cb) cb(false)
        this.onChange(false)
      }
    })
  },

  getToken () {
    return localStorage.token
  },

  logout (cb) {
    delete localStorage.token
    if (cb) cb()
    this.onChange(false)
  },

  loggedIn () {
    return !!localStorage.token
  },

  onChange () {}

}

function pretendRequest (email, pass, cb) {
  setTimeout(() => {
    this.$http.post('localhost:9000/api/login', {email: email, password: pass}).then(response => {
      if (response.status === 200) {
        cb({
          authenticated: true,
          token: Math.random().toString(36).substring(7)
        })
      } else {
        cb({ authenticated: false })
      }
    }, response => {
      console.log('error ' + response.status)
    })
  }, 0)
}
  1. Replace vue-resource with axios. 用axios替换vue-resource。 Easy to do. 容易做到。 Vue-resource is not longer maintained by Vue team, so it's bad choice to use it.( https://medium.com/the-vue-point/retiring-vue-resource-871a82880af4#.dwmy5ymjx ) Vue团队不再维护Vue资源,因此使用它是不好的选择。( https://medium.com/the-vue-point/retiring-vue-resource-871a82880af4#.dwmy5ymjx

    Axios is widely supported. Axios受到广泛支持。 https://github.com/mzabriskie/axios . https://github.com/mzabriskie/axios

    Nice laracasts about using axios with vue. 关于使用axios与vue的好拉拉斯特。 You will quickly get it. 你会很快得到它。 https://laracasts.com/series/learn-vue-2-step-by-step/episodes/18 https://laracasts.com/series/learn-vue-2-step-by-step/episodes/18

  2. It is normal that you can't access Vue instance in your auth module. 您无法访问auth模块中的Vue实例是正常的。 Try to learn more about using this and you will quickly get it 'why?' 尝试了解有关使用this更多信息,您很快就会明白它为什么?

  3. To make ajax requests in your auth module, just import axios and use axios.post / axios.get 要在auth模块中发出ajax请求,只需导入axios并使用axios.post / axios.get

Any questions? 任何问题? Comment and I will explain more. 评论,我会解释更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM