Vue結(jié)合ElementUI實(shí)現(xiàn)數(shù)據(jù)請求和頁面跳轉(zhuǎn)功能(最新推薦)
一、準(zhǔn)備工作
1、創(chuàng)建一個Vue-cli程序
之前的博客有。各位看官姥爺,可以自查。
2、安裝ElementUI
在創(chuàng)建Vue-cli程序的過程中,需要在控制臺執(zhí)行以下指令:
#安裝 element-ui npm i element-ui -S #安裝 SASS 加載器 cnpm install sass-loader node-sass --save-dev
3、準(zhǔn)別好的login.vue和main.vue頁面
這個是提前準(zhǔn)備好的兩個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: '' }, //表單驗(yàn)證,需要在el-form-item 元素中增加prop 屬性 rules: { username: [ {required: true, message: " 賬號不可為空", trigger: 'blur'} ], password: [ {required: true, message: " 密碼不可為空 ", trigger: 'blur'} ] }, //對話框顯示和隱藏 dialogVisible: false } }, methods: { onSubmit(formName) { //為表單綁定驗(yàn)證功能 this.$refs[formName].validate((valid) => { if (valid) { //使用vue-router路由到指定頁面,該方式稱之為編程式導(dǎo)航 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傳遞參數(shù)--> <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>內(nèi)容管理</template> <e1-menu-item-group> <el-menu-item index="2-1">分類管理</el-menu-item> <el-menu-item index="2-2">內(nèi)容列表</el-menu-item> </e1-menu-item-group> </el-submenu> <el-submenu index="3"> <template slot="title"><i class="el-icon-caret-right"></i>系統(tǒng)管理</template> <e1-menu-item-group> <el-menu-item index="3-1">頁面設(shè)置</el-menu-item> <el-menu-item index="3-2">用戶設(shè)置</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>
這兩個頁面中已經(jīng)寫了一些代碼,主要的流程是用戶在login.vue頁面輸入賬號和密碼,只要不為空,就會跳轉(zhuǎn)到main.vue頁面,而在main.vue頁面中,有兩個子導(dǎo)航欄,點(diǎn)擊該導(dǎo)航欄就會跳轉(zhuǎn)到對應(yīng)子路由的子組件中。
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> <!-- 所有的元素,必須在根節(jié)點(diǎn)下,需要有盒子套著 --> <div> <h1>個人信息</h1> {{ id}} </div> </template> <script> export default { } </script> <style scoped> </style>
二、創(chuàng)建路由
代碼如下:
ps:注意main組件下的children屬性是嵌套路由,表示在我們的main.vue中有兩個路由的跳轉(zhuǎn),跳轉(zhuǎn)時,可以先找到main再找到對應(yīng)的子組件的跳轉(zhuǎn)路由。
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, } ] });
設(shè)置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官網(wǎng)的配置 })
三、頁面跳轉(zhuǎn)
其實(shí)現(xiàn)在已經(jīng)可以實(shí)現(xiàn)頁面跳轉(zhuǎn)了,當(dāng)我們“登錄”跳轉(zhuǎn)“主頁面”,點(diǎn)解左右導(dǎo)航欄顯示不同信息。
如下所示:
其實(shí)到這里對于頁面之間的簡單跳轉(zhuǎn)就差不多了,對于我們后端學(xué)前端的人員來說,最重要的就還剩數(shù)據(jù)的請求然后再在頁面顯示即可。
四、數(shù)據(jù)請求
我們以Profile.vue頁碼為例,當(dāng)用戶點(diǎn)擊該導(dǎo)航欄時,實(shí)現(xiàn)數(shù)據(jù)的請求,看能否通過接口拿到我們先要的數(shù)據(jù)呢?
首先我們寫一個靜態(tài)數(shù)據(jù)測一下,因?yàn)檫€未寫后端代碼。
{ "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實(shí)例中,有beforeRouteEnter()、beforeRouteLeave()兩個函數(shù)分別是進(jìn)入路由之前和離開路由之后,我們可以在這兩個函數(shù)之內(nèi)進(jìn)行數(shù)據(jù)的請求,只有請求的方式用的是之前學(xué)的axios來請求。
具體代碼如下:
<script> export default { props:['id'], name: "Profile", beforeRouteEnter:(to,from,next)=>{ console.log("進(jìn)入路由之前"); next(vm=>{ vm.getData(); }); }, beforeRouteLeave:(to,from,next)=>{ console.log("進(jìn)入路由之后"); next(); }, methods:{ getData:function () { this.axios({ method:"get", url:'http://localhost:8080/static/mock/data.json' }).then(function (response){ console.log(response); }) } } } </script>
效果顯示:
如圖所示當(dāng)我們點(diǎn)擊導(dǎo)航欄的時候就可以完成數(shù)據(jù)的請求,后面只需要通過之前博客寫的關(guān)于vue的事件進(jìn)行取值,和elementUI的一些樣式進(jìn)行數(shù)據(jù)渲染即可。
五、總結(jié)
到這里關(guān)于vue的一些基本知識就學(xué)習(xí)的差不多了,接下來博主正在做一個springboot+vue的項(xiàng)目,后面會將我們學(xué)習(xí)的內(nèi)容用到項(xiàng)目中去,也會寫相應(yīng)的博客與大家分享技術(shù)。
到此這篇關(guān)于Vue結(jié)合ElementUI實(shí)現(xiàn)數(shù)據(jù)請求和頁面跳轉(zhuǎn)功能的文章就介紹到這了,更多相關(guān)Vue ElementUI 數(shù)據(jù)請求和頁面跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3的el-table-column增加跳轉(zhuǎn)其他頁面的方法
文章介紹了如何在Vue3的el-table-column中增加跳轉(zhuǎn)其他頁面的功能,并提供了示例代碼和handleUpdate方法的源碼分析,感興趣的朋友跟隨小編一起看看吧2025-02-02Pure admin-Router標(biāo)簽頁配置與頁面持久化實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Pure admin-Router標(biāo)簽頁配置與頁面持久化實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01vue 本地環(huán)境跨域請求proxyTable的方法
今天小編就為大家分享一篇vue 本地環(huán)境跨域請求proxyTable的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09詳解如何在Vue項(xiàng)目中發(fā)送jsonp請求
這篇文章主要介紹了詳解如何在Vue項(xiàng)目中發(fā)送jsonp請求,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10vue 路由頁面之間實(shí)現(xiàn)用手指進(jìn)行滑動的方法
下面小編就為大家分享一篇vue 路由頁面之間實(shí)現(xiàn)用手指進(jìn)行滑動的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨跟隨小編過來看看吧2018-02-02Vue實(shí)現(xiàn)登錄記住賬號密碼功能的思路與過程
最近在學(xué)習(xí)vue,發(fā)現(xiàn)了vue的好多坑,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)登錄記住賬號密碼功能的思路與過程,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-11-11