我有一个Vue网站,通过SOCKET连接到后端python烧瓶。 问题是: 1.如果我在console.log中重新启动后端,则出现401错误,但仍可以浏览站点以查找已存在的数据。对于新数据,我只会看到加载错误。 2.如果后端完全停止,我会得到net :: ERR_EMPTY_RESPONSE,但仍然可以执行前面的操作。
只有刷新后,系统才意识到我有问题并让我登录...
我尝试了在互联网上看到的不同方法,使用store.getters.Authenticated,在模块中使用this.Authenticated,但是系统认为我还可以。我认为我必须使用拦截器,但是文档对我来说并不明确,我不知道应该在哪里使用。
一些代码: main.js
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app');
router.js
const router = new Router({
routes: [
{
path: '*',
name: 'all',
component: Home
}
],
mode: 'history',
});
router.beforeEach((to, from, next) => {
console.log("check if Auth:", store.getters.Authenticated);
// it will always be auth.... so I didn't' show you the case when not auth
next();
});
export default router
store.js
function socketConnect() {
socket = new VueSocketIO({
debug: true,
connection: location.protocol + '//' + document.domain + ':' + location.port,
vuex: {
store,
actionPrefix: 'SOCKET_',
mutationPrefix: 'SOCKET_'
},
});
getters: {
Authenticated(state) {
return state.isAut
},
}
mutations: {
authUser(state, Data) {
if (Data) {
state.isAut = true;
state.user = Data;
state.socket = socketConnect().io;
} else {
state.isAut = false;
state.user = null;
state.sites = null;
state.map.markers = null;
state.socket.disconnect()
}
},
}
P.S。我没有任何与axios相关的东西,也不知道如何在任何地方插入它...