功能:

// 页面跳转后发送ajax请求给后端 请求详细信息

//点击课程推荐跳转到推荐课程详细

// 页面刚加载立即执行函数 = mounted

<template>
<div>
<img :src="detail.img" />
<h1>{{ detail.title }}</h1>
<h3>{{detail.slogon }}</h3>
<h5>{{ detail.level }}</h5>
<p>{{ detail.why }}</p>
<div>
<ul v-for="item in detail.chapter">
<li>{{ item.title }}</li>
</ul>
</div>
<br/>
推荐课程:
<div>
<ul v-for="item in detail.recommends">
<!-- 这个方式有问题 -->
<!-- <li><router-link :to="{name:'index',query:{id:item.id}}">{{ item.title }}</router-link></li> -->
<li @click="changeDetail(item.id)">{{ item.title }}</li>
</ul>
</div>
</div>
</template> <script>
export default {
name: "CourseDetail",
data() {
return {
index: "CourseDetail", detail: {
course: null,
img: null,
level: null,
slogon: null,
title: null,
why: null,
chapter: [],
recommends: []
}
};
},
// created:在模板渲染成html前调用
mounted() { var id = this.$route.query.id;
this.getRouterData(id); },
methods: {
getRouterData(nid) { // 发送ajax请求给后端 请求详细信息
var _this = this; this.$axios
.request({
url: this.$store.state.apiList.coursedetail+nid+'/',
methods: "GET"
})
.then(function(ret) {
// ajax请求发送成功后,获取的响应内容
// ret.data=
if (ret.data.code === 1000) {
_this.detail = ret.data.data;
}
})
.catch(function(ret) {
// ajax请求发送失败后,获取的响应内容
});
},
//点击课程推荐跳转到推荐课程详细
changeDetail(id){
// 我需要重新调用getRouterData 向后端请求数据,不然不然会显示为空
this.getRouterData(id)
this.$router.push({name:'CourseDetail',query:{id:id}}) }
}
};
</script> <style scoped>
</style>

功能(2)

// 登录

//使用$store

// router.beforeEach 全局页面阻拦钩子

// router.beforeEach 局部认证钩子

// cookie 设置

// router.beforeEach 全局页面认证钩子

import Vue from 'vue'
import App from './App'
import router from './router' // 导入store组件
import store from './store/store' // 导入axios用于发送数据给后端
import axios from 'axios'
//在vue的全局变量中设置了 $axios = axios
// 以后每个组件使用时: this.$axios
Vue.prototype.$axios = axios Vue.config.productionTip = false /* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
}) // 这是一个全局路由限制钩子 一般用来做一些进入页面前的限制
router.beforeEach(function(to, from, next){
// to 表示即将要进入的路由对象
//from 表示即将要离开的路由对象
//next 是继续跳转或中断的方法
// 如果路由meta.requireAuth参数 为ture 我需要检查下他的token值有才能让他访问
if (to.meta.requireAuth) {
// // 要去的url只有登录成功后才能访问
if (store.state.token){
// // /我就往下走
next()
}else{
next(
// 页面跳转 传了个值 一个路径 ,to.fullPath = 你当前要点击要跳转的路径
next({name: 'Login',query: {backUrl: to.fullPath}}) )
}
// 如果 路由中 没有定义要 校验限制进入,就直接进入
}else{
next()
}
})

cookie 设置

import Vue from 'vue'
import Vuex from 'Vuex'
import Cookie from 'vue-cookies' Vue.use(Vuex) // 组件中通过 this.$store.state.username 调用
const state = {
// Cookie里面有值的话我就在cookie里面取值,没有我就自动注销
username: Cookie.get('username'),
token: Cookie.get('token'),
apiList: {
course: "http://127.0.0.1:8000/api/v1/course/",
coursedetail:`http://127.0.0.1:8000/api/v1/coursedetail/` ,
auth:'http://127.0.0.1:8000/api/v1/auth/', },
} const mutations = {
// 组件中通过this.$store.commit(参数)调用
//设置kookie
saveToken(state, userToken) { // userToken 传过来的是一个对象,我可以通过点取值
state.username = userToken.username;
state.token = userToken.token;
Cookie.set('username', userToken.username, "20min")
Cookie.set('token', userToken.token, "20min")
},
// 清除 cookie
clearToken: function (state) {
state.username = null
state.token = null
Cookie.remove('username')
Cookie.remove('token')
}
} export default new Vuex.Store({
state,
mutations, })

功能(3)

// 登录 (登录后我就直接 跳转到他之前点击的页面)

<template>
<div>
<h1>登录页面</h1>
<div>账号
<input type="text" placeholder="请输入用户名" v-model="username">
</div>
<div>
密码
<input type="password" placeholder="请输入密码" v-model="password">
</div>
<div>
<input type="button" value="登录" @click="doLogin"> </div> </div>
</template> <script>
export default {
name:'Login',
data(){
return{
username:'',
password:'', }
},
methods:{
doLogin:function(){
var _this = this this.$axios.request({
url:this.$store.state.apiList.auth,
method:'POST',
data:{
user:this.username,
pwd:this.password
},
headers:{
'Content-Type':'application/json'
}
}).then(function(arg){
// 拿到后端的状态码进行判断
if(arg.data.code === 1000){
// 把token 放到state中共享
// _this.$store.state.token = arg.data.token
// _this.$store.state.username = _this.username // saveToken 是一个参数,第二个参数我传了一个对象进去
_this.$store.commit('saveToken',{'token':arg.data.token,'username':_this.username}) //这个是我点击后发现没有登录 跳转过来的页面$route.query.backUrl是有值的
//登录后我就直接 跳转到他之前点击的页面
var url = _this.$route.query.backUrl
if(url){
_this.$router.push({path:url})
// 如果是直接登录的话 我就跳转到主页面
}else{
_this.$route.push({path:'/'}) }
}else{
alert(arg.data.error) } }).catch(function(arg){
console.log(发生错误) })
}
}
}
</script>> <style scoped> </style>

// (局部限制) 你必须带token 才能访问我

    mounted(){
if(!this.$store.state.token){
this.$router.push({name:'login'})
}
}

路由中的设置

import Vue from 'vue'
import Router from 'vue-router' // 引入路由页面
import Homepag from '@/components/Homepag'
import Course from '@/components/Course'
import CourseDetail from '@/components/CourseDetail'
import Login from '@/components/Login'
import FeatureLeesion from '@/components/FeatureLeesion' //引入elment快速搭建
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css'; //使用elment组件
Vue.use(ElementUI); Vue.use(Router); export default new Router({
routes: [
{
path: '/',
name: 'Homepag',
component: Homepag,
meta:{
requireAuth:true
}
},
{
path: '/course',
name: 'Course',
component: Course,
meta:{
requireAuth:true
}
},
{
path: '/coursedetail/:id',
name: 'CourseDetail',
component: CourseDetail,
meta:{
requireAuth:true
}
},
{
path: '/login',
name: 'Login',
component: Login,
meta:{
requireAuth:false
}
},
{
path: '/featureleesion',
name: 'FeatureLeesion',
component: FeatureLeesion,
meta:{
requireAuth:true
}
},
]
})

vue 用户登录 路由拦截 vuex cookie的更多相关文章

  1. vue实现登录路由拦截

    第一步 在router.js里面 把需要判断是否登录的路由添加meta对象属性 在meta对象里面自定义一个属性值 第二步 : 在router.js里面 与const router = new Rou ...

  2. 初学node.js-nodejs中实现用户登录路由

    经过前面几次的学习,已经可以做下小功能,今天要实现的事用户登录路由. 一.users_model.js  功能:定义用户对象模型 var mongoose=require('mongoose'), S ...

  3. taotao用户登录springMVC拦截器的实现

    在springMVC中写拦截器,只需要两步: 一.写 java 拦截器类,实现 interceptor 拦截器接口. 二.在 springMVC 的xml配置文件中,配置我们创建的拦截器对象及其拦截目 ...

  4. 关于django用户登录认证中的cookie和session

    最近弄django的时候在用户登录这一块遇到了困难,网上的资料也都不完整或者存在缺陷. 写这篇文章的主要目的是对一些刚学django的新手朋友提供一些帮助.前提是你对django中的session和c ...

  5. 初识ABP vNext(4):vue用户登录&菜单权限

    Tips:本篇已加入系列文章阅读目录,可点击查看更多相关文章. 目录 前言 开始 登录 菜单权限 运行测试 最后 前言 上一篇已经创建好了前后端项目,本篇开始编码部分. 开始 几乎所有的系统都绕不开登 ...

  6. vue用户登录状态判断

    之前项目中用来判断是否登录我写了多种方案,但是最终只有一个方案是比较好的,这篇博客就是分享该方案; 先说基本要求: 项目中的登录状态是依据服务器里的状态来作为判断依据; 每一个需要登录后才能操作的接口 ...

  7. 关于用户登录状态存session,cookie还是数据库或者memcache的优劣

    session中保存登陆状态: 优:整个应用可以从session中获取用户信息,并且查询时很方便.在session中保存用户信息是不可缺少的(web应用中) 缺:session中不宜保存大量信息,会增 ...

  8. 「Vue」登陆-路由拦截器

    1.main.js设置拦截器 router.beforeEach(function (to,from,next) { if (to.meta.requireAuth) { if (store.stat ...

  9. 用户登录之asp.net cookie的写入、读取与操作

    页面前面: <div id="login" runat="server"> <span class="log_title" ...

随机推荐

  1. pytorch1.0批训练神经网络

    pytorch1.0批训练神经网络 import torch import torch.utils.data as Data # Torch 中提供了一种帮助整理数据结构的工具, 叫做 DataLoa ...

  2. [转帖]鲁大师Q3季度PC处理器排行:AMD、Intel终于五五开了

    鲁大师Q3季度PC处理器排行:AMD.Intel终于五五开了 https://www.cnbeta.com/articles/tech/902375.htm 近日,鲁大师发布了Q3季度PC处理器排行. ...

  3. 【转帖】2019年中国5G行业细分市场发展现状和市场前景分析 通信基站数量快速增长

    2019年中国5G行业细分市场发展现状和市场前景分析 通信基站数量快速增长 中国有 600多万个基站 平均每200个人 一个基站.. 一个基站十万块钱的话 相当于 每个人 需要分摊 500块钱. ht ...

  4. Python 装饰器执行顺序

    Python 装饰器执行顺序 之前同事问到两个装饰器在代码中使用顺序不同会不会有什么问题,装饰器是对被装饰的函数做了一层包装,然后执行的时候执行了被包装后的函数,例如: def decorator_a ...

  5. 2019ICPC南昌现场赛总结

    非常可惜的一场比赛,多了60分钟罚时与银牌无缘.今年6场ICPC网络赛里面打的最差的就是南昌站,冥冥之中自有天意吧,最后被安排去了南昌. 开场被队友叫去先看的L,说是足球,发现就是简单模拟,就直接上机 ...

  6. python 之 并发编程(生产者消费者模型、守护进程的应用)

    9.8 生产者消费者模型 该模型中包含两类重要的角色: 1.生产者:将负责造数据的任务比喻为生产者 2.消费者:接收生产者造出的数据来做进一步的处理的被比喻成消费者 实现生产者消费者模型三要素:1.生 ...

  7. PAT(B) 1078 字符串压缩与解压(Java)

    题目链接:1078 字符串压缩与解压 (20 point(s)) 题目描述 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示 ...

  8. go context 源码分析

    WithCancel func WithCancel(parent Context) (ctx Context, cancel CancelFunc) { c := newCancelCtx(pare ...

  9. nodejs中使用mongodb

    /** * 使用mongodb存储数据 * 1 首先安装mongodb nodejs插件 npm install mongodb --save-dev * 2 安装express (非必须) * * ...

  10. C#判断字符串中含有多少个汉字

    private void button1_Click(object sender, EventArgs e) { ArrayList itemList = new ArrayList(); CharE ...