Vue 實現重定向、404和路由鉤子(六)

一、重定向

1.1 修改 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 傳遞參數,v-bind 進行對象綁定--><router-link v-bind:to="{name:'Profile222',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><el-menu-item-group><el-menu-item index="2-1">分類管理</el-menu-item><el-menu-item index="2-2">內容列表</el-menu-item></el-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></el-header><el-main><router-view/></el-main></el-container></el-container></div>
</template><script>
export default {name: "Main"
}
</script><style  scoped lang="scss">
.el-header {background-color: yellow;color: blue;line-height: 60px;
}.el-aside {color: #333;
}
</style>

1.2 修改 router 路由

????????修改 router 文件夾下的 index.js,內容如下所示:

import Vue from 'vue'
import Router from 'vue-router'
import Main from '../views/Main'
import Login from '../views/Login'
import List from '../views/user/List'
import Profile from '../views/user/Profile'Vue.use(Router);export default new Router({routes:[{path:'/main',component:Main,// 配置嵌套路由children:[{path:'/user/list',component:List},{// 使用:id 進行參數接收path:'/user/profile/:id',name:'Profile222',component:Profile,props:true},{// 配置重定向信息path:'/goHome',redirect:'/main'}]},{path:'/Login',component:Login}]
})

1.3 測試

???啟動工程,如下所示:

?????????在地址欄的后綴輸入?main ,顯示的內容如下所示,先點擊個人信息,再點擊回到首頁,就可以發現地址欄發生了跳轉。

二、顯示當前登錄的用戶姓名

2.1 修改 Login.vue

<template><div><el-form ref="loginForm" :model="form" :rules="rules" label-width="8px" 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:''},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){// 傳遞當前用戶輸入的用戶名參數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>

2.2 修改 router 路由

????????修改 router 文件夾下的 index.js,內容如下所示:

import Vue from 'vue'
import Router from 'vue-router'
import Main from '../views/Main'
import Login from '../views/Login'
import List from '../views/user/List'
import Profile from '../views/user/Profile'Vue.use(Router);export default new Router({routes:[{// 接收 login 傳過來的參數path:'/main/:name',component:Main,// 允許接收參數props:true,// 配置嵌套路由children:[{path:'/user/list',component:List},{// 使用:id 進行參數接收path:'/user/profile/:id',name:'Profile222',component:Profile,props:true},{// 配置重定向信息path:'/goHome',redirect:'/main'}]},{path:'/Login',component:Login}]
})

2.3 修改 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 傳遞參數,v-bind 進行對象綁定--><router-link v-bind:to="{name:'Profile222',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><el-menu-item-group><el-menu-item index="2-1">分類管理</el-menu-item><el-menu-item index="2-2">內容列表</el-menu-item></el-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/></el-main></el-container></el-container></div>
</template><script>
export default {// 接收 name 參數props:['name'],name: "Main"
}
</script><style  scoped lang="scss">
.el-header {background-color: yellow;color: blue;line-height: 60px;
}.el-aside {color: #333;
}
</style>

2.4?測試

????????啟動工程,網址后綴輸入 login,并隨便登錄,如下所示:

三、路由模式與404

3.1 路由模式

路由默認分為兩種,默認的是 hash

????????1、hash:路徑帶 # 符號,如:http://localhost/#/login

????????2、history:路徑不帶 # 符號,如:http://localhost/login

????????修改路由配置,代碼如下:

export default new Router({mode:'history',routes:[]
});

3.2 配置 404

????????如果用戶訪問的路徑不存在怎么辦?給他返回一個 404 即可,新建一個 NotFound.vue 的試圖組件,內容如下所示:

<template><div>頁面不存在,請重試!</div>
</template><script>
export default {name: "NotFound"
}
</script><style scoped></style>

????????配置路由信息 index.js 如下:

import Vue from 'vue'
import Router from 'vue-router'
import Main from '../views/Main'
import Login from '../views/Login'
import List from '../views/user/List'
import Profile from '../views/user/Profile'
// 導入組件
import NotFound from '../views/user/NotFound'Vue.use(Router);export default new Router({routes:[{path:'/main/:name',component:Main,props:true,// 配置嵌套路由children:[{path:'/user/list',component:List},{// 使用:id 進行參數接收path:'/user/profile/:id',name:'Profile222',component:Profile,props:true},{// 配置重定向信息path:'/goHome',redirect:'/main'}]},{path:'/Login',component:Login},// 做配置{path:'*',component:NotFound}]
})

????????測試:

?四、路由鉤子與異步請求

4.1 路由鉤子

????????beforeRouteEnter:在進入路由前執行

????????beforeRouteLeave:在進入路由后執行

export default {name: "Profile",// 進入路由前執行// 過濾器,從哪來,到哪去,下一步是什么beforeRouteEnter:(to,from,next)=>{console.log("進入路由之前");next();},// 進入路由后執行beforeRouteLeave:(to,from,next)=>{console.log("進入路由之后");next();},
}

4.1.1 參數說明

????????to:路由將要跳轉的路徑信息

????????from:路徑跳轉前的路徑信息

????????next:路由的控制參數

????????????????next() :跳入下一個頁面

????????????????next('/path') :改變路由的跳轉方向,使其跳到另一個路由

????????????????next(false) :返回原來的頁面

????????????????next(vm=>{}) :僅在 beforeRouterEnter 中可用,vm是組件實例

4.2?在鉤子函數中使用異步請求

4.2.1 安裝 Axios

# cnpm 安裝失敗了就用 npm 安裝,我也安裝了好幾次才啟動成功了
cnpm install axios -snpm install axios -s

4.2.2 在 main.js 中引用 Axios

import axios from 'axios'
import VueAxios from 'vue-axios'Vue.use(VueAxios,axios);

4.2.3 創建測試數據

????????在 static 目錄下創建 mock 文件夾,并創建 data.json,內容如下所示:

{"name": "狂神說Java","url": "https://blog.kuangstudy.com","page": 1,"isNonProfit": true,"address": {"street": "含光門","city": "陜西西安","country": "中國"},"links": [{"name": "bilibili","url": "https://space,bilibili.com/95256449"},{"name": "狂伸說iava","ur1": "https://blog.kuangstudy.com"},{"name": "百度","url": "https://www.baidu.com/"}]
}

4.2.4 修改 Profile.vue

<template><!--所有的元素必須不能在根節點下,必須被div 包裹--><div><h1>個人信息</h1>
<!--    {{$route.params.id}}-->{{id}}</div></template><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>

4.2.5 測試

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/36172.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/36172.shtml
英文地址,請注明出處:http://en.pswp.cn/news/36172.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

MongoDB常用命令

什么是MongoDB ? MongoDB 是由C語言編寫的&#xff0c;是一個基于分布式文件存儲的開源數據庫系統。 在高負載的情況下&#xff0c;添加更多的節點&#xff0c;可以保證服務器性能。 MongoDB 旨在為WEB應用提供可擴展的高性能數據存儲解決方案。 MongoDB 將數據存儲為一個…

【網絡基礎實戰之路】基于BGP協議中的聯邦號連接三個AS區域的實戰詳解

系列文章傳送門&#xff1a; 【網絡基礎實戰之路】設計網絡劃分的實戰詳解 【網絡基礎實戰之路】一文弄懂TCP的三次握手與四次斷開 【網絡基礎實戰之路】基于MGRE多點協議的實戰詳解 【網絡基礎實戰之路】基于OSPF協議建立兩個MGRE網絡的實驗詳解 【網絡基礎實戰之路】基于…

Dalsa線陣相機說明(Linea Color GigESeries 2k and 4K)

文章目錄 一. Dalsa相機軟件整體架構二. 相機編號說明以及軟件要求三. 相機硬件參數三. 相機基本參數四. 軟件參數設置列表1. Sensor Control Category2. I/O Control Category3. Counter and Timer Control Category4. Advanced Processing Control Category(1) 平場校正介紹(…

學習Vue:插值表達式和指令

在 Vue.js 中&#xff0c;Vue 實例與數據綁定是構建動態交互界面的關鍵。在這篇文章中&#xff0c;我們將重點介紹 Vue 實例中兩種實現數據綁定的方式&#xff1a;插值表達式和指令。這些機制允許您將數據無縫地渲染到界面上&#xff0c;實現實時的數據更新和展示。 插值表達式…

U盤提示格式化怎么修復?學會這幾個方法!

“不知道大家有沒有遇到過將u盤插入電腦后提示格式化的情況呀&#xff1f;第一次遇到這種情況真的好無助&#xff0c;這是可以修復的嗎&#xff1f;請大家幫幫我&#xff01;” U盤作為一個便捷的存儲工具&#xff0c;幫助我們存儲了很多重要的數據和文件。但在使用的過程中&am…

Dockerfile 使用技巧篇

默認的 docker 鏡像使用 Linux 來當作基礎鏡像 01. 使用 alpine 鏡像&#xff0c;而不是默認的 linux 鏡像 PS: alpine 譯為高山植物&#xff0c;就是很少的資源就能存活的意思。alpine 裁剪了很多不必要的 linux 功能&#xff0c;使得鏡像體積大幅減小了。 比如 FROM node:1…

PHP8定義字符串的方法-PHP8知識詳解

字符串&#xff0c;顧名思義&#xff0c;就是將一堆字符串聯在一起。字符串簡單的定義方法是使用英文單引號&#xff08; &#xff09;或英文雙引號&#xff08;" "&#xff09;包含字符。另外&#xff0c;還可以使用定界符定義字符串。本文還介紹了字符串的連接符。…

TCP的三次握手和四次揮手

文章目錄 三次握手四次揮手TIME_WAITCLOSE_WAIT 使用wireshark觀察 三次握手 握手的最終目的是主機之間建立連接 首先要有兩個預備知識點 三次握手建立連接不一定會成功&#xff0c;其中最擔心的就是最后一次握手失敗&#xff0c;不過會有配套的解決方案建立好連接后是需要被…

【重溫老古董——Strust2框架】基于Idea使用maven創建Strust2項目

1、新建項目 紅色圈出的部分是【強制】,其他部分看個人喜好。 2、修改 pom 文件,管理依賴 <dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.5.22</version></dependency&g…

微服務中RestTemplate訪問其他服務返回值轉換問題

背景&#xff1a; 接收一個springcloud項目&#xff0c;UI模塊訪問其他服務的接口&#xff0c;返回數據統一都是使用fastjson進行轉換&#xff0c;但是新開發了幾個新模塊之后發現fastjson很多bug&#xff08;各種內存溢出&#xff09;&#xff0c;但是很多地方已經重度依賴fa…

數據結構:力扣OJ題(每日一練)

目錄 題一&#xff1a;環形鏈表 思路一&#xff1a; 題二&#xff1a;復制帶隨機指針的鏈表 思路一&#xff1a; 本人實力有限可能對一些地方解釋的不夠清晰&#xff0c;可以自己嘗試讀代碼&#xff0c;望海涵&#xff01; 題一&#xff1a;環形鏈表 給定一個鏈表的頭節點…

IDEA如何調試Stream API

Stream API現在在實際開發中應用非常廣泛&#xff0c;經常會遇到需要調試Stream API的場景&#xff0c;這篇文章主要講解如何使用IDEA調試Stream Testpublic void test(){Stream.of(10, 20, 30, 40, 50).mapToInt(e->e*10).filter(e->e>200).forEach(System.out::pri…

使用css實現時間線布局(TimeLine)

前言 在使用uni-app開發微信小程序過程中&#xff0c;遇到了時間軸布局&#xff0c;由于每項的內容高度不一致&#xff0c;使用uniapp自帶的擴展組件uni-steps&#xff0c;樣式布局無法對齊豎線&#xff0c;于是自己造輪子&#xff0c;完成特殊的布局。顯示效果如下&#xff1…

linux shell變量

linux shell變量 1、變量命名規則2、只讀變量3、刪除變量 1、變量命名規則 變量名不能加$命名只能使用英文字母、數字和下劃線&#xff0c;首個字母不能以數字開頭中間不能有空格。可以有下劃線不能使用標點符號不能使用bash中的關鍵字 username"tom"引用 $userna…

WebDAV之π-Disk·派盤+Commander One

Commander one是一款為Mac用戶設計的雙窗格文件管理器,Commander One專業版在原先的版本功能擁有較大的提升。Commander One PRO可以幫助大家將文件從一個地方復制到另一個地方,支持多標簽瀏覽、搜索、自定義熱鍵設置、顯示隱藏文件等功能。 π-Disk派盤 – 知識管理專家 派…

(一)創建型設計模式:4、原型模式(Prototype Pattern)

目錄 1、原型模式的含義 2、C實現原型模式的簡單實例 1、原型模式的含義 通過復制現有對象來創建新對象&#xff0c;而無需依賴于顯式的構造函數或工廠方法&#xff0c;同時又能保證性能。 The prototype pattern is a creational design pattern in software development. …

【校招VIP】java語言考點之Map1.7和1.8

考點介紹&#xff1a; HashMap是大中小廠面試的高頻考點&#xff0c;主要從底層結構&#xff0c;和線程安全等角度來進行考察&#xff0c;考察點比較集中&#xff0c;但是有一定難度。 分為初級和高級兩種&#xff1a;初級一般集中在中小公司的map的key-value的可重復和可空問題…

Server - WandB 統計運行 Epoch 以及 手動上傳日志

歡迎關注我的CSDN&#xff1a;https://spike.blog.csdn.net/ 本文地址&#xff1a;https://spike.blog.csdn.net/article/details/132227253 WandB (Weights & Biases) 是在線的模型訓練可視化工具&#xff0c;可以幫助跟蹤機器學習項目&#xff0c;記錄運行中的超參數和輸…

linux shell快速入門

linux shell快速入門 0 、前置1、簡單使用 0 、前置 一安裝linux的虛擬環境 1、簡單使用 1、新建/usr/shell目錄 2、新建hello.sh 文件 3、編寫腳本文件# !/bin/bashecho "hello world"查看是否具備執行權限 新增執行權限 chomd x hello.sh執行hello.sh文件 /b…

限制編輯下的PDF可以轉換其他格式嗎?這2個方法可行

我們知道&#xff0c;PDF可以通過設置“限制編輯”來保護文件不被隨意更改&#xff0c;那PDF設置了“限制編輯”還可以轉換其他格式嗎&#xff1f; 如果PDF設置的是禁止任何更改的“限制編輯”&#xff0c;那PDF菜單【轉換】界面下的格式選項就會呈現灰色狀態&#xff0c;無法…