参考B站自学用Vue,element开发电商管理系统记录

初始化
1 npm install
2 node app.js
3 postman跑接口

代码
终端里git status 查看工作区域是否干净
创建新分支 git checkout -b login
查看分支 git branch

创建login.vue
路由里import:import Login from '../components/Login.vue'
创建路径:

index.js里配置

Vue.use(VueRouter)
export default new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login }
  ]
})

APP.Vue里渲染
<router-view></router-view>

按需导入

import { Button, Form, FormItem, Input } from 'element-ui'
Vue.use(Button)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)

表单
给表单绑定一个数据对象loginForm,在script里定义:

data(){
    return {
      loginForm: {
          username: 'gyd',
          password: '1234'
      }
    }
  }

接下来给每个具体的表单项双向绑定到对象的某个属性

校验表单数据是否合法:

  1. 给form绑定一个规则校验对象
  2. 在规则校验对象中定义几个校验属性
  3. 在item中用prop指定对应的校验规则

重置表单功能:

  • 表单添加ref=‘loginFormRef’,(这个名字作用为了能获取是这个表单)
  • 重置按钮加方法resetLoginForm
  • methods里
 resetLoginForm(){
      this.$refs.loginFormRef.resetFields();
    }

登录

  • 给登录按钮绑定单击事件login
  • 导入弹框然后挂载 Vue.prototype.$message = Message
  • methods定义:
login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const {data:res} = await this.$http.post("login",this.loginForm);
        *//(↑解构赋值)*               *(↑获取服务端返回数据)*
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  • 新建Home.vue,import进router/index.js,在route里添加path

路由导航守卫

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

以上完成登录页面,用git同步代码到码云
终端里 git status

  1. git add .
  2. git commit -m “完成登录功能”
  3. git push 如果是第一次提交新分支的话继续往下
  4. git push -u origin login(创建新分支)
  5. git checkout master
  6. git merge login(切换到主分支再合并)
  7. git push(将本地最新的master分支上传)

拦截器一般做什么?

1. 修改请求头的一些配置项

2. 给请求的过程添加一些请求的图标

3. 给请求添加参数
main.js

axios.interceptors.request.use(config => {
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
})

侧边栏的折叠与展开
collapse,动态绑定aside的width 写个点击iscollapse取反的方法就ok
代码:

<el-aside :width="isCollapse ? '64px' : '200px'">
<div class="toggle-button" @click="isCollapse = !isCollapse">
{{ isCollapse? "展开>>" : "<<折叠" }}
</div>
</el-aside>
//初始定义isCollapse为false

拼接路由

   {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    //(后续用到)
    children: [{ path: '/welcome', component: Welcome },
      { path: '/users', component: Users }
    ]
  }

制表

  • el-row里:gutter 间距
  • el-rcol里:span 占位符的宽度
  • el-table里:data绑定数据源
  • el-table-column里prop数据项,label标签名

其他校验规则

  • data里定义一个变量校验规则 checkEmail
var checkEmail = (rule, value, cb) => {
        const regEmail = /规则xxx/
        if( regEmail.test(value) ) return cb()//校验成功
        return cb(new Error('请输入合法邮箱'))//失败
      }
  • 再在校验规则名里加上
{ validator: checkEmail, trigger: 'blur', message: '邮箱不合法'}

校验表单合法

this.$refs.addForm.validate( valid => {
          if!valid) return//不合法
         //接下来可完成请求
        } )

请求编辑用户

// 挂包
import axios from 'axios'
Vue.prototype.$http = axios

用 this.$http.put(路径,参数)

editUserInfo() {
        this.$refs.editFormRef.validate(async valid => {
          if (!valid) return
          // 开始完成用户请求
          const {data:res} = await this.$http.put('users/' + this.editForm.id , this.editForm)
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          this.editDialogVisible = false
          this.getUsersList()
          return this.$message.success(res.meta.msg)
        })
      },

confirm弹窗
挂载
Vue.prototype.$confirm = MessageBox.confirm

const result = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).catch(err >= err)
                //result的值此时有confirm和cancel 用if判断后续操作

以上完成用户列表功能,开始同步至码云

  1. git终端操作命令
  2. git checkout -b user
  3. git branch
  4. git status
  5. git add .
  6. git commit -m ‘完成用户功能开发’
  7. git push -u origin user (创建新分支)
  8. git checkout master (切换到主分支再合并)
  9. git merge user(合并)
  10. git push(将本地最新的master分支上传)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值