這篇“基于Vue3和elementplus怎么實(shí)現(xiàn)登錄功能”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“基于Vue3和elementplus怎么實(shí)現(xiàn)登錄功能”文章吧。
站在用戶的角度思考問題,與客戶深入溝通,找到宣化網(wǎng)站設(shè)計(jì)與宣化網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、申請(qǐng)域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋宣化地區(qū)。
登錄頁面:
注冊(cè)頁面:
(1)引入element-plus組件庫
引入組件庫的方式有好多種,在這里我就在main.js全局引入了.
npm i element-plus -S
main.js中代碼:
import { createApp } from "vue"; //element-plus import ElementPlus from "element-plus"; import "element-plus/dist/index.css"; import App from "./App.vue"; import router from "./router"; import axios from "axios"; import store from "./store"; //創(chuàng)建實(shí)例 const app = createApp(App); //全局應(yīng)用配置 app.config.globalProperties.$axios = axios; app.use(ElementPlus).use(store).use(router).mount("#app");
引入之后自己可以用幾個(gè)按鈕測(cè)試一下是否引入成功.
(2)登錄及注冊(cè)頁面
html部分
views/account/Login.vue
- {{ item.label }}
獲取驗(yàn)證碼 {{ current_menu === "login" ? "登錄" : "注冊(cè)" }}
js部分
css部分(使用了scss)
(3)封裝一些公共方法及樣式
新建styles文件夾,然后新建幾個(gè)樣式文件:
normalize.scss
/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */ /* Document ========================================================================== */ /** * 1. Correct the line height in all browsers. * 2. Prevent adjustments of font size after orientation changes in iOS. */ /* div的默認(rèn)樣式不存在padding和margin為0的情況*/ html, body, span, applet, object, iframe, h3, h4, h5, h6, h7, h7, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, fieldset, form, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } html { line-height: 1.15; /* 1 */ -webkit-text-size-adjust: 100%; /* 2 */ } /* Sections ========================================================================== */ /** * Remove the margin in all browsers. */ body { margin: 0; font-family: 'Microsoft YaHei'; font-size: 14px; } /** * Render the `main` element consistently in IE. */ main { display: block; } /** * Correct the font size and margin on `h3` elements within `section` and * `article` contexts in Chrome, Firefox, and Safari. */ /* Grouping content ========================================================================== */ /** * 1. Add the correct box sizing in Firefox. * 2. Show the overflow in Edge and IE. */ hr { box-sizing: content-box; /* 1 */ height: 0; /* 1 */ overflow: visible; /* 2 */ } /** * 1. Correct the inheritance and scaling of font size in all browsers. * 2. Correct the odd `em` font sizing in all browsers. */ pre { font-family: monospace, monospace; /* 1 */ font-size: 1em; /* 2 */ } /* Text-level semantics ========================================================================== */ /** * Remove the gray background on active links in IE 10. */ a { background-color: transparent; text-decoration: none; } /** * 1. Remove the bottom border in Chrome 57- * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari. */ abbr[title] { border-bottom: none; /* 1 */ text-decoration: underline; /* 2 */ text-decoration: underline dotted; /* 2 */ } /** * Add the correct font weight in Chrome, Edge, and Safari. */ b, strong { font-weight: bolder; } /** * 1. Correct the inheritance and scaling of font size in all browsers. * 2. Correct the odd `em` font sizing in all browsers. */ code, kbd, samp { font-family: monospace, monospace; /* 1 */ font-size: 1em; /* 2 */ } /** * Add the correct font size in all browsers. */ small { font-size: 80%; } /** * Prevent `sub` and `sup` elements from affecting the line height in * all browsers. */ sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; } sub { bottom: -0.25em; } sup { top: -0.5em; } /* Embedded content ========================================================================== */ /** * Remove the border on images inside links in IE 10. */ img { display: block; border-style: none; } /* Forms ========================================================================== */ /** * 1. Change the font styles in all browsers. * 2. Remove the margin in Firefox and Safari. */ button, input, optgroup, select, textarea { font-family: inherit; /* 1 */ font-size: 100%; /* 1 */ margin: 0; /* 2 */ } /** * Show the overflow in IE. * 1. Show the overflow in Edge. */ button, input { /* 1 */ overflow: visible; } /** * Remove the inheritance of text transform in Edge, Firefox, and IE. * 1. Remove the inheritance of text transform in Firefox. */ button, select { /* 1 */ text-transform: none; } /** * Correct the inability to style clickable types in iOS and Safari. */ button, [type="button"], [type="reset"], [type="submit"] { -webkit-appearance: button; } /** * Remove the inner border and padding in Firefox. */ button::-moz-focus-inner, [type="button"]::-moz-focus-inner, [type="reset"]::-moz-focus-inner, [type="submit"]::-moz-focus-inner { border-style: none; padding: 0; } /** * Restore the focus styles unset by the previous rule. */ button:-moz-focusring, [type="button"]:-moz-focusring, [type="reset"]:-moz-focusring, [type="submit"]:-moz-focusring { outline: 1px dotted ButtonText; } /** * Correct the padding in Firefox. */ fieldset { padding: 0.35em 0.75em 0.625em; } /** * 1. Correct the text wrapping in Edge and IE. * 2. Correct the color inheritance from `fieldset` elements in IE. * 3. Remove the padding so developers are not caught out when they zero out * `fieldset` elements in all browsers. */ legend { box-sizing: border-box; /* 1 */ color: inherit; /* 2 */ display: table; /* 1 */ max-width: 100%; /* 1 */ padding: 0; /* 3 */ white-space: normal; /* 1 */ } /** * Add the correct vertical alignment in Chrome, Firefox, and Opera. */ progress { vertical-align: baseline; } /** * Remove the default vertical scrollbar in IE 10+. */ textarea { overflow: auto; } /** * 1. Add the correct box sizing in IE 10. * 2. Remove the padding in IE 10. */ [type="checkbox"], [type="radio"] { box-sizing: border-box; /* 1 */ padding: 0; /* 2 */ } /** * Correct the cursor style of increment and decrement buttons in Chrome. */ [type="number"]::-webkit-inner-spin-button, [type="number"]::-webkit-outer-spin-button { height: auto; } /** * 1. Correct the odd appearance in Chrome and Safari. * 2. Correct the outline style in Safari. */ [type="search"] { -webkit-appearance: textfield; /* 1 */ outline-offset: -2px; /* 2 */ } /** * Remove the inner padding in Chrome and Safari on macOS. */ [type="search"]::-webkit-search-decoration { -webkit-appearance: none; } /** * 1. Correct the inability to style clickable types in iOS and Safari. * 2. Change font properties to `inherit` in Safari. */ ::-webkit-file-upload-button { -webkit-appearance: button; /* 1 */ font: inherit; /* 2 */ } /* Interactive ========================================================================== */ /* * Add the correct display in Edge, IE 10+, and Firefox. */ details { display: block; } /* * Add the correct display in all browsers. */ summary { display: list-item; } /* Misc ========================================================================== */ /** * Add the correct display in IE 10+. */ template { display: none; } /** * Add the correct display in IE 10. */ [hidden] { display: none; } ul, li { list-style: none; }
elementui.scss(當(dāng)時(shí)測(cè)試時(shí)用的)
.el-button-block{ display: block; width: 100%; }
新建main.scss(引入上方兩個(gè)樣式文件)
@import "./normalize.scss"; @import './elementui.scss'
vue.config.js配置一下樣式文件
css: { // 是否使用css分離插件 ExtractTextPlugin extract: true, // 開啟 CSS source maps? sourceMap: false, // css預(yù)設(shè)器配置項(xiàng) loaderOptions: { scss: { additionalData: `@import "./src/styles/main.scss";`, }, }, // requireModuleExtension: true, },
登錄中封裝的校驗(yàn)方法
新建utils文件夾,
a.validate.js
// 校驗(yàn)郵箱 export function validate_email(value) { let regEmail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/; return regEmail.test(value); } // 校驗(yàn)密碼 export function validate_password(value) { let regPassword = /^(?!\D+$)(?![^a-zA-Z]+$)\S{6,20}$/; return regPassword.test(value); } // 校驗(yàn)驗(yàn)證碼 export function validate_code(value) { let regCode = /^[a-z0-9]{6}$/; return regCode.test(value); }
封裝請(qǐng)求方法
npm i axios -S
記得先在main.js中引入axios
import axios from "axios";
utils中新建request.js
import axios from "axios"; //引入element-plus import { ElMessage } from "element-plus"; console.log("11", process.env.VUE_APP_API); //undefined?? //創(chuàng)建實(shí)例 const service = axios.create({ baseURL: "/devApi", //請(qǐng)求地址 timeout: 5000, //超時(shí) }); //添加請(qǐng)求攔截器 service.interceptors.request.use( function (config) { //在發(fā)送請(qǐng)求之前做些什么 return config; }, function (error) { console.log(error.request); const errorData = JSON.parse(error.request.response); if (errorData.message) { //判斷是否具有message屬性 ElMessage({ message: errorData.message, type: "error", }); } //對(duì)請(qǐng)求錯(cuò)誤做些什么 return Promise.reject(errorData); } ); //添加響 應(yīng)攔截器 service.interceptors.response.use( function (response) { //對(duì)響應(yīng)數(shù)據(jù)做些什么 console.log("響應(yīng)數(shù)據(jù)", response); const data = response.data; if (data.resCode === 0) { return Promise.resolve(data); } else { ElMessage({ message: data.message, type: "error", }); return Promise.reject(data); } }, function (error) { //對(duì)響應(yīng)錯(cuò)誤做些什么 const errorData = JSON.parse(error.request.response); if (errorData.message) { //判斷是否具有message屬性 ElMessage({ message: errorData.message, type: "error", }); } return Promise.reject(errorData); } ); //暴露service export default service;
(4)配置環(huán)境變量
和項(xiàng)目根路徑同級(jí),新建幾個(gè)文件:
.env.development
VUE_APP_API = '/devApi'
可以自定義,但是必須是VUE_APP_XXX的格式
.env.production
VUE_APP_API = '/production'
.env.test
VUE_APP_API = '/test'
配置完后記得在axios文件中打印一下,看下能輸出自己配置的環(huán)境變量嗎.
(5)配置代理(跨域)
基本大同小異,代理地址改成自己的就可以了.
devServer: { open: false, //編譯完成是否自動(dòng)打開網(wǎng)頁 host: "0.0.0.0", //指定使用地址,默認(rèn)是localhost,0.0.0.0代表可以被外界訪問 port: 8080, proxy: { "/devApi": { target: "http://v3.web-jshtml.cn/api", //(必選)API服務(wù)器的地址 changeOrigin: true, //(必選) 是否允許跨域 ws: false, //(可選) 是否啟用websockets secure: false, //(可選) 是否啟用https接口 pathRewrite: { "^/devApi": "", //匹配開頭為/devApi的字符串,并替換成空字符串 }, }, }, },
以上就是關(guān)于“基于Vue3和elementplus怎么實(shí)現(xiàn)登錄功能”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。