setToken({ commit }, state) { return new Promise((resolve) => { commit('SET_TOKEN', state.token); Cookies.set('JavaInfo', JSON.stringify(state)); setToken(data.token); resolve(); }); },这个函数是递归函数吗
时间: 2024-04-26 14:21:14 浏览: 25
不是,这个函数并不是递归函数。递归函数指的是在函数体内部调用自身的函数,而这个函数并没有调用自身的语句,也没有任何循环语句。
这个函数是一个 Vue.js 中的 action 函数,用于设置用户的 token 值并将其保存到 cookie 中。函数返回一个 Promise 对象,当 action 执行完毕后会将其 resolved。
相关问题
setToken({ commit }, state) { return new Promise((resolve) => { commit('SET_TOKEN', state.token); Cookies.set('JavaInfo', JSON.stringify(state)); setToken(data.token); resolve(); }); },这段代码是什么意思
这段代码是一个 Vue.js 中的 action 函数,用于设置用户的 token 值并将其保存到 cookie 中。具体来说,它接受两个参数:`commit` 和 `state`。`commit` 是一个用于提交 mutation 的方法,`state` 则是当前的状态对象。函数返回一个 Promise 对象,当 action 执行完毕后会将其 resolved。
在函数体内,`commit('SET_TOKEN', state.token)` 会调用名为 `SET_TOKEN` 的 mutation 函数,将 `state.token` 参数传入,用于更新应用的状态。`Cookies.set('JavaInfo', JSON.stringify(state))` 会将 `state` 对象转换为 JSON 字符串并存储到名为 `JavaInfo` 的 cookie 中。最后,`setToken(data.token)` 会将 `data.token` 设置为当前用户的 token 值。
login({ commit }, userInfo) { const { username, password } = userInfo return new Promise((resolve, reject) => { login({ username: username.trim(), password: password }).then(response => { const { data } = response commit('SET_TOKEN', data.token) setToken(data.token) resolve() }).catch(error => { reject(error) }) }) }, 让该请求携带cookie 如何处理
如果要让该请求携带 cookie,我们可以使用 axios 来发送请求,并在请求头中带上 cookie。具体做法如下:
1. 在发送请求前,先从 cookie 中获取 token,并设置到请求头中:
```javascript
import { getCookie } from '@/utils/cookie'
const token = getCookie('token')
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
```
2. 在登录成功后,将服务器返回的 token 存储到 cookie 中:
```javascript
import { setCookie } from '@/utils/cookie'
const { data } = response
setCookie('token', data.token, 7)
```
下面是整个函数的代码示例:
```javascript
import axios from 'axios'
import { setCookie, getCookie } from '@/utils/cookie'
export function login({ commit }, userInfo) {
const { username, password } = userInfo
const config = {
headers: {
'Content-Type': 'application/json'
}
}
// 从 cookie 中获取 token,设置到请求头中
const token = getCookie('token')
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return new Promise((resolve, reject) => {
axios.post('/api/login', {
username: username.trim(),
password: password
}, config).then(response => {
const { data } = response
commit('SET_TOKEN', data.token)
setCookie('token', data.token, 7) // 将 token 存储到 cookie 中
resolve()
}).catch(error => {
reject(error)
})
})
}
```
这样,我们就可以让该请求携带 cookie 了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)