技术点:通过vue状态管理器,对已经登录的用户显示不同的页面:

一  vue之状态管理器应用

主要用来存储cookie信息
与vue-cookies一起使用
安装:npm install vue-cookies

1.1登录页面

<template>
<div class="login">
<h1>登录</h1> <p>用户名:<input type="text" v-model="name"></p>
<p>密码:<input type="password" v-model="pwd"></p> <button @click="login" class="btn bg-danger">登录</button>
</div>
</template> <script> export default {
data: function () {
return {
name:'',
pwd:''
} },
methods: {
login: function () {
let _this = this
this.$http.request({
url: _this.$url + 'login/',
method: 'post',
data:{'name':_this.name,'pwd':_this.pwd}
}).then(function (response) {
console.log(response.data)
alert(_this.$store.state.name)
if(response.data.status==100){
//往stort.js的state的字典中写入值
// _this.$store.state.name=_this.name
// _this.$store.state.token=response.data.token
//调用store下的某个方法,用commit,第一个参数是方法名,第二个参数是参数
_this.$store.commit('login',response)
}
alert(_this.$store.state.name) }).catch(function (response) {
console.log(response)
}) }, },
}
</script>
<style>
</style>

1.2在app中

如果登录,显示用户姓名与注销,否则为登录
<template>
<div id="app">
<div id="nav">
<router-link to="/">Home</router-link>
|
<router-link to="/about">About</router-link>
|
<router-link to="/course">Course</router-link>
|
<span class="pull-right">
<span v-if="!this.$store.state.name">
<router-link to="/login">登录</router-link>
<router-link to="/login">注册</router-link>
</span>
<span v-else>
<span>{{this.$store.state.name}}</span>
<a @click="logout">注销</a>
</span> </span> </div>
<router-view/>
</div>
</template>
<script> export default { methods: {
logout: function () {
this.$store.commit('logout') }, },
}
</script> <style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
} #nav {
padding: 30px;
} #nav a {
font-weight: bold;
color: #2c3e50;
} #nav a.router-link-exact-active {
color: #42b983;
}
</style>

1.3store中

import Vue from 'vue'
import Vuex from 'vuex'
import Cookie from 'vue-cookies'
Vue.use(Vuex) export default new Vuex.Store({
//存储全局变量
state: {
//不要写死,要从cookie中取
name: Cookie.get('name'),
token: Cookie.get('token') },
mutations: {
// 写方法,这里面写的所有方法,第一个参数,必须是state
login:function (state,response) {
//修改这两个变量的值
state.name=response.data.name
state.token=response.data.token
// 往cookie中写数据
Cookie.set('name',response.data.name)
Cookie.set('token',response.data.token) },
logout:function (state) {
//修改这两个变量的值
state.name=""
state.token=""
// 往cookie中写数据
Cookie.set('name',"")
Cookie.set('token',"")
} },
actions: {}
})

二  后台登录views

from rest_framework.views import APIView
from rest_framework.response import Response
from api import models
from api.MySer import CourseSerializer, CourseDetailSerializer, CourseCategorySerializer
from api.utils.commonUtils import MyResponse
import uuid class Login(APIView):
def post(self, request, *args, **kwargs):
response = MyResponse()
name = request.data.get('name')
pwd = request.data.get('pwd')
user = models.UserInfo.objects.filter(name=name, pwd=pwd).first()
if user:
# 得去UserToken表中存数据
# 生成一个随机字符串,不会重复
token = uuid.uuid4()
models.UserToken.objects.update_or_create(user=user, defaults={'token': token})
response.token = token
response.name=name
response.msg = '登录成功'
else:
response.msg = '用户名或密码错误'
response.status = '' return Response(response.get_dic)

vue状态管理器(用户登录简单应用)的更多相关文章

  1. vue - 状态管理器 Vuex

    状态管理 vuex是一个专门为vue.js设计的集中式状态管理架构.状态?我把它理解为在data中的属性需要共享给其他vue组件使用的部分,就叫做状态.简单的说就是data中需要共用的属性.

  2. Vue状态管理模式---Vuex

    1. Vuex是做什么的? 官方解释: Vuex 是一个专为Vue.js 应用程序开发的 状态管理模式 它采用 集中式存储管理 应用的所有组件的状态, 并以相应的规则保证状态以一种可预测的方式发生变化 ...

  3. Vue状态管理vuex

    前面的话 由于多个状态分散的跨越在许多组件和交互间各个角落,大型应用复杂度也经常逐渐增长.为了解决这个问题,Vue提供了vuex.本文将详细介绍Vue状态管理vuex 引入 当访问数据对象时,一个 V ...

  4. VueX状态管理器 的应用

    VueX状态管理器 cnpm i vuex axios -S 1 创建Vuex 仓库 import Vue from 'vue' import Vuex from 'vuex' vue.use(Vue ...

  5. 对于React各种状态管理器的解读

    首先我们要先知道什么是状态管理器,这玩意是干啥的? 当我们在多个页面中使用到了相同的属性时就可以用到状态管理器,将这些状态存到外部的一个单独的文件中,不管在什么时候想使用都可以很方便的获取. reac ...

  6. 一文解析Pinia和Vuex,带你全面理解这两个Vue状态管理模式

    Pinia和Vuex一样都是是vue的全局状态管理器.其实Pinia就是Vuex5,只不过为了尊重原作者的贡献就沿用了这个看起来很甜的名字Pinia. 本文将通过Vue3的形式对两者的不同实现方式进行 ...

  7. vuex(vue状态管理)

    vuex(vue状态管理) 1.先安装vuex npm install vuex --save   2.在项目的src目录下创建store目录,并且新建index.js文件,然后创建vuex实例,引入 ...

  8. VueX(vue状态管理)简单小实例

    VueX:状态管理 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 核心模块:State. ...

  9. vue项目--vuex状态管理器

    本文取之官网和其他文章结合自己的理解用简单化的语言表达.用于自己的笔记记录,也希望能帮到其他小伙伴理解,学习更多的前端知识. Vuex 是什么? Vuex 是一个专为 Vue.js 应用程序开发的状态 ...

随机推荐

  1. PgSQl临时表的创建

    创建前可先删除 drop table tmp0 创建临时表 select * into temp table tmp0 from xxx create index idx_tmp0_inner_cd ...

  2. [systemd]Linux系统启动之systemd

    参照:https://wiki.debian.org/systemd 最近在添加板子应用程序自启动的时候,发现在rcN.d中的符号链接并没有用,文件系统为Debian Jessie 8, 后来从同事那 ...

  3. NAT and Traversal NAT(TURN/STUN/ICE)

    http://www.cnblogs.com/whyandinside/archive/2010/12/08/1900492.html -------------------------------- ...

  4. Requests blocked by CORS policy in spring boot and angular

    在本地启动Spring Boot后端和Angular前端调试时遇到跨域访问的问题导致前端请求失败. 错误描述 Access to XMLHttpRequest at 'http://localhost ...

  5. SQL Server,MySql,Oracle数据库的默认端口号

    SQL Server默认端口号为:1433 MySQL 默认端口号为:3306 Oracle 默认端口号为:1521

  6. 在Zookeeper中,znode是一个跟Unix文件系统路径相似的节点,可以往这个节点存储或获取数据

    在Zookeeper中,znode是一个跟Unix文件系统路径相似的节点,可以往这个节点存储或获取数据.如果在创建znode时Flag设置为EPHEMERAL,那么当创建这个znode的节点和Zook ...

  7. 欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function

    http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函 ...

  8. PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享

    php屏蔽电话号码中间四位: Method 1: function hidtel($phone){ $IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9] ...

  9. Gradle详解+Groovy

    http://blog.csdn.net/u014761700/article/details/51867939

  10. lua基础(一)

    参考链接: http://blog.csdn.net/lyh916/article/details/49719697 一.注释 --这是行注释 --[[ 这是块注释 这是块注释 这是块注释 --]] ...