axios

vue+axios 前端实现登录拦截(路由拦截、http拦截)

╄→гoц情女王★ 提交于 2020-12-20 02:37:34
一、路由拦截 登录拦截逻辑 第一步:路由拦截 首先在定义路由的时候就需要多添加一个自定义字段 requireAuth ,用于判断该路由的访问是否需要登录。如果用户已经登录,则顺利进入路由, 否则就进入登录页面。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 const routes = [ { path: '/' , name: '/' , component: Index }, { path: '/repository' , name: 'repository' , meta: { requireAuth: true , // 添加该字段,表示进入这个路由是需要登录的 }, component: Repository }, { path: '/login' , name: 'login' , component: Login } ]; 定义完路由后,我们主要是利用 vue-router 提供的钩子函数 beforeEach() 对路由进行判断。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 router.beforeEach((to, from , next) => { if (to.meta.requireAuth) { // 判断该路由是否需要登录权限 if (store.state

vue+axios 前端实现登录拦截(路由拦截、http拦截)

白昼怎懂夜的黑 提交于 2020-12-20 02:32:26
一、路由拦截 登录拦截逻辑 第一步:路由拦截 首先在定义路由的时候就需要多添加一个自定义字段 requireAuth ,用于判断该路由的访问是否需要登录。如果用户已经登录,则顺利进入路由, 否则就进入登录页面。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 const routes = [ { path: '/' , name: '/' , component: Index }, { path: '/repository' , name: 'repository' , meta: { requireAuth: true , // 添加该字段,表示进入这个路由是需要登录的 }, component: Repository }, { path: '/login' , name: 'login' , component: Login } ]; 定义完路由后,我们主要是利用 vue-router 提供的钩子函数 beforeEach() 对路由进行判断。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 router.beforeEach((to, from , next) => { if (to.meta.requireAuth) { // 判断该路由是否需要登录权限 if (store.state

使用Typescript重构axios(二十九)——添加baseURL

一个人想着一个人 提交于 2020-12-19 03:34:45
0. 系列文章 1.使用Typescript重构axios(一)——写在最前面 2.使用Typescript重构axios(二)——项目起手,跑通流程 3.使用Typescript重构axios(三)——实现基础功能:处理get请求url参数 4.使用Typescript重构axios(四)——实现基础功能:处理post请求参数 5.使用Typescript重构axios(五)——实现基础功能:处理请求的header 6.使用Typescript重构axios(六)——实现基础功能:获取响应数据 7.使用Typescript重构axios(七)——实现基础功能:处理响应header 8.使用Typescript重构axios(八)——实现基础功能:处理响应data 9.使用Typescript重构axios(九)——异常处理:基础版 10.使用Typescript重构axios(十)——异常处理:增强版 11.使用Typescript重构axios(十一)——接口扩展 12.使用Typescript重构axios(十二)——增加参数 13.使用Typescript重构axios(十三)——让响应数据支持泛型 14.使用Typescript重构axios(十四)——实现拦截器 15.使用Typescript重构axios(十五)——默认配置 16

vue-router 导航守卫

回眸只為那壹抹淺笑 提交于 2020-12-18 09:30:10
<! DOCTYPE html > < html lang ="en" > < head > < meta charset ="UTF-8" > < title ></ title > </ head > < body > < div id ="app" ></ div > < script type ="text/javascript" src ="../vue.min.js" ></ script > < script type ="text/javascript" src ="../js/node_modules/vue-router/dist/vue-router.min.js" ></ script > < script src ="../node_modules/axios/dist/axios.js" ></ script > < script type ="text/javascript" > // 导航完成后获取数据,这让我们有机会在数据获取期间展示一个 loading 状态,还可以在不同视图间展示不同的 loading 状态。 var Index = { template: ` < div > 我是首页 < / div> ` }; var Post = { data() { return { loading: false , error: null , post

axios

て烟熏妆下的殇ゞ 提交于 2020-12-16 11:29:07
使用axios中碰到的问题, 比如怎么写header,怎么添加authentication, config中放什么内容,https request会报error unable to verify the first certificate…… 参考了 https://www.npmjs.com/package/axios 有基本使用方法,这也是一种学习方法,用到某个nodejs package,就到npm上面搜索,都有guideline。 https://www.jianshu.com/p/e23918190912 有更详细的使用教程。 https request会报error unable to verify the first certificate, 解决方法如下:加一个httpsAgent const ax = axios.create({ baseURL:xxxxxxxxx, httpsAgent: new https.Agent({ keepAlive: true,rejectUnauthorized: false }) }); headers 需要config多个fields,下面两种写法都可以 ax.defaults.headers.common={}; //必须先置空,因为是全局默认配置,如果有多个axios request,配置值会混到一起 ax

axios解决跨域问题

有些话、适合烂在心里 提交于 2020-12-16 08:47:31
axios解决跨域问题 参考文章: (1)axios解决跨域问题 (2)https://www.cnblogs.com/zhangycun/p/10944071.html 备忘一下。 来源: oschina 链接: https://my.oschina.net/u/4437884/blog/4667991

Unauthorise axios put request without data

杀马特。学长 韩版系。学妹 提交于 2020-12-15 06:49:30
问题 I want to send a axios put request into my backend but unauthorise error keeps coming out Error: PUT http://localhost:5000/api/items/unlike/5ef9e8176b04a67fe823bf5e/S 401 (Unauthorized) On my put request, I attach my variable (clothes id and size) into the request url so i dont have request body. Here is my code. Frontend: toCart = () => { let self = this; let config = { headers: { "Content-Type": "application/json", "x-auth-token": this.state.token, }, }; // config = JSON.stringify(config);

How to add a property to ALL objects in an array (data) using Vue.js?

你。 提交于 2020-12-15 05:21:32
问题 Background : I have an array in data() that gets populated with objects from the backend. If the GET request retrieves 6 objects those 6 objects will update in the array. Problem : I already understand vm.$set is needed to add properties to an object. But how do add properties to all object in the array? I want to change: data() { return { expenseButton: [{key:value},{key:value},{key:value}] }; } to data() { return { expenseButton: [{key:value, key2:value2},{key:value, key2:value2},{key:value

Vue Login with Axios Request HTTP

╄→гoц情女王★ 提交于 2020-12-15 04:43:34
问题 Im new at Vue and im trying to make a Request HTTP to my backend, When i inspect in my browser, i get the access token from /login but in the api/users i get "Token is Invalid". How do i get my api/users data? import axios from "axios"; export default { name: "login", async created() { const response = await axios.get("api/users", { headers: { Authorization: "Bearer " + localStorage.getItem("token") } }); console.log(response); }, data() { return { showError: false, email: "", password: "", }

Vue Login with Axios Request HTTP

好久不见. 提交于 2020-12-15 04:43:26
问题 Im new at Vue and im trying to make a Request HTTP to my backend, When i inspect in my browser, i get the access token from /login but in the api/users i get "Token is Invalid". How do i get my api/users data? import axios from "axios"; export default { name: "login", async created() { const response = await axios.get("api/users", { headers: { Authorization: "Bearer " + localStorage.getItem("token") } }); console.log(response); }, data() { return { showError: false, email: "", password: "", }