vuex+vue-router拦截
干就完了
项目中经常遇到这样一个场景,用户信息或者进行增删改的一些模块,需要根据用户是否登录,进行路由拦截,直接上代码
在store文件夹下的store.js中存放一个默认登录状态
/*
* store.js
* */
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = {
user: false,
todos: []
}
const mutations = {
//登录
login(state,user){
state.user = user
},
//退出
logout(state,user){
state.user = false
}
}
export default new Vuex.Store({
state,
mutations
})
在main.js中进行配置
import Vue from 'vue'
import App from './App'
import VueRouter from 'vue-router'
import store from './store/store'
Vue.use(VueRouter)
Vue.config.productionTip = false
const router = new VueRouter({
mode:'history',
routes:[
{
path:'/',
component:(resolve) => {
require(['./components/Home'],resolve)
}
},{
path:'/login',
name:'login',
component:resolve => require(['./components/Login'],resolve)
},{
path:'/contact',
name:'contact',
// 需要登录才能进入的页面可以增加一个meta属性
meta:{
requireAuth: true
},
component:resolve => require(['./components/Contact'],resolve)
},{
path:'*',
component:resolve => require(['./components/Error'],resolve)
}
]
})
router.beforeEach((to, from, next) => {
// 判断是否需要登录权限
if(to.matched.some(res => res.meta.requireAuth)){
if(!store.state.user && to.matched.some(function (item) { return item.path == '/contact' })){
next('/login')
}else{
next()
}
}else{
next()
}
})
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
在Login组件中进行登录和登出
<template>
<div id="login">
<button @click="loginIn">登录</button>
<button @click="loginOut">登出</button>
</div>
</template>
<script>
import store from '../store/store'
export default({
name:'login',
store,
methods:{
loginIn(){
store.commit('login',true);
},
loginOut(){
store.commit('logout',false);
}
}
})
</script>
ok,完美
vuex+vue-router拦截的更多相关文章
- vue router拦截器的简单使用
之前,为了实现router跳转的每个页面的url上都带上addressCode,然后用了一下router拦截器,很好用,当然也可以专门封装一个方法来实现(跳转的页面上带有addressCode),不过 ...
- Vue router拦截 如果用户并未登录直接跳转到登录界面(最简单的cookie演示)
router.beforeEach(function(to,from,next){ console.log('路由拦截') console.log(to.name) console.log(from. ...
- python 全栈开发,Day91(Vue实例的生命周期,组件间通信之中央事件总线bus,Vue Router,vue-cli 工具)
昨日内容回顾 0. 组件注意事项!!! data属性必须是一个函数! 1. 注册全局组件 Vue.component('组件名',{ template: `` }) var app = new Vue ...
- Vue 2.0 + Vue Router + Vuex
用 Vue.js 2.x 与相配套的 Vue Router.Vuex 搭建了一个最基本的后台管理系统的骨架. 当然先要安装 node.js(包括了 npm).vue-cli 项目结构如图所示: ass ...
- 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI
一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...
- Vue3: 如何以 Vite 创建,以 Vue Router, Vuex, Ant Design 开始应用
本文代码: https://github.com/ikuokuo/start-vue3 在线演示: https://ikuokuo.github.io/start-vue3/ Vite 创建 Vue ...
- vue router.beforeEach(),详解
outer.beforeEach()一般用来做一些进入页面的限制. 比如没有登录, 就不能进入某些页面,只有登录了之后才有权限查看某些页面...说白了就是路由拦截.第一步 规定进入路由需不需要权限 @ ...
- vue 路由拦截、axios请求拦截
路由拦截 项目中,有些页面需要登录后才能进入,例如,在某页面A,用户在操作前需要先进入登录页(此时需要将上一页的地址(/survey/start)作为query存入login页面的地址中,如: htt ...
- 【mock】后端不来过夜半,闲敲mock落灯花 (mockjs+Vuex+Vue实战)
mock的由来[假] 赵师秀:南宋时期的一位前端工程师 诗词背景:在一个梅雨纷纷的夜晚,正处于项目编码阶段,书童却带来消息:写后端的李秀才在几个时辰前就赶往临安度假去了,!此时手头仅有一个简单 ...
- Vue Router路由守卫妙用:异步获取数据成功后再进行路由跳转并传递数据,失败则不进行跳转
问题引入 试想这样一个业务场景: 在用户输入数据,点击提交按钮后,这时发起了ajax请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...
随机推荐
- HTML <frameset>不同frame之间传值
布局 左右30%--70%,点击左边的复选框,右边显示相应的反应. 代码 main2.html <html> <frameset cols="30%, 70%"& ...
- FZU 2122——又见LKity——————【字符串匹配、暴力】
Problem 2122 又见LKity Accept: 407 Submit: 1413Time Limit: 1000 mSec Memory Limit : 32768 KB Pr ...
- Dubbo与Zookeeper、Spring整合使用 maven+springmvc+dubbo+zookeeper
为什么要用dubbo? 还是让官方来解释吧: http://dubbo.io/User+Guide-zh.htm http://dubbo.io/ 一般 nginx+tomcat | - ...
- 基于Ajax与用户认证系统的登录验证
一.登录页面 from django.contrib import admin from django.urls import path from blog import views urlpatte ...
- Android存储扩展学习-----应用的清除数据和清除缓存
前几天和朋友聊到了APP清除数据这块,聊到了清除数据都会清掉哪些数据,我们每个人的手机在”设置–>应用管理”里面,选择任意一个App,都会看到两个按钮,一个是清除缓存,另一个是清除数据,那么当我 ...
- 【MATLAB】十进制字节矩阵与比特流矩阵的互相转化
for i=1:length(enc_out_data) data_bits_temp=dec2bin(enc_out_data(i),8); databits((i-1)*8+1:i*8)=doub ...
- Jcrontab定时任务
两篇博客: http://blog.csdn.net/jijijiujiu123/article/details/9086847 网站同事写的(chenrui) ...
- Flask入门 flask结构 url_for 重定向(一)
Flask入门(一) 1 安装虚拟环境Mac,linux sudo pip install virtualenv ubuntu系统 sudo apt-get install python-virt ...
- ZT 困难是什么?困
困难是什么?困难就是摆在我们面前的山峰,需要我们去翻越;困难就是摆阻碍我们前行的巨浪,需要我们扬帆劈刀斩浪航行:困难就是我们眼前所下的暴风雨,要坚信暴风雨过后会有阳光和彩虹. 其实困难并不可怕,怕的就 ...
- swift语言的特点(相对于oc)
1.泛型.泛型约束与扩展: 2.函数式编程: 3.值类型.引用类型: 4.枚举.关联值.元组等其他 上述为swift最大的特点 Another safety feature is that by de ...