解决方法:
vue_project\src\router\index.js 路由主入口文件当中添加如下代码
const originalPush = VueRouter.prototype.push
//解决重复提交相同链接报错
VueRouter.prototype.push = function push(location, onResolve, onReject) {
if (onResolve || onReject)
return originalPush.call(this, location, onResolve, onReject)
return originalPush.call(this, location).catch((err) => {
if (VueRouter.isNavigationFailure(err)) {
// resolve err
return err
}
// rethrow error
return Promise.reject(err)
})
}
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location, onResolve, onReject) {
if (onResolve || onReject){
//回调函数里面会用到this的指向,所以就要使用call
return originalReplace.call(this, location, onResolve, onReject)
}
return originalReplace.call(this, location).catch((err) => {
if (VueRouter.isNavigationFailure(err)) {
//如果为相同链接引发的错误,返回错误原因,promise状态为resolve
// resolve err
return err
}
// rethrow error
return Promise.reject(err)
})
}
由于视频的是react,所以配置代理花了一点时间
在vue.config.js当中,主要是proxy的配置
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
lintOnSave:false,
transpileDependencies: true,
devServer:{
proxy:{
"/dev":{
//转发
target:"http://localhost:5000",
changeOrigin:true,
//重写,删除/dev前缀
pathRewrite:{
"^/dev":"",
}
}
}
}
})
注意:这样子写的话所有api请求都需要戴上/dev前缀了,所以一般都是对axios进行二次封装,下面是我的二次封装
import axios from "axios";
import nprogress from "nprogress"
import "nprogress/nprogress.css"
//创建一个axios的实例化对象
//传入配置对象
const service = axios.create({
//每一个ajax请求都添加/dev前缀
//比如http://localhost:8080/dev/login
//http://localhost:8080/dev/home
baseURL:"/dev",
//请求超时时间
timeout: 2000
});
//请求拦截器
service.interceptors.request.use((config) => {
//进度显示(当然,nprogress是假进度条)
nprogress.start();
return config;
})
//响应拦截器
service.interceptors.response.use((response) => {
nprogress.done();
return response.data || response;
}, (error) => {
nprogress.done();
console.log("未知错误!");
return new Promise(() => {});
})
export default service;
开发流程介绍
应用登记
登记地址:https://github.com/settings/applications/new

查看地址:https://github.com/settings/developers


4.1 前台项目准备好个人中心组件,供授权成功后查看
4.2 将得到的 client_id 、clinet_secret配置到服务器config\index.js中,随后重启服务器。
// github oauth
const CLIENT_ID = "xxxxxxxxxxxxxxx";
const CLIENT_SECRET = "xxxxxxxxxxxxxxx";

4.3 配置前端请求地址,这里就不写了
4.4 项目中携带网站标识跳转到授权页
loginGithub = ()=>{
window.location.href = AUTH_BASE_URL+'?client_id='+CLIENT_ID
}


主页登录

获取验证码

个人中心

github关联

关联后
