SoFunction
Updated on 2025-04-03

Detailed explanation of Vue Elememt-UI build management backend

Install

I am using vue-cli to initialize the project, with the following command:

npm i -g vue-cli
mkdir my-project && cd my-project
vue init webpack

Modify the file:

...
"dependencies": {
 "vue": "^2.5.2",
 "vue-router": "^3.0.1",
 "element-ui": "^2.0.7", // element-ui
 "axios": "^0.17.1" // http request library}
...

Then execute npm install to install the dependency. If the installation speed is a bit slow, you can try cnpm and find the specific installation and usage by yourself.

A brief introduction to the directory structure of the project:

├─build // Build configuration├─config // Configuration file├─src // vue development source file directory├────assets // css/js file├────components // vue component├────router  //Route├────  // Start the component├──── // Entry file├─static // Static file directory├─test // Test Directory

After that, execute npm run dev in the project root directory, open the browser and enter http://localhost:8080 to view it.

Target

  • Login page, login, logout function
  • Home page, call interface rendering list

routing

The route uses vue-router, please refer to the official documentation

We need two routes here:

src/router/

import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/components/Index'
import Login from '@/components/Login'

(Router)

const routers = new Router({
 routes: [
  {
   path: '/index',
   name: 'index',
   component: Index
  },
  {
   path: '/login',
   name: 'login',
   component: Login
  }
 ]
})

((to, from, next) => {
 if ( !== 'login' && !('token')) {
  next({path: 'login'})
 } else {
  next()
 }
})

export default routers

Login page

src/components/

<template>
 <div class="login">
  <el-form name="aa" :inline="true" label-position="right" label-width="80px">
    <el-form-item label="username">
     <el-input v-model=""></el-input>
    </el-form-item>
    <el-form-item label="password">
     <el-input type="password" v-model=""></el-input>
    </el-form-item>
    <el-form-item label=" ">
     <el-button type="primary" @click="login()">Log in</el-button>
    </el-form-item>
  </el-form>
 </div>
</template>

<script>
import $http from '@/api/'
import config from '@/config'

export default {
 data () {
  return {
   user: {
    name: '',
    password: ''
   }
  }
 },
 mounted: function () {
  var token = ('token')
  if (token) {
   this.$('/index')
  }
 },
 methods: {
  login: function () {
   var data = {
    grant_type: 'password',
    client_id: config.oauth_client_id,
    client_secret: config.oauth_secret,
    username: ,
    password: 
   }
   var _this = this
   $(data).then(function (res) {
    if ( === 200) {
     $(.access_token)
     _this.$message({
      showClose: false,
      message: 'Login successfully',
      type: 'success'
     })
     _this.$('/index')
    } else {
     _this.$message({
      showClose: false,
      message: 'Login failed',
      type: 'error'
     })
    }
   })
  }
 }
}
</script>

<style>
.login{
  width: 300px;
  margin: 100px auto;
  background-color: #ffffff;
  padding: 30px 30px 5px;
  border-radius: 5px;
}
</style>

front page

src/components/

<template>
 <div class="main">
  <el-table
   stripe
   v-loading="loading"
   element-loading-background="#dddddd"
   :data="tableData"
   style="width: 100%">
   <el-table-column
    prop="id"
    label="ID">
   </el-table-column>
   <el-table-column
    prop="name"
    label="name">
   </el-table-column>
  </el-table>
  <el-pagination
   background
   layout="prev, pager, next"
   :total="total"
   class="page"
   @current-change="pageList">
  </el-pagination>
 </div>
</template>

<script>
import $http from '@/api/'

export default {
 data () {
  return {
   tableData: [],
   total: 0,
   loading: false
  }
 },
 mounted: function () {
  ()
 },
 methods: {
  pageList: function (page) {
    = page
   ()
  },
  getList: function () {
   var _this = this
   _this.loading = true
   $().then(function (res) {
    if ( === 200) {
     _this.tableData = 
     _this.total = 
    }
    _this.loading = false
   })
  }
 }
}
</script>

App

src/

<template>
 <div >
  <el-row v-if="token">
   <menus class="left-menu">
    <h3 class="logo"><a href="/" rel="external nofollow" >Admin</a></h3>
   </menus>
   <el-col :span="21" :gutter="0" :offset="3">
    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
     <el-breadcrumb-item :to="{ path: '/' }">front page</el-breadcrumb-item>
     <el-breadcrumb-item class="active">List</el-breadcrumb-item>
    </el-breadcrumb>
    <el-dropdown @command="operate" class="header">
     <img src="/static/image/" />
     <el-dropdown-menu slot="dropdown" :click="true">
      <el-dropdown-item command="/user/profile">Basic information</el-dropdown-item>
      <el-dropdown-item command="/logout">Secure exit</el-dropdown-item>
     </el-dropdown-menu>
    </el-dropdown>
    <router-view/>
   </el-col>
   <el-col :span="21" :gutter="0" :offset="3" class="footer">Copyright © 2017 Flyerboy All Rights Reserved</el-col> 
  </el-row>

  <router-view v-if="!token" />
 </div>
</template>

<script>
import Menus from '@/components/Menu'
export default {
 name: 'App',
 data () {
  return {
   token: false
  }
 },
 mounted: function () {
   = ('token') ? true : false
 },
 watch: {
  '$': function ($newVal, $oldVal) {
    = ('token') ? true : false
  }
 },
 methods: {
   operate: function (command) {
   if (command === '/logout') {
    ('token')
    this.$('login')
   } else {
    this.$(command)
   }
  }
 },
 components: {
  Menus
 }
}
</script>

<style>
body{
 margin: 0;
 padding: 0;
 background-color: #eeeeee;
}
.header{
 position: absolute;
 top: 5px;
 right: 20px;
}
.header img{
 width: 38px;
 height: 38px;
 border-radius: 20px;
 border: 1px solid #aaaaaa;
}
#app {
 font-family: 'Avenir', Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
}
.main{
 padding: 20px;
 min-height: 600px;
 margin-bottom: 20px;
}
.main table{
 background: #ffffff;
}
.left-menu{
 background-color: #33374B;
}
.logo{
 padding: 20px 0 15px 20px;
 font-size: 24px;
 border-bottom: 2px solid #3a8ee6;
}
.logo a{
 color: #ffffff;
 text-decoration: none;
}

.left-menu .el-menu{
 border-right: 0;
}
.breadcrumb{
 line-height: 40px;
 padding: 5px 20px;
 background: #ffffff;
}
.breadcrumb span{
 color: #069;
 font-weight: normal;
}
.breadcrumb .active{
 color: #aaaaaa;
}
.page{
 margin: 20px 0 0;
 margin-left: -10px;
}
.page .el-pager {
 background-color: #ffffff;
}
.el-submenu .el-menu-item{
 padding-left: 60px !important;
}
.footer{
 position: fixed;
 bottom: 0;
 right: 0;
 font-size: 12px;
 color: #888888;
 padding: 15px 20px;
 text-align: center;
 background-color: #ffffff;
 margin-top: 40px;
}
</style>

Calling API

src/api/

import axios from 'axios'
 = 'http://localhost:8000/'
['Content-Type'] = 'application/x-www-form-urlencoded'
['Authorization'] = 'Bearer ' + ('token')


export default {
 setToken: function (token) {
  ('token', token)
  ['Authorization'] = 'Bearer ' + token
 },
 login: function (param) {
  return ('oauth/token', param)
 },
 index: function (params) {
  return ('api/user/list', {
   params: params
  })
 }
}

config

src/ here to configure client_id and secret required for login oauth

export default {
 oauth_client_id: 2,
 oauth_secret: ''
}


src/

import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/'

(ElementUI)

 = false

new Vue({
 el: '#app',
 router,
 components: { App },
 template: '<App/>'
})

api interface

There are two main interfaces, one is api/oauth/token login to get the token interface, and the other is api/user/list.

The first interface uses laravel oauth, and the second interface is directly a simple interface for querying user lists. The detailed explanation will be described in the next article.

The above is all the content about the Vue Elememt-UI build and management backend compiled this time. Thank you for your support.