*IT axios:足迹第八十步:vue与路由

本文详细介绍了在web app开发中使用Vue进行路由配置和axios请求的方法。从编写login.vue开始,逐步讲解如何设置导航卫士、发送axios请求、构建index.html、main.js的组织结构,包括App.vue和store/index.js的编写。此外,还涵盖了菜单框和导航框的创建及在路由中的应用。
摘要由CSDN通过智能技术生成

1)如何写login.vue

<template>
<el-form size="medium" style="padding-bottom: 1rem;">
  <div class="login-container">
  <table class="table_border" style="width:100%">
    <tbody>
		   <tr >
		      <td class="t_left">
		        <span>用户名&nbsp;</span>
		        <span style="color:red">*</span>
		      </td>
		      <td>
		        <el-input style="width:100%;" v-model="username" clearable>
		
		        </el-input>
		      </td>
		    </tr>
		    <tr >
		      <td class="t_left">
		        <span>密码&nbsp;</span>
		        <span style="color:red">*</span>
		      </td>
		      <td>
		        <el-inputstyle="width:100%;" v-model="password" type="password" clearable>
		
		        </el-input>
		      </td>
		    </tr>
        </tbody>
  </table>
  <div class="div_c">
      <el-button icon="el-icon-close"  @click="conso" round>取 消</el-button>
      <el-button icon="el-icon-check"type="primary"@click="submitAdd" round>确 认</el-button>
    </div>
  </div>
</el-form>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'

export default {
  name: 'login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loading: false
    }
  },
  methods: {
 
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: '/system' }) // 登录后重定向到首页
          }).catch(() => {
            this.loading = false
            this.$message('用户名或密码错误')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px #293444 inset !important;
    -webkit-text-fill-color: #fff !important;
  }
  input {
    background: transparent;
    border: 0px;
    -webkit-appearance: none;
    border-radius: 0px;
    padding: 12px 5px 12px 15px;
    color: $light_gray;
    height: 47px;
  }

}
</style>

2)如何写导航卫士

import router from './router'
import store from './store'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'// Progress 进度条样式
import { Message } from 'element-ui'
import { getToken } from '@/utils/auth' // 验权

const whiteList = ['/login'] // 不重定向白名单
router.beforeEach((to, from, next) => {
  NProgress.start()
if (getToken()) {
    if (to.path === '/') {
      next({ path: '/login' })
    } else {
      if (store.getters.roles.length === 0) {
        // store.dispatch('GetInfo').then(res => { // 拉取用户信息
        next()
        // }).catch(() => {
        //   store.dispatch('FedLogOut').then(() => {
        //     Message.error('验证失败,请重新登录')
        //     next({ path: '/login' })
        //   })
        // })
      } else {
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})

3)如何用axios发送请求

import axios from 'axios'
import store from '../store'
import { getToken } from '@/utils/auth'

// 创建axios实例
const service = axios.create({
  baseURL: 'http://111.xxx.xxx.xxx:21086',
  timeout: 15000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(
  config => {
    if (store.getters.token) {
      config.headers = {
        // 判断是否存在token,如果存在的话,则每个http header都加上token
        authorization: 'Bearer ' + getToken(),
        'Content-Type': 'application/json;charset=UTF-8',
        Accept: 'application/json, */*'
      }
    } else {
      config.headers = {
        'Content-Type': 'application/json;charset=UTF-8',
        Accept: 'application/json, text/plain, */*'
      }
    }
    return config
  },
  error => {
    console.log(error) // for debug
    Promise.reject(error)
  }
)

// respone拦截器
service.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    console.log('err' + error) // for debug
    return Promise.reject(error)
  }
)

export default service

4)如何写index.html:包含 <div id="app">

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>单点登录系统</title>
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

5)如何写main.js:引入App.vue与store.js方法

import Vue from 'vue'
import 'babel-polyfill'

import 'normalize.css/normalize.css'// A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// import locale from 'element-ui/lib/locale/lang/en'

import '@/styles/index.scss' // global css

import App from './App'
import router from './router'
import store from './store'

import i18n from './lang' // Internationalization
import '@/icons' // icon
import '@/permission' // permission control

// Vue.use(ElementUI, { locale })
Vue.use(ElementUI, {
  size: 'medium', // set element-ui default size
  i18n: (key, value) => i18n.t(key, value)
})
new Vue({
  el: '#app',
  router,
  store,
  i18n,
  template: '<App/>',
  components: { App }
})

5.1)如何写App.vue:引入<router-view>

<template>
  <div id="app" style="height: 100%;">
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'app',
  data() {
    return {
    }
  }
}
</script>
<style>
html,
body {
  height: 100%;
  width: 100%;
  overflow-x: hidden;
}
</style>

5.2)如何写store下index.js:引入所有页面配套的js方法

import Vue from 'vue'
import Vuex from 'vuex'
import app from './modules/app'
import user from './modules/user'
import systemUser from './modules/system/user'
import tagsView from './modules/tagsView'
import getters from './getters'
import orginfo from './modules/system/orginfo'
import positioninfo from './modules/system/positioninfo'
import userinfo from './modules/system/userinfo'
import appsystem from './modules/system/appsystem'
import modelBasic from './modules/subsys/model/modelBasic'
import modelPermission from './modules/subsys/model/modelPermission'
import userPermission from './modules/subsys/userPermission'
import userPermissionAdmin from './modules/subsys/userPermissionAdmin'
import userRoleAdmin from './modules/subsys/userRoleAdmin'
import userLog from './modules/subsys/userLog'
import adminLog from './modules/subsys/adminLog'
import orgTree from './modules/orgTree'
import modelTree from './modules/modelTree'

Vue.use(Vuex)

const store = new Vuex.Store({
  modules: {
    app,
    tagsView,
    user,
    systemUser,
    orginfo,
    positioninfo,
    userinfo,
    appsystem,
    modelBasic,
    modelPermission,
    userPermission,
    userPermissionAdmin,
    userRoleAdmin,
    userLog,
    adminLog,
    orgTree,
    modelTree
  },
  getters
})

export default store

6)如何写菜单框

在这里插入图片描述

7)如何在路由js中引入菜单框

在这里插入图片描述

8)如何引入导航框

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值