javascript – Vue:无法检索cookie并传入请求

  methods: {
getCookie (name) {  /// Retrieve cookie function
  match = document.cookie.match(new RegExp(name + '=([^;]+)'));
  if (match) return match[1];
  return
},
handleLoginFormSubmit () {
  const postData = {
    username: this.login.username,
    password: this.login.password
  }
  const servicegroupid = getCookie("servicegroupid") ///Call cookie function assign to var
  const authUser = {}

  this.$http.post(loginUrl + "/" + servicegroupid, postData, {emulateJSON: true}) /// Pass variable into request
    .then(response => {
      if (response.status === 200) {
        console.log('session', response)
        authUser.auth = response.data.auth
        authUser.sgid = response.data.sgid
        authUser.user = this.login.username
        window.localStorage.setItem('authUser', JSON.stringify(authUser))
        this.$store.dispatch('setUserObject', authUser)
        this.$router.push({name: 'dashboard'})
      }
    })
  }
}

尝试检索cookie,将其分配给变量,并将其传递给请求.得到很多未定义的.

Vuex,资源和路由器都运行良好.

得到错误:

getCookie is not defined

如果我尝试从方法块中拉出函数,我得到了

match is not defined

任何帮助是极大的赞赏.

最佳答案 更改

const servicegroupid = getCookie("servicegroupid") 

const servicegroupid = this.getCookie("servicegroupid")

这是组件的一种方法.

点赞