Vue.js、node.js基本操作

vue界面登录

Login.vue代码如下,去访问数据库

<!-- 定义模版对象 -->
<template>
    <div class="login_container">
      <div class="login_box">
        <!-- 头像区域-->
        <div class="avatar_box">
          <img src="../assets/logo.png" alt="VUE图片" />
        </div>

        <!-- 登陆表单区域
              ref代表当前表单引用对象
             :model 是表格中封装的对象
        -->
        <el-form ref="loginFormRef" label-width="0" class="login_form" :model="loginForm" :rules="rules" >
          <el-form-item prop="username">
            <el-input  prefix-icon="iconfont iconuser" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input  prefix-icon="iconfont iconsuo" type="password" v-model="loginForm.password"></el-input>
          </el-form-item>
          <el-form-item class="btns">
             <el-button type="primary" @click="login">登录</el-button>
              <el-button type="info" @click="resetBtn">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
</template>

<!-- 定义JS变量 -->
<script>
export default {
  data(){
    return {
      loginForm: {
        username: '',
        password: ''
      },
      //定义表单的验证规则
      rules: {
         //定义校验用户名
         username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max:30, message: '长度在 330 个字符', trigger: 'blur' }
         ],
         //定义校验密码
         password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max:30, message: '长度在 330 个字符', trigger: 'blur' }
         ]
      }
    }
  },
  methods: {
    resetBtn(){
      //this对象代表当前组件对象
      //console.log(this)
      //实现数据重置
      this.$refs.loginFormRef.resetFields()
    },
    login(){
      //获取表单对象之后进行数据校验
      //valid 表示校验的结果 true表示通过  false表示失败
      this.$refs.loginFormRef.validate(async valid => {
         //如果没有完成校验则直接返回
         if(!valid) return

         //如果校验成功,则发起ajax请求
        const {data: result} = await this.$http.post('/user/login',this.loginForm)//JSON.stringify(this.loginForm)
        if(result.status !== 200) return this.$message.error("用户登录失败")
        this.$message.success("用户登陆成功")

        //获取用户token信息
        let token = result.data
        window.sessionStorage.setItem("token",token)

        //用户登录成功之后,跳转到home页面
        this.$router.push("/home")
      })
    }
  }
}
</script>

<!-- 防止组件冲突 -->
<style lang="less" scoped>
.login_container{
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #FFFFFF;
  /* 设定圆弧角*/
  border-radius: 10px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #EEEEEE;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #DDDDDD; /* 添加阴影*/
    position: absolute;           /* 绝对定位*/
    left: 50%;                    /* 距离左侧50%*/
    transform: translate(-50%,-50%);   /*回调50%*/
    background-color: #FFFFFF;
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #EEEEEE;
    }
  }

  .btns {
    display: flex;
    justify-content: flex-end;

  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>

路由导航守卫实现
//配置路由对象
const router = new VueRouter({
  routes
})

/* 配置路由导航守卫 控制权限
  1.to 要跳转的网址
  2.from 请求从哪里来
  3.next 回调函数  放行/跳转
 */
router.beforeEach((to,from,next) => {
  //1.如果用户访问 /login  请求应该放行 终止程序
  if(to.path === '/login') {
    return next()
  }

  //2.如果用户访问不是login 则需要校验是否登录 检查是否有token
  let token = window.sessionStorage.getItem('token')
  if(token !== null && token.length > 0){
    return next()
  }else {
    //没有token 应该跳转到登录页面 "/login"
    return next("/login")
  }
})

gitee

使用Axios的Get传单一值

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用Axios传单一值</title>
    <script src="vue.js"></script>
    <script src="axios.min.js"></script>
    
</head>

<body>
    <div id="app">
       <input type="text" v-model="uid" placeholder="请输入用户编号">
       <button @click="getUserById()">查询</button><br>
        电话:{{user.telephone}}<br>
        密码:{{user.password}}
    </div>
</body>

</html>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            url: "http://localhost:8080/select",
            user:{},
            uid:""
        },
        methods: {
            //获取用户信息
            getUserById() {
                axios.get(this.url+"?uid="+this.uid).then(res=>{
                   this.user=res.data.data.user;
                })
            }
        },
    })
</script>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用Axios传单一值</title>
    <script src="vue.js"></script>
    <script src="axios.min.js"></script>
    
</head>

<body>
    <div id="app">
       <input type="text" v-model="uid" placeholder="请输入用户编号">
       <button @click="getUserById()">查询</button><br>
        电话:{{user.telephone}}<br>
        密码:{{user.password}}
    </div>


</body>

</html>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            url: "http://localhost:8080/select",
            user:{},
            uid:""
        },
        methods: {
            //获取用户信息
            getUserById() {
                axios.get(this.url,{
                    params:{
                        uid:this.uid
                    }
                }).then(res=>{
                   this.user=res.data.data.user;
                })
            }
        },
    })
</script>

Axios使用GET数据Restful风格

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用Axios传值Restful风格</title>
    <script src="vue.js"></script>
    <script src="axios.min.js"></script>
    
</head>

<body>
    <div id="app">
       <input type="text" v-model="uid" placeholder="请输入用户编号">
       <button @click="getUserById()">查询</button><br>
        电话:{{user.telephone}}<br>
        密码:{{user.password}}
    </div>


</body>

</html>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            uid:"",
            // url:`http://localhost:8080/users/${this.uid}`,
            user:{}
        },
        methods: {
            //获取用户信息
            getUserById() {
                var url=`http://localhost:8080/users/${this.uid}`;
               axios.get(url).then(res=>{
                   this.user=res.data.data.user;
               })
            }
        },
    })
</script>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用Axios传值Restful风格</title>
    <script src="vue.js"></script>
    <script src="axios.min.js"></script>
    
</head>

<body>
    <div id="app">
       <input type="text" v-model="uid" placeholder="请输入用户编号">
       <button @click="getUserById()">查询</button><br>
        电话:{{user.telephone}}<br>
        密码:{{user.password}}
    </div>


</body>

</html>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            uid:"",
            url:`http://localhost:8080/users`,
            user:{}
        },
        methods: {
            //获取用户信息
            getUserById() {
               axios({
                   url:this.url+"/"+this.uid,
                   method:"Get"
               }).then(res=>{
                   this.user=res.data.data.user;
               })
                   
            }
        },
    })
</script>

使用Axios的Get标准形式

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用Axios传单一值</title>
    <script src="vue.js"></script>
    <script src="axios.min.js"></script>
    
</head>

<body>
    <div id="app">
       <input type="text" v-model="uid" placeholder="请输入用户编号">
       <button @click="getUserById()">查询</button><br>
        电话:{{user.telephone}}<br>
        密码:{{user.password}}
    </div>


</body>

</html>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            url: "http://localhost:8080/select",
            user:{},
            uid:""
        },
        methods: {
            //获取用户信息
            getUserById() {
               axios({
                   url:this.url,
                   method:"Get",
                   params:{
                       uid:this.uid
                   }
               }).then(res=>{
                  this.user=res.data.data.user;
               })
            }
        },
    })
</script>

使用Axios的Get完成后端數據集合顯示

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用Axios完成后端交互</title>
    <script src="vue.js"></script>
    <script src="axios.min.js"></script>
    <style>
        td{
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="app">
        <button @click="getUserList()">获取所有用户信息</button>
        <div>
            <table border="1px" cellpadding="3px" cellspacing="2px">
                <tr>
                    <td>用户id</td>
                    <td>电话</td>
                    <td>密码</td>
                    <td>姓名</td>
                    <td>生日</td>
                    <td>创建日期</td>
                    <td>头像</td>
                    <td>是否可用</td>
                    <td>部门名称</td>
                </tr>
                <tr v-for="user in users">
                    <td>{{user.uid}}</td>
                    <td>{{user.telephone}}</td>
                    <td>{{user.password}}</td>
                    <td>{{user.realname}}</td>
                    <td>{{user.birthday}}</td>
                    <td>{{user.createdate}}</td>
                    <td>{{user.headimg}}</td>
                    <td>{{user.available}}</td>
                    <td>{{user.dept.deptname}}</td>
                </tr>
            </table>
        </div>
    </div>


</body>

</html>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            url: "http://localhost:8080/users",
            users:[]
        },
        methods: {
            //获取所有用户信息
            getUserList() {
                axios.get(this.url).then(res => {
                    console.log(res);
                   this.users=res.data.data.users;
                }).catch(res => {
                    Promise.reject();
                })
            }
        },
    })
</script>

Axios使用Post新增数据Restful风格

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Axios使用Post新增数据Restful风格</title>
    <script src="vue.js"></script>
    <script src="axios.min.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" placeholder="请输入部门名称" v-model="dept.deptname">
        <input type="text" placeholder="请输入部门人数" v-model="dept.deptnum">
        <input type="text" placeholder="请输入部门描述" v-model="dept.deptdesc">
        <button @click="addDept">新增部门信息</button>
    </div>
</body>

</html>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            dept: {
                deptid: "",
                deptname: "",
                deptnum: "",
                deptdesc: ""
            },
           url:"http://localhost:8080/depts"
        },
        methods: {
            addDept(){
                axios({
                    url:this.url,
                    method:"POST",
                    data:this.dept
                }).then(res=>{
                    console.log(res);
                })
            }

        },
    })
</script>

Axios使用Delete删除数据Restful风格

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Axios使用Delete删除数据Restful风格</title>
    <script src="vue.js"></script>
    <script src="axios.min.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" placeholder="请输入部门编号" v-model="deptid">
        <button @click="deleteDept">删除部门信息</button>
    </div>
</body>

</html>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            url:"http://localhost:8080/depts",
            deptid:""
        },
        methods: {
            deleteDept() {
                axios({
                    url: this.url,
                    method:"DELETE",
                    params:{
                        deptid:this.deptid
                    }
                }).then(res => {
                    console.log(res);
                })
            }
        }
    });
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值