欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue結(jié)合ElementUI實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求和頁(yè)面跳轉(zhuǎn)功能(最新推薦)

 更新時(shí)間:2024年05月06日 10:43:15   作者:Eliauk &  
我們?cè)赑roflie.vue實(shí)例中,有beforeRouteEnter、beforeRouteLeave兩個(gè)函數(shù)分別是進(jìn)入路由之前和離開(kāi)路由之后,我們可以在這兩個(gè)函數(shù)之內(nèi)進(jìn)行數(shù)據(jù)的請(qǐng)求,下面給大家分享Vue結(jié)合ElementUI實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求和頁(yè)面跳轉(zhuǎn)功能,感興趣的朋友一起看看吧

一、準(zhǔn)備工作

1、創(chuàng)建一個(gè)Vue-cli程序

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

2、安裝ElementUI

在創(chuàng)建Vue-cli程序的過(guò)程中,需要在控制臺(tái)執(zhí)行以下指令:

#安裝 element-ui
npm i element-ui -S
#安裝 SASS 加載器
cnpm install sass-loader node-sass --save-dev

3、準(zhǔn)別好的login.vue和main.vue頁(yè)面

這個(gè)是提前準(zhǔn)備好的兩個(gè)login.vue和main.vue頁(yè)面。

login.vue頁(yè)面:

<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="賬號(hào)" prop="username">
        <el-input type="text" placeholder="請(qǐng)輸入賬號(hào)" v-model="form.username"/>
      </el-form-item>
      <el-form-item label="密碼" prop="password">
        <el-input type="password" placeholder="請(qǐng)輸入密碼" 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>請(qǐng)輸入賬號(hào)和密碼</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: " 賬號(hào)不可為空", trigger: 'blur'}
        ],
        password: [
          {required: true, message: " 密碼不可為空 ", trigger: 'blur'}
        ]
      },
//對(duì)話框顯示和隱藏
      dialogVisible: false
    }
  },
  methods: {
    onSubmit(formName) {
//為表單綁定驗(yàn)證功能
      this.$refs[formName].validate((valid) => {
        if (valid) {
//使用vue-router路由到指定頁(yè)面,該方式稱之為編程式導(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頁(yè)面:

<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}}">個(gè)人信息</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">回到首頁(yè)</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">頁(yè)面設(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>個(gè)人信息</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>

   這兩個(gè)頁(yè)面中已經(jīng)寫(xiě)了一些代碼,主要的流程是用戶在login.vue頁(yè)面輸入賬號(hào)和密碼,只要不為空,就會(huì)跳轉(zhuǎn)到main.vue頁(yè)面,而在main.vue頁(yè)面中,有兩個(gè)子導(dǎo)航欄,點(diǎn)擊該導(dǎo)航欄就會(huì)跳轉(zhuǎn)到對(duì)應(yīng)子路由的子組件中。

4、main.js下的兩個(gè)頁(yè)面List.vue和Profile.vue

List.vue頁(yè)面:

<template>
<h1>用戶列表</h1>
</template>
<script>
export default {
  name: "UserList"
}
</script>
<style scoped>
</style>

Profile.vue頁(yè)面:

<template>
<!-- 所有的元素,必須在根節(jié)點(diǎn)下,需要有盒子套著 -->
  <div>
    <h1>個(gè)人信息</h1>
    {{ id}}
  </div>
</template>
<script>
export default {
}
</script>
<style scoped>
</style>

二、創(chuàng)建路由

代碼如下:

ps:注意main組件下的children屬性是嵌套路由,表示在我們的main.vue中有兩個(gè)路由的跳轉(zhuǎn),跳轉(zhuǎn)時(shí),可以先找到main再找到對(duì)應(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不然識(shí)別不到axios
Vue.use(router);
Vue.use(VueAxios,axios);//這個(gè)順序也不能變
Vue.use(ElementUI);
new Vue({
  el: '#app',
  router,
  render: h => h(App)//ElementUI官網(wǎng)的配置
})

三、頁(yè)面跳轉(zhuǎn)

其實(shí)現(xiàn)在已經(jīng)可以實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)了,當(dāng)我們“登錄”跳轉(zhuǎn)“主頁(yè)面”,點(diǎn)解左右導(dǎo)航欄顯示不同信息。

如下所示:

    其實(shí)到這里對(duì)于頁(yè)面之間的簡(jiǎn)單跳轉(zhuǎn)就差不多了,對(duì)于我們后端學(xué)前端的人員來(lái)說(shuō),最重要的就還剩數(shù)據(jù)的請(qǐng)求然后再在頁(yè)面顯示即可。

四、數(shù)據(jù)請(qǐng)求

  我們以Profile.vue頁(yè)碼為例,當(dāng)用戶點(diǎn)擊該導(dǎo)航欄時(shí),實(shí)現(xiàn)數(shù)據(jù)的請(qǐng)求,看能否通過(guò)接口拿到我們先要的數(shù)據(jù)呢?

首先我們寫(xiě)一個(gè)靜態(tài)數(shù)據(jù)測(cè)一下,因?yàn)檫€未寫(xiě)后端代碼。

{
  "name":"lfy",
  "url": "http://baidu.com",
  "page": "1",
  "isNonProfit":"true",
  "address": {
    "street": "含光門(mén)",
    "city":"陜西西安",
    "country": "中國(guó)"
  },
  "links": [
    {
      "name": "B站",
      "url": "https://www.bilibili.com/"
    },
    {
      "name": "4399",
      "url": "https://www.4399.com/"
    },
    {
      "name": "百度",
      "url": "https://www.baidu.com/"
    }
  ]
}

   我們?cè)赑roflie.vue實(shí)例中,有beforeRouteEnter()、beforeRouteLeave()兩個(gè)函數(shù)分別是進(jìn)入路由之前和離開(kāi)路由之后,我們可以在這兩個(gè)函數(shù)之內(nèi)進(jìn)行數(shù)據(jù)的請(qǐng)求,只有請(qǐng)求的方式用的是之前學(xué)的axios來(lái)請(qǐng)求。

具體代碼如下:

<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í)候就可以完成數(shù)據(jù)的請(qǐng)求,后面只需要通過(guò)之前博客寫(xiě)的關(guān)于vue的事件進(jìn)行取值,和elementUI的一些樣式進(jìn)行數(shù)據(jù)渲染即可。

五、總結(jié)

   到這里關(guān)于vue的一些基本知識(shí)就學(xué)習(xí)的差不多了,接下來(lái)博主正在做一個(gè)springboot+vue的項(xiàng)目,后面會(huì)將我們學(xué)習(xí)的內(nèi)容用到項(xiàng)目中去,也會(huì)寫(xiě)相應(yīng)的博客與大家分享技術(shù)。

到此這篇關(guān)于Vue結(jié)合ElementUI實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求和頁(yè)面跳轉(zhuǎn)功能的文章就介紹到這了,更多相關(guān)Vue ElementUI 數(shù)據(jù)請(qǐng)求和頁(yè)面跳轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論