【Vue】结合ElementUI实现简单数据请求和页面跳转功能

一、准备工作

1、创建一个Vue-cli程序

之前的博客有。各位看官姥爷,可以自查。

2、安装ElementUI

在创建Vue-cli程序的过程中,需要在控制台执行以下指令:

#安装 element-ui

npm i element-ui -S

#安装 SASS 加载器

cnpm install sass-loader node-sass --save-dev

3、准别好的login.vue和main.vue页面

这个是提前准备好的两个login.vue和main.vue页面。

login.vue页面:

<template>
  <div>
    <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
      <h3 class="login-title">欢迎登录</h3>
      <el-form-item label="账号" prop="username">
        <el-input type="text" placeholder="请输入账号" v-model="form.username"/>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" placeholder="请输入密码" v-model="form.password"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" v-on:click="onSubmit('loginForm')">登录</el-button>
      </el-form-item>
    </el-form>

    <el-dialog
      title="温馨提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <span>请输入账号和密码</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      //表单验证,需要在el-form-item 元素中增加prop 属性
      rules: {
        username: [
          {required: true, message: " 账号不可为空", trigger: 'blur'}
        ],
        password: [
          {required: true, message: " 密码不可为空 ", trigger: 'blur'}
        ]
      },
//对话框显示和隐藏
      dialogVisible: false
    }
  },
  methods: {
    onSubmit(formName) {
//为表单绑定验证功能
      this.$refs[formName].validate((valid) => {
        if (valid) {
//使用vue-router路由到指定页面,该方式称之为编程式导航
          this.$router.push("/main/"+this.form.username);
        } else {
          this.dialogVisible = true;
          return false;
        }
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.login-box {
  border: 1px solid #DCDFE6;
  width: 350px;
  margin: 180px auto;
  padding: 35px 35px 15px 35px;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  box-shadow: 0 0 25px #909399;
}

.login-title {
  text-align: center;
  margin: 0 auto 40px auto;
  color: #303133;
}
</style>

main.vue页面:

<template>
  <div>
    <el-container>
      <el-aside width="200px">
        <el-menu :default-openeds="['1']">

          <el-submenu index="1">
            <template slot="title"><i class="el-icon-caret-right"></i>用户管理</template>
            <el-menu-item-group>
              <el-menu-item index="1-1">
<!--                name传组件名更方便 params传递参数-->
                <router-link v-bind:to="{name: 'UserProfile',params:{id: 1}}">个人信息</router-link>
              </el-menu-item>
              <el-menu-item index="1-2">
                <router-link to="/user/list">用户列表</router-link>
              </el-menu-item>
              <el-menu-item index="1-3">
                <router-link to="/goHome">回到首页</router-link>
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>

          <el-submenu index="2">
            <template slot="title"><i class="el-icon-caret-right"></i>内容管理</template>
            <e1-menu-item-group>
              <el-menu-item index="2-1">分类管理</el-menu-item>
              <el-menu-item index="2-2">内容列表</el-menu-item>
            </e1-menu-item-group>
          </el-submenu>

          <el-submenu index="3">
            <template slot="title"><i class="el-icon-caret-right"></i>系统管理</template>
            <e1-menu-item-group>
              <el-menu-item index="3-1">页面设置</el-menu-item>
              <el-menu-item index="3-2">用户设置</el-menu-item>
            </e1-menu-item-group>
          </el-submenu>


        </el-menu>
      </el-aside>
      <el-container>
        <el-header style="text-align: right; font-size: 12px">
          <el-dropdown>
            <i class="el-icon-setting" style="margin-right:15px"></i>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>个人信息</el-dropdown-item>
              <el-dropdown-item>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <span>{{name}}</span>
        </el-header>

        <el-main>
          <router-view></router-view>
        </el-main>

      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  props:['name'],
  name: "Main"
}
</script>

<style scoped lang="scss">
.el-header {
  .el-header {
    backdrop-color: #2c568f;
    color: #333;
    line-height: 60px;
  }

  .el-aside {
    color: #333;
  }
}
</style>

   这两个页面中已经写了一些代码,主要的流程是用户在login.vue页面输入账号和密码,只要不为空,就会跳转到main.vue页面,而在main.vue页面中,有两个子导航栏,点击该导航栏就会跳转到对应子路由的子组件中。

4、main.js下的两个页面List.vue和Profile.vue

List.vue页面:

<template>
<h1>用户列表</h1>
</template>

<script>
export default {
  name: "UserList"
}
</script>

<style scoped>

</style>

Profile.vue页面:

<template>
<!-- 所有的元素,必须在根节点下,需要有盒子套着 -->
  <div>
    <h1>个人信息</h1>
    {{ id}}
  </div>
</template>

<script>
export default {

}
</script>

<style scoped>

</style>

二、创建路由

代码如下:

ps:注意main组件下的children属性是嵌套路由,表示在我们的main.vue中有两个路由的跳转,跳转时,可以先找到main再找到对应的子组件的跳转路由。

import Vue from "vue"
import Router from 'vue-router'
import Main from "../views/Main";
import Login from "../views/Login";
import UserList from "../views/user/List";
import UserProfile from "../views/user/Profile";
import NotFound from "../views/NotFound";
Vue.use(Router);

export default new Router({
  mode:"history",
  routes:[
    {
      path:'/login',//嵌套路由
      component: Login,
    },{
    path: '/main/:name',
      component: Main,
      props:true,
      children:[
        {
          path:'/user/profile/:id',
          name:'UserProfile',//注意名字是字符串
          props:true,
          component:UserProfile
        },{
          path:'/user/list',
          component:UserList
        },{
      path:'/goHome',
          redirect:'/main'
        }
      ]
    },{
    path:'*',
      component:NotFound,
    }
  ]
});

设置main.js,将elementUI和router引入,代码如下:

import Vue from 'vue'
import App from './App'
import router from './router'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios';
import VueAxios from 'vue-axios'
//先router在axios不然识别不到axios
Vue.use(router);
Vue.use(VueAxios,axios);//这个顺序也不能变
Vue.use(ElementUI);
new Vue({
  el: '#app',
  router,
  render: h => h(App)//ElementUI官网的配置
})

三、页面跳转

其实现在已经可以实现页面跳转了,当我们“登录”跳转“主页面”,点解左右导航栏显示不同信息。

如下所示:

3cb837ad8319461292d9d97bddf4fe26.png

5c7dc70278324b70b106a4d9198645b7.png

b903aad944384457949b06568b955c94.png

    其实到这里对于页面之间的简单跳转就差不多了,对于我们后端学前端的人员来说,最重要的就还剩数据的请求然后再在页面显示即可。

四、数据请求

  我们以Profile.vue页码为例,当用户点击该导航栏时,实现数据的请求,看能否通过接口拿到我们先要的数据呢?

首先我们写一个静态数据测一下,因为还未写后端代码。

{
  "name":"lfy",
  "url": "http://baidu.com",
  "page": "1",
  "isNonProfit":"true",
  "address": {
    "street": "含光门",
    "city":"陕西西安",
    "country": "中国"
  },
  "links": [
    {
      "name": "B站",
      "url": "https://www.bilibili.com/"
    },
    {
      "name": "4399",
      "url": "https://www.4399.com/"
    },
    {
      "name": "百度",
      "url": "https://www.baidu.com/"
    }
  ]
}

   我们在Proflie.vue实例中,有beforeRouteEnter()、beforeRouteLeave()两个函数分别是进入路由之前和离开路由之后,我们可以在这两个函数之内进行数据的请求,只有请求的方式用的是之前学的axios来请求。

具体代码如下:

<script>
export default {
  props:['id'],
  name: "Profile",
  beforeRouteEnter:(to,from,next)=>{
    console.log("进入路由之前");
    next(vm=>{
      vm.getData();
    });
  },
  beforeRouteLeave:(to,from,next)=>{
    console.log("进入路由之后");
    next();
  },
  methods:{
    getData:function () {
      this.axios({
        method:"get",
        url:'http://localhost:8080/static/mock/data.json'
      }).then(function (response){
        console.log(response);
      })
    }
  }
}
</script>

效果显示:

87e312a7b58148ddb7c19f230d1889ce.png

   如图所示当我们点击导航栏的时候就可以完成数据的请求,后面只需要通过之前博客写的关于vue的事件进行取值,和elementUI的一些样式进行数据渲染即可。

五、总结

   到这里关于vue的一些基本知识就学习的差不多了,接下来博主正在做一个springboot+vue的项目,后面会将我们学习的内容用到项目中去,也会写相应的博客与大家分享技术。那我们就下一篇博客再见!如果这篇博客对各位小伙伴有所帮助,请点赞,收藏支持一波哦~

 

  • 19
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot 和 Vue 结合使用,可以通过 Element UI 来实现动态路由。 具体步骤如下: 1. 在 Spring Boot 中,定义一个 API 接口,用于返回动态路由的配置信息,例如菜单列表、权限信息等。 2. 在 Vue 中,使用 axios 等工具调用该 API 接口获取动态路由的配置信息。 3. 在 Vue 中,使用 vue-router 来实现路由功能。在路由配置中,使用获取到的动态路由信息来动态生成路由。例如,使用菜单列表来生成菜单路由,使用权限信息来控制路由的访问权限等。 4. 在 Vue 中,结合 Element UI 的组件,可以实现一些常见的路由功能,例如面包屑导航、菜单栏等。 综上所述,通过 Spring Boot、VueElement UI 的结合,可以实现动态路由功能,从而实现更加灵活、可扩展的前端页面管理。 ### 回答2: Spring Boot是一个开发框架,Vue是一个前端框架,Element UI是一个UI组件库,如何实现动态路由呢? 首先,在Spring Boot后端,需要定义一个接口来获取动态路由的数据。可以使用一个数据库表来存储路由信息,如路由路径、组件名称、图标等。然后,通过编写一个控制器类,来处理获取动态路由数据请求。在该控制器类中,可以调用相应的服务类或数据访问层来获取路由数据,并返回给前端。 接下来,在Vue前端项目中,可以使用Vue Router来实现动态路由。可以在项目的入口文件(如main.js)中,通过发送请求获取动态路由数据。可以使用axios等库来发送请求,获取后端返回的动态路由数据。获取到数据后,可以通过遍历的方式,动态地把路由配置项添加到Vue Router中。 同时,在项目中引入Element UI组件库,可以使用其中的菜单、导航等组件,来展示动态路由。可以根据获取的动态路由数据,来生成菜单和导航栏的数据,并将其展示在页面中。 为了实现动态路由的跳转,可以使用Vue Router中的路由守卫(如beforeEach),在路由跳转之前判断是否有权限访问该路由。可以根据当前用户的权限信息,来判断是否有权限访问该路由。如果没有权限,则可以跳转到其他页面或者显示相应的提示信息。 总结来说,通过在Spring Boot后端定义接口获取动态路由数据,并在Vue前端项目中将其配置到Vue Router中,配合使用Element UI的菜单、导航组件,就可以实现Spring Boot、VueElement UI的动态路由。 ### 回答3: 要实现动态路由,我们可以结合使用Spring Boot、VueElement UI。 首先,在Spring Boot后端,我们需要建立一个API接口,用于获取动态路由的数据。这个接口可以返回一个JSON对象,包含了多个路由对象的信息,如路由名称、路径、组件等。 接下来,在Vue前端,我们可以使用Element UI的导航菜单组件来实现动态路由。首先,我们需要在Vue项目中安装Element UI,并引入导航菜单组件。然后,在主页面组件中,我们可以通过调用后端的API接口获取动态路由数据,然后根据返回的数据动态生成导航菜单。可以使用Vue Router来管理路由,并使用 `<router-view>` 标签来展示对应的页面组件。 在生成导航菜单时,我们可以使用递归组件来实现无限嵌套的导航菜单结构。每个导航菜单项可以绑定点击事件,当用户点击菜单项时,可以通过Vue Router进行路由跳转,展示对应的页面组件。 为了保证路由权限控制,我们可以在后端API接口中加入用户权限验证的逻辑。在前端,我们可以根据用户的角色或权限信息动态生成导航菜单,只展示用户有权限访问的路由。 总结来说,使用Spring Boot提供API接口获取动态路由数据,然后在Vue前端使用Element UI的导航菜单组件构建动态路由。通过递归组件生成无限嵌套的导航菜单,并通过Vue Router实现路由跳转。同时,可以结合用户权限信息进行路由权限控制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值