vue项目中js-cookie的使用存储token操作
时间: 2023-05-25 08:01:02 浏览: 490
在 Vue 项目中使用 js-cookie 库存储 token 可以按照下面的步骤进行操作:
首先,安装 js-cookie 库,可以使用 npm 安装,命令为:
```
npm install js-cookie
```
然后,在需要存储 token 的组件中引入 js-cookie 库:
```javascript
import Cookies from 'js-cookie'
```
接着,可以使用 `Cookies.set()` 方法将 token 存储到 Cookie 中:
```javascript
Cookies.set('token', 'your_token_here')
```
其中,第一个参数 `'token'` 是 Cookie 名称,第二个参数 `'your_token_here'` 是要存储的 token 值。
需要注意的是,如果要设置 token 过期时间,可以在 `Cookies.set()` 方法中传入第三个参数,例如:
```javascript
Cookies.set('token', 'your_token_here', { expires: 7 })
```
上述代码表示将 token 存储到 Cookie 中,并设置过期时间为 7 天。
最后,在需要使用 token 的组件中,可以使用 `Cookies.get()` 方法获取存储在 Cookie 中的 token:
```javascript
const token = Cookies.get('token')
```
这样就可以在 Vue 项目中使用 js-cookie 库进行 token 的存储和获取操作了。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)