首页 > 其他分享 >前端vue2+vue-router3+ElementUI+axios综合使用代码教程登录页案例

前端vue2+vue-router3+ElementUI+axios综合使用代码教程登录页案例

时间:2022-11-05 21:57:07浏览次数:40  
标签:vue ElementUI router3 signin axios background import message

为了测试vue2+vue-router3路由+ElementUI界面+axios网络HTTP请求的基本使用情况

专门编写一个登录页面进行demo验证

 

 

依赖情况package.json

{
  "name": "default",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build"
  },
  "dependencies": {
    "axios": "^1.1.3",
    "core-js": "^2.6.5",
    "element-ui": "^2.15.10",
    "vue": "^2.6.10",
    "vue-router": "^3.6.5"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.8.0",
    "@vue/cli-service": "^3.8.0",
    "vue-template-compiler": "^2.6.10"
  }
}

main.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import ElementUi from 'element-ui'

import 'element-ui/lib/theme-chalk/index.css';

import Login from './components/Login.vue'
import App from './App.vue'
//Vue.config.productionTip = false
Vue.prototype.ApiHost = 'https://gofly.v1kf.com';

Vue.use(VueRouter)
Vue.use(ElementUi)

const routes = [
  { path: '/login', component: Login },
]

const router = new VueRouter({
  routes
})

const app = new Vue({
    render: h => h(App),
    router
}).$mount('#app')

App.vue

<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'app',
}
</script>

./components/Login.vue

<template>
    <div>
<div class="loginBox">
            <div class="loginBg"></div>
            <div  class="signin">
                <div class="loginHtml">
                    <h1 class="loginTitle">系统登录</h1>
                    <el-form :model="kefuForm"  :rules="rules" ref="kefuForm">
                        <el-form-item  prop="username">
                            <el-input v-model="kefuForm.username" placeholder="账号"></el-input>
                        </el-form-item>
                        <el-form-item  prop="password">
                            <el-input show-password v-on:keyup.enter.native="kefuLogin('kefuForm')" v-model="kefuForm.password" placeholder="密码"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" style="width: 100%" :loading="loading" @click="kefuLogin('kefuForm','pc')">登录</el-button>
                        </el-form-item>
                    </el-form>
                    <div class="clear"></div>
                </div>
                <p class="copyright"> </p>
            </div>
        </div>
    </div>
</template>

<script>
    import axios from 'axios'
    export default {
      name: 'Login',
        data() {
          return {
            kefuForm: {
                username:"",
                password:"",
            },
            rules: {},
            loading:false,
          }
        },
        methods: {
          kefuLogin() {
            var _this=this;
            this.loading=true;
            axios.post(this.ApiHost+'/check', {
                username: this.kefuForm.username,      
                password: this.kefuForm.password
              })
              .then(function (response) {
                  _this.loading=false;
                  console.log(response);
                  var code=response.data.code;
                  var msg=response.data.msg;
                  if(code!=200){
                      _this.$message({
                          message: msg,
                          type: 'error'
                      });
                      return;
                  }
                  _this.$message({
                      message: msg,
                      type: 'success'
                  });
              })
              .catch(function (error) {
                  _this.loading=false;
                  console.log(error);
                  _this.$message({
                      message: error.message,
                      type: 'error'
                  });
              });
          }
        }

    }
</script>

<style>
    body {
        margin: 0;
        padding: 0;
    }
    .loginBg{
        width: 100%;
        height: 100%;
        position: fixed;
        top: 0px;
        left: 0px;
        background: rgba(0, 0, 0, 0.5);
        opacity: 0.5;
        z-index: 0;
    }
    .loginBox{
        width: 100%;
        height: 100%;
        background: url("/static/images/loginBg.jpg") no-repeat;
        background-size: cover;
        position: absolute;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .clear{clear: both;}
    .signin {
        width: 460px;
        padding: 25px;
        background: #fff;
        box-shadow: 0 25px 30px -13px rgba(40,40,40,.4);
        border-radius: 6px;
        z-index: 1;
    }
    .signin h1,.signin h2,.signin .copyright{
        font-weight: normal;
        color: #4d627b;
        text-align: center;
    }
    .signin .loginTitle{
        font-size: 24px;
        margin: 15px 0px;
    }
    .signin .loginDesc{
        font-size: 14px;
        margin-bottom: 15px;
    }
    .signin .loginDesc .regBtn{
        float: left;
    }
    .signin .loginDesc a{
        text-decoration: none;
        cursor: pointer;
        color: #409EFF;
    }
    .signin .copyright{
        font-size: 12px;
    }
    @media (max-width: 768px) {
        body{
            background-color: #fff;
        }
        .signin{
            width: 80%;
            background-color: #fff;
            box-shadow:none;
        }
    }
</style>

 

标签:vue,ElementUI,router3,signin,axios,background,import,message
From: https://www.cnblogs.com/taoshihan/p/16861430.html

相关文章

  • 读取数组树下的某值,并返回其父级下的任何值 vue
    1//遍历树获取对应id的项中的值2queryTree(tree,value){3letstark=[];4stark=stark.concat(tree);5while(stark.length)......
  • 8.Vue中的组件化开发
    1.组件化概念在开发中,我们将页面的某一部分功能编写成一个组件,以后直接在页面中引用即可,如导航栏,轮播图,等,封装成一个个的组件,以后直接使用即可Vue的官方也推荐我们组件化......
  • 7.Vue常用属性
    1.data:数据属性在之前的学习中我们已经了解到了data,属性中存放的就是js变量<script>newVue({el:'#app',//datadata:{......
  • vue如何过滤出一个数组中不包含另一个数组的数据
    data里面定义测试数组arr1:[1,3,5,7,11,0],arr2:[1,11],arrres:[]然后过滤一下for(letoneofthis.arr1){if(this.arr2.indexOf(one)==-1)......
  • 6.Vue中与后端的交互
    浏览器的同源策略:不允许向不同源(IP+Port)发送请求或者获取数据解决跨域的方式:1.后端解决:在响应头中添加"Access-Control-Allow-Origin=*"2.前端解决3.服务器代理......
  • 【Vue】可编辑表格与三级联动下拉
    需求是给员工分配岗位,设计上是一人多岗的存在...单位—— 部门—— 岗位 这样的层级功能效果:因为员工可以在不同的单位下任职岗位,所以这个每一个岗位都是一个独立......
  • VUE一些特殊的地方
    1、js引用类型赋值不改变原对象值 在JS中对象的赋值是默认引用赋值的,如果改变了赋值对象原对象也会跟着改变,如果想要复制赋值,那么就需要重新分配对象:vara={'name':......
  • vue+WangEditor+自定义上传图片
     1<divclass="content">2<pname="editor"id="editor"ref="editor"style="z-index:-1"></p>3<el-inputid="in"type="hidden"></el......
  • 在Vue中使用Swiper轮播图、同时解决点击轮播图左右切换按钮不生效的问题、同时将轮播
    轮播图左右的切换按钮、如果点击没有反应,控制台也没有报错。很大可能是==版本问题==。如果不指定版本信息、默认安装的是最新的版本。版本过高或者过低都有可能导致无效。......
  • 不使用hbuilderx创建 基于vue3 + vite的uniapp
    创建以javascript开发的工程npxdegitdcloudio/uni-preset-vue#vitemy-vue3-project创建以typescript开发的工程npxdegitdcloudio/uni-preset-vue#vite-tsmy-......