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

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

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

一、準備工作

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、準別好的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傳遞參數(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>內容管理</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>系統(tǒng)管理</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>
<!-- 所有的元素,必須在根節(jié)點下,需要有盒子套著 -->
  <div>
    <h1>個人信息</h1>
    {{ id}}
  </div>
</template>
<script>
export default {
}
</script>
<style scoped>
</style>

二、創(chuàng)建路由

代碼如下:

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官網的配置
})

三、頁面跳轉

其實現(xiàn)在已經可以實現(xiàn)頁面跳轉了,當我們“登錄”跳轉“主頁面”,點解左右導航欄顯示不同信息。

如下所示:

    其實到這里對于頁面之間的簡單跳轉就差不多了,對于我們后端學前端的人員來說,最重要的就還剩數(shù)據的請求然后再在頁面顯示即可。

四、數(shù)據請求

  我們以Profile.vue頁碼為例,當用戶點擊該導航欄時,實現(xiàn)數(shù)據的請求,看能否通過接口拿到我們先要的數(shù)據呢?

首先我們寫一個靜態(tài)數(shù)據測一下,因為還未寫后端代碼。

{
  "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()兩個函數(shù)分別是進入路由之前和離開路由之后,我們可以在這兩個函數(shù)之內進行數(shù)據的請求,只有請求的方式用的是之前學的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>

效果顯示:

   如圖所示當我們點擊導航欄的時候就可以完成數(shù)據的請求,后面只需要通過之前博客寫的關于vue的事件進行取值,和elementUI的一些樣式進行數(shù)據渲染即可。

五、總結

   到這里關于vue的一些基本知識就學習的差不多了,接下來博主正在做一個springboot+vue的項目,后面會將我們學習的內容用到項目中去,也會寫相應的博客與大家分享技術。

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

相關文章

  • 詳解vue.js移動端配置flexible.js及注意事項

    詳解vue.js移動端配置flexible.js及注意事項

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

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

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

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

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

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

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

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

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

    前端使用rtsp視頻流接入??低晹z像頭的具體步驟

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

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

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

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

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

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

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

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

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

最新評論

华阴市| 板桥市| 赣州市| 铜鼓县| 云阳县| 微山县| 贺州市| 叙永县| 潍坊市| 镶黄旗| 满城县| 乐亭县| 社会| 鹤山市| 绥宁县| 乐平市| 定陶县| 顺昌县| 武川县| 涟源市| 莒南县| 武义县| 江孜县| 南丹县| 海淀区| 西华县| 岢岚县| 平舆县| 博乐市| 措美县| 通河县| 韶山市| 克山县| 泾阳县| 涿州市| 德安县| 惠东县| 中超| 彰武县| 新干县| 永福县|