uses the login permission judgment in vue-element-admin, which is basically introduced and loaded according to it, but it still prompts you to report an error
main.js
import Vue from "vue"
import App from "./App"
import router from "./router"
import store from "./store"
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: "-sharpapp",
router,
store,
components: { App },
template: "<App/>"
})
login.vue
export default {
data () {
return {
userName: "",
passWord: ""
}
},
methods: {
verify () {
if (this.userName === "") {
mui.toast("", { duration: "long", type: "div" })
return false
}
if (this.passWord === "") {
mui.toast("", { duration: "long", type: "div" })
return false
}
return true
},
login () {
let result = this.verify()
if (result) {
let data = {
username: this.userName,
password: this.passWord
}
this.$store.dispatch("LoginByUsername", data).then(() => {
this.$router.push({ path: "/" })
// mui.toast("", { duration: "long", type: "div" })
}).catch(() => {
// mui.toast(res.msg, { duration: "long", type: "div" })
})
} else {
console.log("error submit!!")
return false
}
}
}
}
login.js
import request from "@/utils/request"
export function loginByUsername (username, password) {
const data = {
username,
password
}
return request({
url: "/User/login",
method: "post",
data
})
}
user.js
import { loginByUsername, logout, getUserInfo } from "@/api/login"
import { getToken, setToken, removeToken } from "@/utils/index"
actions: {
//
LoginByUsername ({ commit }, userInfo) {
const username = userInfo.username.trim()
return new Promise((resolve, reject) => {
loginByUsername(username, userInfo.password).then(response => {
const data = response.data
console.log(data)
commit("SET_TOKEN", data.token)
setToken(response.data.token)
resolve()
}).catch(error => {
reject(error)
})
})
}
}
request.js
import axios from "axios"
import mui from "mui"
import store from "@/store"
import { getToken } from "@/utils/index"
// request interceptor
service.interceptors.request.use(config => {
// Do something before request is sent
if (store.getters.token) {
// token-- ["X-Token"]key
config.headers["XX-Token"] = getToken()
}
console.log("token +".store.getters.token)
return config
}, error => {
// Do something with request error
console.log(error) // for debug
Promise.reject(error)
})
then when you request, you always get the wrong Cannot read property "getters" of undefined
.