最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue實現(xiàn)用戶沒有登陸時,訪問后自動跳轉(zhuǎn)登錄頁面的實現(xiàn)思路

 更新時間:2023年02月23日 08:23:34   作者:劃水的魚dm  
這篇文章主要介紹了Vue實現(xiàn)用戶沒有登陸時,訪問后自動跳轉(zhuǎn)登錄頁面,定義路由的時候配置屬性,這里使用needLogin標(biāo)記訪問頁面是否需要登錄,本文通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下

設(shè)計思路

  • 定義路由的時候配置屬性,這里使用needLogin標(biāo)記訪問頁面是否需要登錄
  • 設(shè)置路由守衛(wèi),每個頁面在跳轉(zhuǎn)之前都要經(jīng)過驗證,校驗用戶信息是否存在,不存在跳轉(zhuǎn)到登錄頁
  • 用戶登錄后將用戶信息存儲在localStorage
  • 退出登錄后,將用戶信息清空

 代碼實現(xiàn)

1、router文件夾的index.js文件中

  • 在router中每個地址在meta屬性中配置needLogin熟悉,判斷訪問頁面是否需要登錄
  • 404頁面放在最后,匹配所有鏈接,實現(xiàn)輸入不存在的地址時自動跳轉(zhuǎn)404頁面
import Vue from 'vue'
import Router from 'vue-router'
import LoginCard from "../components/LoginCard";
import Home from "../components/Home";
import ErrorPage from "../components/ErrorPage";

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'LoginCard',
      component: LoginCard,
      meta: {
        needLogin: false
      }
    },
    {
      path: '/loginCard',
      name: 'LoginCard',
      component: LoginCard,
      meta: {
        needLogin: false
      }
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      meta: {
        needLogin: true
      }
    }, {
      path: '/*',
      name: 'ErrorPage',
      component: ErrorPage,
      meta:{
        needLogin: false
      }

    }
  ]
})

2、在main.js中定義一個路由前置守衛(wèi),每次跳轉(zhuǎn)頁面進(jìn)行判斷,沒有登陸自動挑戰(zhàn)登陸界面

import Vue from 'vue'
import App from './App'
import router from './router'
import VueRouter from "vue-router";
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import * as auth from './utils/auth'
import store from './store'
import Vuex from "vuex";

Vue.config.productionTip = false;
Vue.use(ElementUI);
Vue.use(VueRouter);
Vue.use(Vuex)


//這個方法需要放在new Vue之前,不然按F5刷新頁面不會調(diào)用這個方法
router.beforeEach(function (to, from, next) {
  console.log('是否需要登錄才能訪問')
  if (to.meta.needLogin) {
    if (auth.getAdminInfo()) {
      console.log(auth.getAdminInfo())
      console.log('有cookie信息')
      next();
    }else {
      console.log('無cookie信息')

      next({
        path:'/loginCard'
      });
    }
  }else{
    next();
  }
})

new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

3、編寫一個存儲數(shù)據(jù)的工具,使用cookie存儲用戶登錄后的信息

import Cookies from 'js-cookie'
const adminInfo = "adminInfo"

//獲取用戶信息
export function getAdminInfo() {
  const admin = Cookies.get(adminInfo)
  if(admin){
    return JSON.parse(admin)
  }
  return ''
}
//存儲用戶信息
export function setAdminInfo(admin) {
  return Cookies.set(adminInfo, JSON.stringify(admin))
}
//移除用戶信息
export function removeAdminInfo() {

  return Cookies.remove(adminInfo)
}

4、寫一個登錄頁面,用戶登錄后就將數(shù)據(jù)存儲在cookie中

?<template>
  <div>
    <el-form ref="loginForm" :rules="formRules" :model="loginUser" label-width="80px" class="login-box">
      <h3 style="margin-bottom: 50px">歡迎登錄</h3>
      <el-form-item label="用戶名" prop="username">
        <el-input prefix-icon="el-icon-user" type="text" v-model="loginUser.username"  placeholder="請輸入用戶名" :maxlength="50" clearable></el-input>
      </el-form-item>
      <el-form-item label="密碼" prop="password">
        <el-input prefix-icon="el-icon-lock" type="password" v-model="loginUser.password"  placeholder="請輸入密碼" :maxlength="50" clearable>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登陸</el-button>
        <el-button icon="" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as auth from '../utils/auth'
export default {
  name: 'LoginCard',
  data() {
    return {
      loginUser: {
        username: '',
        password: '',
      },
      formRules: {
        //制定表單輸入的規(guī)則
        username: [{required: true, message: '用戶名不能為空', trigger: 'blur'}],
        password: [{required: true, message: '密碼不能為空', trigger: 'blur'}]
      }
    }
  },
  methods: {

    onSubmit() {
      //判斷表單是否符合規(guī)則
      this.$refs['loginForm'].validate((valid) => {
          if (valid) {
            if (this.loginUser.username !== '123456' || this.loginUser.password !== '123456'){
              this.$message({
                message:'賬號或密碼錯誤',
                type: 'error',
              });
              return;
            }
            auth.setAdminInfo(this.loginUser);
            this.$router.push({path:'/home'});
          }
        }
      )
    },
    resetForm(){
      this.$refs['loginForm'].resetFields();
    },
  }
}
</script>
<style scoped>
.login-box {
  border: 1px solid #DCDFE6;
  width: 400px;
  margin: 180px auto;
  padding: 35px 35px 15px 35px;
  border-radius: 5px;
}
</style>

5、編寫一個退出頁面,用戶退出以后,將用戶信息從cookie中去除,跳轉(zhuǎn)到登陸頁面

?<template>
  <div>
    <h1>主頁面</h1>
    <el-button @click="logout">退出登錄</el-button>
  </div>
</template>
<script>
import * as auth from '../utils/auth'

export default {
  name : 'Home',
  data() {
    return {
    };
  },
  methods: {
    logout(){
      auth.removeAdminInfo();
      this.$router.push({path:'/loginCard'});
    }
  },
  mounted() {
  }
}
</script>

基本目錄結(jié)構(gòu)是這樣的

到此這篇關(guān)于Vue學(xué)習(xí):實現(xiàn)用戶沒有登陸時,訪問后自動跳轉(zhuǎn)登錄頁面的文章就介紹到這了,更多相關(guān)Vue自動跳轉(zhuǎn)登錄頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue項目打包后部署到服務(wù)器的詳細(xì)步驟

    vue項目打包后部署到服務(wù)器的詳細(xì)步驟

    這篇文章主要介紹了vue項目打包后部署到服務(wù)器,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09
  • 手把手教你使用寶塔部署Vue項目

    手把手教你使用寶塔部署Vue項目

    這篇文章主要給大家介紹了關(guān)于如何使用寶塔部署Vue項目的相關(guān)資料,寶塔面板提供了非常方便的方式來部署 Vue 項目,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • 詳解vue-cli 腳手架項目-package.json

    詳解vue-cli 腳手架項目-package.json

    本篇文章主要介紹了詳解vue-cli 腳手架項目-package.json,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Vue.js特性Scoped Slots的淺析

    Vue.js特性Scoped Slots的淺析

    這篇文章主要介紹了Vue.js特性Scoped Slots的淺析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • vue3緩存頁面keep-alive及路由統(tǒng)一處理詳解

    vue3緩存頁面keep-alive及路由統(tǒng)一處理詳解

    當(dāng)我們不想每次跳轉(zhuǎn)路由都會重新加載頁面時(重新加載頁面很耗時),就可以考慮使用keep-alive緩存頁面了,這篇文章主要給大家介紹了關(guān)于vue3緩存頁面keep-alive及路由統(tǒng)一處理的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • vue動態(tài)加載本地圖片的處理方法

    vue動態(tài)加載本地圖片的處理方法

    最近遇到了個問題,用v-bind動態(tài)綁定img的src,圖片加載不出來,所以下面這篇文章主要給大家介紹了關(guān)于vue動態(tài)加載本地圖片的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • elementui實現(xiàn)表格自定義排序的示例代碼

    elementui實現(xiàn)表格自定義排序的示例代碼

    本文主要介紹了elementui實現(xiàn)表格自定義排序的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 阿望教你用vue寫掃雷小游戲

    阿望教你用vue寫掃雷小游戲

    這篇文章主要介紹了阿望教你用vue寫掃雷小游戲,本文通過實例代碼效果圖展示的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • 詳解vue2.0+axios+mock+axios-mock+adapter實現(xiàn)登陸

    詳解vue2.0+axios+mock+axios-mock+adapter實現(xiàn)登陸

    這篇文章主要介紹了詳解vue2.0+axios+mock+axios-mock+adapter實現(xiàn)登陸,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • ant-design-vue 實現(xiàn)表格內(nèi)部字段驗證功能

    ant-design-vue 實現(xiàn)表格內(nèi)部字段驗證功能

    這篇文章主要介紹了ant-design-vue 實現(xiàn)表格內(nèi)部字段驗證功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12

最新評論

中宁县| 许昌市| 呼玛县| 牙克石市| 清原| 阳春市| 肥乡县| 睢宁县| 英山县| 临潭县| 黄浦区| 嵊州市| 枣庄市| 防城港市| 白朗县| 恭城| 汉沽区| 东辽县| 济阳县| 江阴市| 巴马| 石泉县| 龙陵县| 酒泉市| 成都市| 河南省| 大宁县| 霍城县| 建水县| 兴山县| 白银市| 界首市| 邯郸县| 和平县| 方正县| 甘孜县| 广昌县| 新晃| 永福县| 右玉县| 信宜市|