亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

設(shè)計思路

  • 定義路由的時候配置屬性,這里使用needLogin標記訪問頁面是否需要登錄
  • 設(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)頁面進行判斷,沒有登陸自動挑戰(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學習:實現(xiàn)用戶沒有登陸時,訪問后自動跳轉(zhuǎn)登錄頁面的文章就介紹到這了,更多相關(guān)Vue自動跳轉(zhuǎn)登錄頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用watch監(jiān)聽路由變化和watch監(jiān)聽對象的實例

    使用watch監(jiān)聽路由變化和watch監(jiān)聽對象的實例

    下面小編就為大家分享一篇使用watch監(jiān)聽路由變化和watch監(jiān)聽對象的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • 詳解vue移動端項目的適配(以mint-ui為例)

    詳解vue移動端項目的適配(以mint-ui為例)

    這篇文章主要介紹了詳解vue移動端項目的適配(以mint-ui為例),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • axios庫的核心代碼解析及總結(jié)

    axios庫的核心代碼解析及總結(jié)

    這篇博客針對axios庫的核心代碼做一個簡要總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • vue2.0 computed 計算list循環(huán)后累加值的實例

    vue2.0 computed 計算list循環(huán)后累加值的實例

    下面小編就為大家分享一篇vue2.0 computed 計算list循環(huán)后累加值的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • 對Vue.js之事件的綁定(v-on: 或者 @ )詳解

    對Vue.js之事件的綁定(v-on: 或者 @ )詳解

    今天小編就為大家分享一篇對Vue.js之事件的綁定(v-on: 或者 @ )詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • 基于Vue制作組織架構(gòu)樹組件

    基于Vue制作組織架構(gòu)樹組件

    最近公司在做一個基于vue開發(fā)的項目,項目需要開發(fā)一個展示組織架構(gòu)的樹組件,在網(wǎng)上搜了半天,沒有找到合適的,下面小編給大家分享一個基于Vue制作組織架構(gòu)樹組件,需要的朋友參考下吧
    2017-12-12
  • 解決vue 使用setTimeout,離開當前路由setTimeout未銷毀的問題

    解決vue 使用setTimeout,離開當前路由setTimeout未銷毀的問題

    這篇文章主要介紹了解決vue 使用setTimeout,離開當前路由setTimeout未銷毀的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue請求后端數(shù)據(jù)和跨域問題解決

    vue請求后端數(shù)據(jù)和跨域問題解決

    這篇文章主要介紹了vue請求后端數(shù)據(jù)和跨域問題,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • vue實現(xiàn)換膚功能

    vue實現(xiàn)換膚功能

    這篇文章主要為大家詳細介紹了vue實現(xiàn)換膚功能,一套深色,一套淺色,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Vue組件設(shè)計-滾動置頂設(shè)計案例

    Vue組件設(shè)計-滾動置頂設(shè)計案例

    這篇文章主要介紹了Vue組件設(shè)計-滾動置頂設(shè)計案例,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04

最新評論