赞
踩
// 配置请求的根路径
axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1
// 使用get方法需要有第二个参数
async getUserList() {
const {data: res} = await this.$http.get('users', { params: this.queryInfo})
if (res.meta.status !== 200) {
return this.$message.error('获取用户列表失败!')
}
this.userlist = res.data.users
this.total = res.data.total
}
router.beforeEach((to, from, next) => {
// to 将要访问的路径
// from 代表从那个路径跳转而来
// next 是一个函数,表示放行
// next() 放行 next('/login') 强制跳转
if (to.path === '/login') return next();
// 获取token
const tokenStr = window.sessionStorage.getItem('token')
if (!tokenStr) return next('/login')
next()
})
> 栗子:添加 // :model => 定义添加用户的模板 // :rules => 指定表单验证规则 <el-form :model="addForm" :rules="addFormRules" ref="addUserFormRef" label-width="70px"> // prop => 写入到预先定义的添加模板,在:model内定义 <el-form-item label="用户名" prop="username"> <el-input v-model="addForm.username"></el-input> </el-form-item> <el-form-item label="密码" prop="password"> <el-input v-model="addForm.password"></el-input> </el-form-item> <el-form-item label="邮箱" prop="email"> <el-input v-model="addForm.email"></el-input> </el-form-item> <el-form-item label="手机" prop="mobile"> <el-input v-model="addForm.mobile"></el-input> </el-form-item> </el-form> // 定义添加模板如下 // addForm: { // username: '', // password: '', // email: '', // mobile: '' // }
> 栗子:修改
// 修改与添加不同的地方在于:model绑定的对象为一个空对象
// 其余地方基本相同
<el-form :model="editForm" :rules="editFormRules" ref="editUserFormRef" label-width="70px">
<el-form-item label="用户名">
<el-input v-model="editForm.username" disabled></el-input>
</el-form-item>
<el-form-item label="邮箱" prop="email">
<el-input v-model="editForm.email"></el-input>
</el-form-item>
<el-form-item label="手机" prop="mobile">
<el-input v-model="editForm.mobile"></el-input>
</el-form-item>
</el-form>
$ git clone 仓库地址
$ git status
$ git add .
$ git commit -m ‘初始化项目’
$ git push
$ git checkout -b 分支名
$ git checkout 分支名
$ git branch
$ git push -u origin 保存的分支名
$ git merge 需要合并的分支名
// 通过ref找到要重置的form表单,调用 resetFields() 方法
this.$refs.addCateFormRef.resetFields()
split() // 该方法用于把一个字符串分割成字符串数组
resetFields() // 该方法可用于关闭对话框时重置对话框信息
$nextTick() // $nextTick方法的作用,就是当页面上元素被重新渲染之后,才会指定回调函数中的代码
this.$refs.saveTagInput.$refs.input.focus() // 在$nextTick方法内调用可以让切换出来的输入框获得焦点
// html
<el-table :data="userlist" border stripe> <el-table-column label="#" type="index"></el-table-column> <el-table-column label="姓名" prop="username"></el-table-column> <el-table-column label="邮箱" prop="email"></el-table-column> <el-table-column label="电话" prop="mobile"></el-table-column> <el-table-column label="角色" prop="role_name"></el-table-column> <el-table-column label="状态" width="150px"> <template v-slot="scope"> <el-switch v-model="scope.row.mg_state" @change="userStateChanged(scope.row)"> </el-switch> </template> </el-table-column> <el-table-column label="操作" width="180px"> <template v-slot="scope"> <!-- 修改按钮--> <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button> <!-- 删除按钮--> <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUserById(scope.row.id)"></el-button> <!-- 分配角色按钮--> <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false"> <el-button type="warning" icon="el-icon-setting" size="mini" @click="setRole(scope.row)"></el-button> </el-tooltip> </template> </el-table-column> </el-table>
js
// data userlist: [] // js async getUserList() { const {data: res} = await this.$http.get('users', { params: this.queryInfo}) if (res.meta.status !== 200) { return this.$message.error('获取用户列表失败!') } this.userlist = res.data.users this.total = res.data.total }, // 监听pagesize改变的事件 handleSizeChange(newSize) { this.queryInfo.pagesize = newSize this.getUserList() }
// html
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="queryInfo.pagenum"
:page-sizes="[5, 10, 15]"
:page-size="queryInfo.pagesize"
layout="total, sizes, prev, pager, next, jumper"
:total="total"
background>
</el-pagination>
// data queryInfo: { query: '', pagenum: 1, pagesize: 5 }, total: 0, orderList: [] // js handleSizeChange(newSize) { this.queryInfo.pagesize = newSize this.getOrderList() }, handleCurrentChange(newPage) { this.queryInfo.pagenum = newPage this.getOrderList() }
// html
<el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList">
<el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
</el-input>
// data
queryInfo: {
query: '',
pagenum: 1,
pagesize: 2
},
// js
async getUserList() {
const {data: res} = await this.$http.get('users', { params: this.queryInfo})
if (res.meta.status !== 200) {
return this.$message.error('获取用户列表失败!')
}
this.userlist = res.data.users
this.total = res.data.total
}
// 对话框html 按钮添加 @click 单击事件,将 editDialogVisible 改为true达到展示的效果 <el-dialog title="修改用户" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed"> <el-form :model="editForm" :rules="editFormRules" ref="editUserFormRef" label-width="70px"> <el-form-item label="用户名"> <el-input v-model="editForm.username" disabled></el-input> </el-form-item> <el-form-item label="邮箱" prop="email"> <el-input v-model="editForm.email"></el-input> </el-form-item> <el-form-item label="手机" prop="mobile"> <el-input v-model="editForm.mobile"></el-input> </el-form-item> </el-form> <span slot="footer" class="dialog-footer"> <el-button @click="editDialogVisible = false">取 消</el-button> <el-button type="primary" @click="editUserInfo">确 定</el-button> </span> </el-dialog>
// data editForm: {} // 用户信息对象 // 表单验证 editFormRules: { email: [ {required: true, message: '请输入密码', trigger: 'blur'}, {validator: checkEmail, trigger: 'blur'} ], mobile: [ {required: true, message: '请输入手机号码', trigger: 'blur'}, {validator: checkMobile, trigger: 'blur'} ] } // js async showEditDialog(id) { // console.log(id); const {data: res} = await this.$http.get('users/' + id) // get方法获取数据 if (res.meta.status !== 200) { return this.$message.error('查询用户信息失败!') } this.editForm = res.data this.editDialogVisible = true // 将对话框修改未true显示 }
editUserInfo() {
this.$refs.editUserFormRef.validate(async vaild => { // 提交前调用 validate 方法预验证表单
if (!vaild) return
// 发起修改用户信息的数据请求
const {data: res} = await this.$http.put('users/' + this.editForm.id, this.editForm)
if (res.meta.status !== 200) {
this.$message.error('修改用户信息失败!')
}
this.$message.success('修改用户信息成功!')
this.editDialogVisible = false
this.getUserList()
})
},
// html
// 按钮添加 @click 调用删除方法
// js async removeUserById(id) { const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' } ).catch(err => err) console.log(id); // 如果用户确认删除, 则返回值为字符串 confirm // 如果用户取消了删除,则返回值为字符串 cancel if (confirmResult !== 'confirm') { return this.$message.info('已取消删除!') } const {data: res} = await this.$http.delete('users/' + id) if (res.meta.status !== 200) { this.$message.error('删除用户失败!') } else if (res.meta.status == 200) { this.$message.success('删除用户成功!') } this.getUserList() }
// html <!-- 添加对话框--> <el-dialog title="添加用户" :visible.sync="addUserDialogVisible" width="50%" @close="addDialogClosed"> <!-- 内容主题区域--> <el-form :model="addForm" :rules="addFormRules" ref="addUserFormRef" label-width="70px"> <el-form-item label="用户名" prop="username"> <el-input v-model="addForm.username"></el-input> </el-form-item> <el-form-item label="密码" prop="password"> <el-input v-model="addForm.password"></el-input> </el-form-item> <el-form-item label="邮箱" prop="email"> <el-input v-model="addForm.email"></el-input> </el-form-item> <el-form-item label="手机" prop="mobile"> <el-input v-model="addForm.mobile"></el-input> </el-form-item> </el-form> <!-- 底部区域--> <span slot="footer" class="dialog-footer"> <el-button @click="addUserDialogVisible = false">取 消</el-button> <el-button type="primary" @click="addUser">确 定</el-button> </span> </el-dialog>
// data addForm: { username: '', password: '', email: '', mobile: '' } // js addUser() { this.$refs.addUserFormRef.validate(async valid => { if (!valid) return // 可以发起添加用户的网络请求 const {data: res} = await this.$http.post('users', this.addForm) if (res.meta.status !== 201) { this.$message.error('添加用户失败!') } this.$message.success('添加用户成功!') // 隐藏添加用户的对话框 this.addUserDialogVisible = false // 重新获取用户列表 this.getUserList() }) }
// 判定是运行serve服务器或build打包,决定是否去掉console语句
const prodPlugins = []
if (process.env.NODE_ENV === 'production') {
prodPlugins.push('transform-remove-console')
}
// plugins配置内加上
>> ...prodPlugins
// 将原来导入组件imort改成
const 命名 => () => import('路径')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。