欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3使用localStorage實現(xiàn)登錄注冊功能實例

 更新時間:2023年06月30日 10:23:07   作者:萬能守恒定律  
這篇文章主要給大家介紹了關(guān)于vue3使用localStorage實現(xiàn)登錄注冊功能的相關(guān)資料, localStorage這個特性主要是用來作為本地存儲來使用的,解決了cookie存儲空間不足的問題,需要的朋友可以參考下

也許我們使用過vuex實現(xiàn)過登錄注冊,但是我們會發(fā)現(xiàn),vuex一但刷新,就會被消除,于是我們就使用網(wǎng)頁的存儲方式localStorage的方式進(jìn)行存儲。

首先我們要構(gòu)造一個vue3的項目。目錄創(chuàng)建如下,紅色區(qū)域為需要修改的地方

App.vue如下

<template>
  <nav>
    <router-link to="/login">登錄</router-link> |
    <router-link to="/regester">注冊</router-link>
  </nav>
  <router-view/>
</template>
<style lang="scss">
#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;
  a {
    font-weight: bold;
    color: #2c3e50;
    &.router-link-exact-active {
      color: #42b983;
    }
  }
}
</style>

接著去配置路由,router下面的index.js

import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import login from '../views/login.vue'
const routes = [
  {
    //這里需要將根目錄默認(rèn)為Home,方便實現(xiàn)用戶在保持登錄 狀態(tài)下再次登錄時直接跳轉(zhuǎn)至主頁面
      path:"/",
      redirect:{
        name:"HomeView"
      }
    },
    {
      path: "/HomeView",
      name: "HomeView",
      component: HomeView,
    },
    {
      path: "/login",
      name: "login",
      component:login
    }
    ,{
      path: "/regester",
      name: "regester",
      component: () =>
        import("../views/regester.vue")
    }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
// 路由守衛(wèi)
router.beforeEach((to,from,next)=>
{
  //登錄及注冊頁面可以直接進(jìn)入,而主頁面需要分情況
  if(to.path=='/login')
  {
    next();
    console.log(localStorage.s);
  }
  else if(to.path=='/regester')
  {
    next();
  }
  else
  {
    if(from.path=="/login")//從登錄頁面可以直接通過登錄進(jìn)入主頁面
    {
      next();
    }
    else{
        //從/進(jìn)入,如果登錄狀態(tài)是true,則直接next進(jìn)入主頁面
          if(localStorage.s === "true")
            {
              next();
              console.log(localStorage['s'])
            }
        else {//如果登錄狀態(tài)是false,那么跳轉(zhuǎn)至登錄頁面,需要登錄才能進(jìn)入主頁面
          next('/login');
          console.log("需要登錄")
        }
    }
  }
})
export default router

接著我們來看登錄,login.vue

<template>
<div id="background">
    <div class="container">
        <form action="">
          <h1>Login</h1>
          <div class="form">
              <div class="item">
                <label>用戶名:</label><input type="text" name="username" v-model.trim="name" placeholder="請輸入用戶名">
                <!-- v-model把輸入的值傳輸給name變量 -->
                <br/>
              </div>
              <div class="item">
                <label>密碼:</label><input type="password" name="password" v-model.trim="password" placeholder="請輸入密碼">
                <br/>
              </div>
              <div class="keep">
                <input @click="handlesave" id="yes" type="radio" value="0" ><!-- 點擊選中 -->
                <label for="yes">保持登錄狀態(tài)</label>
              </div>
          </div>
        </form>
              <button  type="submit" @click.prevent="handlelogin">登錄            </button>
              <!-- v-on點擊按鈕觸發(fā)handlelogin方法 -->
              <button  @click.prevent="handleregister">注冊</button>
              <router-view></router-view>
    </div>
</div>
</template>
<style scoped>
.container{
  width: 480px;
  height: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background:#00000090;
  text-align: center;
  border-radius: 20px;
  margin-top: 10px;
}
.container h1{
  color: aliceblue;
  margin-left: 20px;
}
.item {
  color: white;
  margin-left: 15%;
  margin-top: 35px;
  font-size: 20px;
  text-align: left;
}
.item label{
  float:left;
  width: 5em;
  margin-right: 1em;
  text-align: right;
}
input{
  margin-left: -5px;
  padding: 4px;
  border: solid 1px #4e5ef3;
  outline: 0;
  font: normal 13px/100% Verdana,Tahoma,sans-serif;
  width: 200px;
  height: 23px;
  background:#f1f1f190;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
}
button{
  position: relative;
  height: 33px;
  width: 100px;
  background: rgba(35, 19, 252, 0.425);
  border-radius: 10px;
  margin-top: 18px;
  box-shadow: none;
  color: white;
  margin-left: 40px;
  margin-right: 10px;
}
.keep{
  color: white;
}
.keep input{
  width: 15px;
  height: 15px;
  margin-top: 7px;
  margin-left: 10px;
  margin-right: 10px;
}
</style>
<script>
export default {
  data(){
    return{
      name:"",//姓名,用v-model綁定監(jiān)聽,將輸入的字符串賦值給name變量
      password:"",//密碼
      st:"false",//false為不保存登錄
    };
  },
  methods:{
    handlelogin:function()
    {
      // 判斷注冊中的信息
      if(this.name===localStorage['name'] && this.password===localStorage['password'])
       {
         this.$router.replace('/HomeView');//如果輸入的名字以及密碼正確路由跳轉(zhuǎn)至個人頁面
       } 
       else if(this.name==='')//名字為空
       {
         alert('用戶名不為空');
       }
       else if(this.password==='')//密碼為空
       {
         alert('密碼不為空');
       }
      else{
         alert('賬號不存在,請注冊后登錄');//查無此號
        }
    },
    handleregister:function()
    {
      this.$router.replace('/regester')//點擊注冊按鈕,跳轉(zhuǎn)至注冊頁面
    },
    //點擊保持登錄狀態(tài)觸發(fā)handlesave
    handlesave:function(){
      this.st="true";//修改登錄狀態(tài)為true
      localStorage.setItem('s',this.st);
      console.log(localStorage.s);
    }
  }
};
</script>

然后是注冊界面,regester.vue

<template>
    <div id="background">
      <div id="contain">
        <h1>Regester</h1>
        <div class="form">
          <label>用戶名:</label><input type="text" v-model.trim="name"><br/>
        </div>
        <div class="form">
          <label>密碼:</label><input type="password" v-model.trim="password"><br/>
        </div>
        <div class="form">
          <label>郵箱:</label><input type="email" v-model.trim="mail"><br/>
        </div>
        <div class="form">
          <label>手機號:</label><input type="tel" v-model.trim="tel"><br/>
        </div>
        <button @click.prevent="handlefinish">提交</button>
      </div>
    </div>
</template>
<style scoped>
#contain{
  width: 580px;
  height: 450px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background:#00000090;
  text-align: center;
  border-radius: 20px;
}
#contain h1{
  color: white;
}
.form{
  color: white;
  margin-left: 20%;
  margin-top: 60px;
  font-size: 20px;
  text-align: left;
}
label{
  float:left;
  width: 5em;
  margin-right: 1em;
  text-align: right;
}
input,textarea{
  margin-left: 10px;
  padding: 4px;
  border: solid 1px #4e5ef3;
  outline: 0;
  font: normal 13px/100% Verdana,Tahoma,sans-serif;
  width: 200px;
  height: 20px;
  background:#f1f1f190;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
}
input:hover,textarea:hover,input:focus,textarea:focus{border-color:#0d0aa1;}
button{
  position: relative;
  height: 33px;
  width: 150px;
  background: rgba(35, 19, 252, 0.425);
  border-radius: 10px;
  margin-top: 38px;
  box-shadow: none;
  color: white;
  margin-left: 40px;
}
</style>
<script>
export default {
    name:'regester',
    props: {
    msg: String
  },
  data(){
    return{
      name:"",
      password:"",
      mail:"",
      tel:""
  };
  },
  methods:{
  //點擊完成按鈕觸發(fā)handlefinish
    handlefinish:function()
    {
      if(localStorage['name']===this.name)
      {
        alert("用戶名已存在");//如果用戶名已存在則無法注冊
      }
      else if(this.name==='')
      {
        alert("用戶名不能為空");
      }
      else{//將新用戶信息存儲到localStorage
        localStorage.setItem('name',this.name);
        localStorage.setItem('password',this.password);
        localStorage.setItem('mail',this.mail);
        localStorage.setItem('tel',this.tel);
        localStorage.setItem('s',"false");
        alert("注冊成功");
        this.$router.replace('/login');//完成注冊后跳轉(zhuǎn)至登錄頁面
      }
    }
  }
};
</script>

最后是展示信息的頁面,HomeView.vue

<template>
    <div id="bg">
      <div id="container">
        <h1>個人信息</h1>
        <p><span>姓名:</span>{{sname}}</p>
        <p><span>郵箱:</span>{{smail}}</p>
        <p><span>手機號:</span>{{stel}}</p>
        <button @click.prevent="logout">退出</button>
      </div>
    </div>
</template>
<script>
export default {
    name:'HomeView',
    data(){
      return{//獲取用戶信息到主頁
        sname:localStorage.getItem('name'),
        smail:localStorage.getItem('mail'),
        stel:localStorage.getItem('tel'),
        isAuth:"",//是否保持登錄狀態(tài)
      };
    },
    methods:{
      //當(dāng)點擊退出按鈕,將不保存登錄狀態(tài)
      logout:function()
      {
        this.isAuth="false";//修改登錄狀態(tài)
        localStorage.setItem('s',this.isAuth);
        this.$router.replace('/login');//頁面跳轉(zhuǎn)至登錄頁面
      }
    }
}
</script>
<style scoped>
  #container{
    width: 480px;
    height: 300px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    background:#00000090;
    text-align: center;
    border-radius: 20px;
    margin-top: 10px;
    color: white;
  }
p{
  font-size: 20px;
  margin-top: 20px;
  text-align: left;
  margin-left: 32%;
}
button{
  position: relative;
  height: 33px;
  width: 150px;
  background: rgba(35, 19, 252, 0.425);
  border-radius: 10px;
  margin-top: 10px;
  box-shadow: none;
  color: white;
  margin-left: 10px;
}
</style>
 

我們來看效果圖:

好啦,其實登錄注冊的形式有很多種,可以是根據(jù)后端,也可以采用node.js中端形式,也可以用vuex臨時存儲~

總結(jié)

到此這篇關(guān)于vue3使用localStorage實現(xiàn)登錄注冊功能的文章就介紹到這了,更多相關(guān)vue3 localStorage實現(xiàn)登錄注冊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue出現(xiàn)did you register the component correctly?解決方案

    Vue出現(xiàn)did you register the component 

    這篇文章主要介紹了Vue出現(xiàn)did you register the component correctly?解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • element el-tooltip動態(tài)顯示隱藏(有省略號顯示,沒有省略號不顯示)

    element el-tooltip動態(tài)顯示隱藏(有省略號顯示,沒有省略號不顯示)

    本文主要介紹了element el-tooltip動態(tài)顯示隱藏,主要實現(xiàn)有省略號顯示,沒有省略號不顯示,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • 如何為Vue3提供一個可媲美Angular的ioc容器

    如何為Vue3提供一個可媲美Angular的ioc容器

    ue3因其出色的響應(yīng)式系統(tǒng),以及便利的功能特性,完全勝任大型業(yè)務(wù)系統(tǒng)的開發(fā),這篇文章主要介紹了如何為Vue3提供一個可媲美Angular的ioc容器,需要的朋友可以參考下
    2024-08-08
  • Vue中使用import進(jìn)行路由懶加載的原理分析

    Vue中使用import進(jìn)行路由懶加載的原理分析

    這篇文章主要介紹了Vue中使用import進(jìn)行路由懶加載的原理分析。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 詳解Vue與VueComponent的關(guān)系

    詳解Vue與VueComponent的關(guān)系

    這篇文章主要為大家介紹了Vue與VueComponent的關(guān)系,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • 頁面內(nèi)錨點定位及跳轉(zhuǎn)方法總結(jié)(推薦)

    頁面內(nèi)錨點定位及跳轉(zhuǎn)方法總結(jié)(推薦)

    這篇文章主要介紹了頁面內(nèi)錨點定位及跳轉(zhuǎn)方法總結(jié),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • VUE前端cookie簡單操作

    VUE前端cookie簡單操作

    這篇文章主要為大家詳細(xì)介紹了VUE前端cookie簡單操作代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • vue項目實現(xiàn)文件下載進(jìn)度條功能

    vue項目實現(xiàn)文件下載進(jìn)度條功能

    這篇文章主要介紹了vue項目實現(xiàn)文件下載進(jìn)度條功能,本文通過具體實現(xiàn)代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • Vue2中配置Cesium全過程

    Vue2中配置Cesium全過程

    這篇文章主要介紹了Vue2中配置Cesium全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • vue實現(xiàn)側(cè)邊定位欄

    vue實現(xiàn)側(cè)邊定位欄

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)側(cè)邊定位欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07

最新評論