Log in
Related configuration of git before development
- Create
git
a new branch (every time a new function is developed, it is best to create a new branch, and then merge it into the main branch after development) - Create a new branch
git checkout -b login
, create a sub-branchlogin
, and switch to that branch - View all branches of the current project
git branch
Create a login component and configure related routes
- Treat
login
the component as a redirected component, ie:
const routes = [
{
path:'/', redirect: '/login'},
{
path:'/login', component: Login}
]
- Introduce the global style
global.css
, and thenmain.js
introduce the global style in the entry fileimport '路径'
//引入全局样式文件
import '@/assets/css/global.css'
login
Then lay out a box in the middle of the component with absolute positioning- Install
element-ui
the plug-in (after installation, don't forget tomain.js
import it, otherwise an error will be reported) - Introduce
el-form、el-form-item
components, where two button components canel-button
also be placedel-form-item
in the component
If you
el-form
set the width to100%
, and thenpadding
squeeze the input box in, you will find that the input box on the right will exceed. The reason is thatform
the form defaults tocontent-box
, and the solution: just change itborder-box
.
In the icon in the input box, useelementui
the attribute of the input box to add the icon instead of标签<i>
the method of adding. That is, you can add display icons at the head and tail of the component through theprefix-icon
andsuffix-icon
attributesinput
, orslot
place icons through .
- Use Ali icon library
- Put the downloaded
font
folder into the resourceassets
directorymain.js
Import the font icon in the entry fileimport '@/assets/fonts/iconfont.css'
- Use: add the base class first
iconfont
, then add the class name of the icon you want
- Set up a request interceptor so that the permission field is added to the request header every time a network request is sent.
instance.interceptors.request.use(config =>{
config.headers.Authorization = window.sessionStorage.getItem('token')
return config
})
- Call the verification method
validate
to verify whether the content of the input box is legal
this.$refs.loginFormRef.validate(回调函数)
- Send a network request for login, and save it to the client
after successful login ; other API interfaces in the project except login must be accessed after login; it should only take effect when the current website is open, so save it intoken
sessionStorage
token
token
sessionStorage
login(){
this.$refs.loginFormRef.validate(async valid =>{
if(!valid) return
const {
data} = await requestLoginData({
params:this.loginForm})
if(data.meta.status === 200){
this.$message.success('登录成功')
window.sessionStorage.setItem('token',data.data.token)
this.$router.push('/home')
}else{
this.$message.error('您输入的用户或者密码不正确')
}
})
- Set route navigation guards
router.beforeEach((to, from, to) =>{
if(to.path == '/login') return next();
const token = window.sessionStorage.getItem('token')
if(!token){
return next('/login')
}
next()
})
- The reset button implements the reset of the form. Implementation method:
element
the method that comes with the callresetFields
preSet(){
this.$refs.loginFormRef.resetFields()
}
Code sync and upload
- View code status:
git status
- Add to cache:
git add
- submit:
git commit -m "完成了登录功能"
- View branch status:
git branch
- merge into master branch
- Switch to master branch:
git checkout master
- Merge all other branches based on the master branch:
git merge login
- Switch to master branch:
- Push the local code to the cloud:
git push
master
The above is just pushing the main branch to the cloud. If you want tologin
push the branch to the cloud, you need- switch to
login
branch - Push to the cloud:
git push -u origin login
- switch to
home page
- Page Layout
<el-container>
<el-header>Header</el-header>
<el-container>
<el-aside>Aside</el-aside>
<el-main>Main</el-main>
</el-container>
</el-container>
-
The logout function in the head realizes
the main task: after clicking logout, because it is a method-basedtoken
login, it only needs to destroy the localtoken
. In this way, subsequent requests will not carrytoken
, and you must log in again to generate a new onetoken
, and then visit the page.
Among them,token
the syntax for destroying the local is:window.sessionStorage.clear()
-
Implementation of the sidebar area
- Click on the merged implementation. Bind width to
el-aside
dynamic instead ofel-menu
dynamic binding.
<el-aside :width="isCollapse ? '64px' :' 200px'">
- Regarding the extra border pixel on the left side of the menu, you can
el-meun、el-submenu
add itoverflow:hidden
.
- Click on the merged implementation. Bind width to
Welcome component
- Take advantage of redirects,
main
displaying inWelcome
- New
Welcome
component - Configure routing information
- New
{
path:'/home',
component: Home,
redirect: '/welcome',
children:[
{
path:'/welcome',
component: Welcome
}]
}
main
Set the route placeholder in<router-view/>
- Using
path
information,main
routing jumps in the configuration. It should be noted that the information in the route must be consistentpath
with that in the returned datapath
- How to ensure that after refreshing the page, the menu
menu-item
is also in the active state?
menu
There is an attribute in: ,default-active
which saves the current active menuindex
, that is, if you want a certain item in the menu to be highlighted and activated, then assign theindex
value of that item todefault-active
.
After clicking a certain menu,index
store the current menu in itsessionStorage
, and after refreshing the page, assign the saved value to 1. Binddefault-active
a click event to the second-level menu . 2.
Pay attention to the question of whether to add'/'
it .path
sessionStorage
create
sessionStorage
User component
- Navigation breadcrumb area
- Usage of scoped slots:
<template slot-scope="scope">
</template>
- When changing the state, a network request needs to be sent. If the network request fails to be sent, the original state needs to be changed. And when the state is changed, you can call
el-switch
the built-inchange
event - The value in the header input box
v-model
can be set as: the parameters passed when sending a network request,query
and then attributes can be added to the input boxclearable
, and thenclear
events can be added, and the event response functions are all network request functions , so that the functions of andgetUserData
can be realized very quickly . Key point: Bind the value of the current input box as a parameter when sending a network request搜索
清除输入框
v-model
- Implementation of custom validation rules
data
Define the rule variable in, note that it is writtenreturn
above
// 验证邮箱的规则
var checkEmail = (rule, value, cb) => {
// 验证邮箱的正则表达式
const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
if (regEmail.test(value)) {
// 合法的邮箱
return cb()
}
cb(new Error('请输入合法的邮箱'))
}
// 验证手机号的规则
var checkMobile = (rule, value, cb) => {
// 验证手机号的正则表达式
const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
if (regMobile.test(value)) {
return cb()
}
cb(new Error('请输入合法的手机号'))
}
- Use validation rules
email:[
{
required: true, message: '请输入用户名', trigger: 'blur' },
{
min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' },
{
validator: checkEmail, trigger: 'blur'}
],
mobile:[
{
required: true, message: '请输入用户名', trigger: 'blur' },
{
min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' },
{
validator: checkMobile, trigger: 'blur'}
]
- After closing
Dialog
the dialog box, the data in the input box should be reset- To
Dialog
add the built-inclose
event function - By
Form
setting the form andref
callingresetFields
the method, the reset of the form is realized
- To
- Add pre-verification
Dialog
to the button in the click dialog box , and then send the network request to add the user确定
this.$refs.userDialogRef.validate(valid =>{
//首先判断是否校验通过
//如果通过了,才发送网络
})
- After the dialog box for editing user information
Dialog
is closed, the reset form method is also called. At this time, the reset is not to reset the form information, but to reset the validation rules. It is different from the form reset inside the user informationDialog
. - To implement the delete button, you need to use
elementUI
the prompt component in the bullet boxMessage Box
, or it is not so much a bullet box component as it is a method call
Roles component
- Through the attribute
<el-table-column type="expand" >
of , you can create a new expanded columnexpand
in the table - In expanded columns
Layout
, the way the data is laid out is achieved through layout, and no ready-made components are used because there are no components available - in the expanded column
- First use the scope slot to get the data of the current row (template)
- Then there is a row label
el-row
, and loop through this labelv-for
to render the first-level permission datael-row
There are two columns in the row labelel-col
The specific data is rendered in the first column label- In the second column label
el-col
, a row label is nested insideel-row
- There are two column labels in the second row label , and the data is rendered by passing
el-row
in the second row labelel-row
v-for
el-col
The specific data is rendered in the first column label , that is, the data of the secondary authority- In the second column label
el-col
, the label is placedel-tag
, andv-for
the data of the third-level authority is rendered directly in the form of changing the label
el-tag
Add attributes to each labelclosable
, so thatel-tag
the delete icon of the label can be realized- After clicking
el-tag
the delete icon of the label,MessageBox
a pop-up box will pop up, and it will be used to display whether to confirm the deletion of the permission el-tag
In the process of adding the upper border line and the lower border line to the labels of each row , dynamic binding class names , class name arrays and ternary expressions are used to determine whether to add class names- Centering the labels of the first-level authority and the second-level authority
el-tag
is also achieved by dynamically binding the class name .