最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2024年05月06日 10:43:15   作者:Eliauk &  
我們在Proflie.vue實(shí)例中,有beforeRouteEnter、beforeRouteLeave兩個函數(shù)分別是進(jìn)入路由之前和離開路由之后,我們可以在這兩個函數(shù)之內(nèi)進(jìn)行數(shù)據(jù)的請求,下面給大家分享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)文章

  • 詳解vue.js移動端配置flexible.js及注意事項(xiàng)

    詳解vue.js移動端配置flexible.js及注意事項(xiàng)

    最近在用vue做移動端項(xiàng)目,網(wǎng)上找了一些移動端適配的方案,個人覺得手淘團(tuán)隊(duì)flexible.js還是比較容易上手,在這里做下總結(jié)。對vue.js移動端配置flexible.js 相關(guān)知識感興趣的朋友跟隨小編一起看看吧
    2019-04-04
  • Vue 實(shí)現(xiàn)列表動態(tài)添加和刪除的兩種方法小結(jié)

    Vue 實(shí)現(xiàn)列表動態(tài)添加和刪除的兩種方法小結(jié)

    今天小編就為大家分享一篇Vue 實(shí)現(xiàn)列表動態(tài)添加和刪除的兩種方法小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • 深入詳解Vue3中的Mock數(shù)據(jù)模擬

    深入詳解Vue3中的Mock數(shù)據(jù)模擬

    這篇文章主要為大家介紹了深入Vue3中的Mock數(shù)據(jù)模擬實(shí)現(xiàn)細(xì)節(jié)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • vue使用openlayers創(chuàng)建地圖

    vue使用openlayers創(chuàng)建地圖

    這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目中使用openlayers創(chuàng)建地圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • vue3中reactive不能直接賦值的解決方案

    vue3中reactive不能直接賦值的解決方案

    這篇文章主要介紹了vue3中reactive不能直接賦值的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 前端使用rtsp視頻流接入??低晹z像頭的具體步驟

    前端使用rtsp視頻流接入海康威視攝像頭的具體步驟

    這篇文章主要介紹了前端使用rtsp視頻流接入??低晹z像頭的具體步驟,前端調(diào)用海康威視攝像頭的過程中,主要涉及到攝像頭的連接、視頻流的獲取以及前端頁面的展示,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-09-09
  • Vue3中watch監(jiān)聽使用詳解

    Vue3中watch監(jiān)聽使用詳解

    watch 函數(shù)用來偵聽特定的數(shù)據(jù)源,并在回調(diào)函數(shù)中執(zhí)行副作用,下面這篇文章主要給大家介紹了關(guān)于Vue3中watch監(jiān)聽使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • vue 雙向數(shù)據(jù)綁定的實(shí)現(xiàn)學(xué)習(xí)之監(jiān)聽器的實(shí)現(xiàn)方法

    vue 雙向數(shù)據(jù)綁定的實(shí)現(xiàn)學(xué)習(xí)之監(jiān)聽器的實(shí)現(xiàn)方法

    這篇文章主要介紹了vue 雙向數(shù)據(jù)綁定的實(shí)現(xiàn)學(xué)習(xí)之監(jiān)聽器的實(shí)現(xiàn)方法,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11
  • Vue實(shí)現(xiàn)阻止瀏覽器記住密碼功能的三種方法

    Vue實(shí)現(xiàn)阻止瀏覽器記住密碼功能的三種方法

    本文主要介紹了Vue實(shí)現(xiàn)阻止瀏覽器記住密碼功能的三種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 解決webpack+Vue引入iView找不到字體文件的問題

    解決webpack+Vue引入iView找不到字體文件的問題

    今天小編就為大家分享一篇解決webpack+Vue引入iView找不到字體文件的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09

最新評論

青铜峡市| 伊通| 阿克苏市| 蚌埠市| 阿勒泰市| 南召县| 东方市| 阿巴嘎旗| 比如县| 内乡县| 德格县| 外汇| 龙山县| 咸阳市| 那坡县| 通州区| 新安县| 漳浦县| 文安县| 清徐县| 莒南县| 江西省| 青川县| 绥江县| 区。| 红原县| 泉州市| 金寨县| 阿勒泰市| 河南省| 华宁县| 张家港市| 汽车| 昌宁县| 兰州市| 泰宁县| 涟源市| 岑溪市| 宝鸡市| 韶山市| 台山市|