vue 路由視圖 router-view嵌套跳轉(zhuǎn)的實現(xiàn)
目的:
實現(xiàn)功能:制作一個登錄頁面,跳轉(zhuǎn)到首頁,首頁包含菜單欄、頂部導航欄、主體,標準的后臺網(wǎng)頁格式。菜單欄點擊不同菜單控制主體展示不同的組件(不同的頁面)。
配置router-view嵌套跳轉(zhuǎn)需要準備兩個主要頁面,一個由app.vue跳轉(zhuǎn)的登錄頁面(login.vue),一個由登錄頁面(login.vue)跳轉(zhuǎn)首頁(index.vue)。另外還需要兩個用于菜單跳轉(zhuǎn)頁面,頁面內(nèi)容自定義
我這里使用的是element-ui作為模板
前置:引入element-ui
在項目目錄下執(zhí)行命令:npm i element-ui -s
修改main.js,引入element組件

步驟:
- 創(chuàng)建登錄頁面(login.vue)
- 創(chuàng)建后臺操作頁面(index.vue)
- 配置后臺操作頁面菜單跳轉(zhuǎn)
- 配置嵌套路由視圖路由控制菜單跳轉(zhuǎn)
1、修改app.vue頁面
app頁面只要放置一個router-view標簽即可,每一個路由請求默認都是從這里進去跳轉(zhuǎn)
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
methods: {
}
}
</script>
<style>
</style>
2、創(chuàng)建登錄頁面(/views/login/login.vue)
這里的登錄按鈕跳轉(zhuǎn)是直接跳轉(zhuǎn)到主頁面,當前登陸頁面將完全被替換掉
登錄頁代碼:point_down:
<template>
<div id="login">
<el-form>
<el-row :gutter="20">
<el-col class="title" :offset="9" :span="6">
<h1>一個登錄頁面</h1>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col class="col-label" :offset="7" :span="4">
<span class="label">賬號:</span>
</el-col>
<el-col :span="4">
<el-input type="text" placeholder="請輸入賬號" v-model="account.username"></el-input>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col class="col-label" :offset="7" :span="4">
<span class="label">密碼:</span>
</el-col>
<el-col :span="4">
<el-input type="password" placeholder="請輸入密碼" v-model="account.password"></el-input>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col :offset="8" :span="8">
<span>
<a href="#" rel="external nofollow" rel="external nofollow" @click="register" >注冊賬號</a>
/
<a href="#" rel="external nofollow" rel="external nofollow" @click="resetPwd" >重置密碼</a>
</span>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col :offset="10" :span="4">
<el-button type="primary" round @click="onSubmit">登錄</el-button>
</el-col>
</el-row>
</el-form>
</div>
</template>
<script>
export default {
name: 'login',
data() {
return {
account: {
username: '',
password: ''
}
}
},
methods: {
register() {
this.$message({
message: '好像沒這功能',
type: 'info'
})
},
resetPwd() {
this.$message({
message: '下輩子吧',
type: 'success'
})
},
onSubmit() {
this.$router.push('/index')
}
}
}
</script>
<style scoped>
#login {
text-align: center;
margin: 0 auto;
}
.label {
height: 40px;
line-height: 40px;
}
.col-label {
text-align: right;
}
.el-row {
margin-top: 15px;
}
.el-button {
width: 120px;
}
.title {
margin-top: 10%;
}
</style>
View Code

2.1、在router/index.js中添加登錄頁面路由
{
path: '/',
name: 'login',
component: () => import('../views/login/login.vue')
},
3、創(chuàng)建主頁面(/components/index.vue)
主頁面主要分為三塊,左側菜單欄(el-menu),頂部導航欄(el-container),主體展示頁面(el-main),這部分是從elment-ui中的布局容器實例拷貝過來的(https://element.eleme.cn/#/zh-CN/component/container)
<template>
<el-container style="border: 1px solid #eee">
<el-aside width="200px" style="background-color: rgb(238, 241, 246)">
<el-menu :default-openeds="['1']" :router="true">
<el-submenu index="1">
<template slot="title"><i class="el-icon-message"></i>導航一</template>
<el-menu-item-group>
<template slot="title">分組一</template>
<el-menu-item index="1-1">選項1</el-menu-item>
<el-menu-item index="1-2">選項2</el-menu-item>
</el-menu-item-group>
</el-submenu>
</el-menu>
</el-aside>
<el-container class="table-div">
<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-item>刪除</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
<span>王小虎</span>
</el-header>
<el-main>
<table></table>
</el-main>
</el-container>
</el-container>
</template>
<style>
.el-header {
background-color: #B3C0D1;
color: #333;
line-height: 60px;
}
.el-aside {
color: #333;
}
</style>
<script>
export default {
name: 'index',
data() {
}
}
</script>
<style>
.table-div {
width: 100%;
}
.el-table {
height: 100%;
}
</style>
3.1、創(chuàng)建主頁面路由
{
path: '/index',
name: 'index',
component: () => import('@/components/index')
}
至此,由登錄頁面跳轉(zhuǎn)到主頁面的操作配置就完成了。看看效果。啟動項目,訪問http://localhost:8080/#/

點擊登錄,跳轉(zhuǎn)到首頁

4、修改首頁
主要修改兩個部分:菜單跳轉(zhuǎn)路徑,主體配置路由視圖
4.1、開啟菜單路由模式,并修改菜單跳轉(zhuǎn)路徑
在el-menu菜單中開啟vue-router模式,并在el-menu-item標簽中的index配置要跳轉(zhuǎn)的頁面地址

4.2、添加router-view
直接將主體內(nèi)容替換為router-view標簽, 并為name屬性添加參數(shù),我這使用table標識,這個很重要,路由需要根據(jù)這個name屬性指定跳轉(zhuǎn)的路由視圖

5、創(chuàng)建兩個子頁面
頁面可以在任意位置,只要路由地址配置正確即可
我這創(chuàng)建了first-page.vue,first-table.vue(頁面內(nèi)容自定義)
在router/index.js配置路由,在哪個頁面下增加的router-view組件,就在相應的路由配置中添加children,這里是在index路由修改配置,添加children,配置path、name和components(這里注意,如果只配置默認路由視圖跳轉(zhuǎn),用的參數(shù)是component,配置多路由視圖跳轉(zhuǎn)時components)
{
path: '/index',
name: 'index',
component: () => import('@/components/index'), // 這里配置首頁默認路由跳轉(zhuǎn)的頁面組件
children: [ // 添加子路由
{
path: 'page', // 注意點:路徑前面不要帶/,否則無法跳轉(zhuǎn),在這里吃過虧
name: 'page',
components: { // 注意參數(shù)名帶s
table: () => import('@/components/first-page') // 這里的table跟首頁的router-view標簽的name一致,才會在首頁的路由視圖進行跳轉(zhuǎn),看3.2
}
},
{
path: 'table',
name: 'table',
components: {
table: () => import('@/components/first-table')
}
}
]
}
這樣配置訪問地址為:localhost:8080/#/index/page、localhost:8080/#/index/table
到這里配置完成,訪問頁面測試一下

可以看到,兩個菜單配置改變了主體的路由視圖,控制視圖展示的頁面組件。配置完成
到此這篇關于vue 路由視圖 router-view嵌套跳轉(zhuǎn)詳情的文章就介紹到這了,更多相關vue 路由視圖 router-view嵌套跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于Vue3+Element Plus 實現(xiàn)多表單校驗demo
表單校驗在日常的開發(fā)需求中是一種很常見的需求,通常在提交表單發(fā)起請求前校驗用戶輸入是否符合規(guī)則,通常只需formRef.value.validate()即可校驗,本文給大家介紹基于Vue3+Element Plus 實現(xiàn)多表單校驗demo,感興趣的朋友一起看看吧2024-06-06
Vue如何解決子組件data從props中無法動態(tài)更新數(shù)據(jù)問題
這篇文章主要介紹了Vue如何解決子組件data從props中無法動態(tài)更新數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
vue+mousemove實現(xiàn)鼠標拖動功能(拖動過快失效問題解決方法)
這篇文章主要介紹了vue+mousemove實現(xiàn)鼠標拖動功能,文中給大家介紹了鼠標移動過快拖動就失效問題的解決方法,需要的朋友可以參考下2018-08-08

