Nuxt 嵌套路由nuxt-child組件用法(父子頁面組件的傳值)
Nuxt嵌套路由官網(wǎng)上的API詳解:點(diǎn)擊鏈接
看了官網(wǎng)上的api實(shí)現(xiàn)了官網(wǎng)的案例你會(huì)發(fā)現(xiàn)訪問父頁面中只能顯示父頁面中的內(nèi)容,要想默認(rèn)的在<nuxt-child>區(qū)域顯示一個(gè)頁面內(nèi)容怎么辦?


自己案例代碼:
pages/parent.vue
<template>
<div>
<h2>父組件的頁面的內(nèi)容</h2>
<ul>
<!-- 進(jìn)行切換子頁面,寫法同vue.js -->
<li><nuxt-link to='/parent/child'>child</nuxt-link></li>
<li><nuxt-link to='/parent/child2'>child2</nuxt-link></li>
</ul>
<hr>
<div class="box">
<p>嵌套子頁面內(nèi)容區(qū)</p>
<!-- <nuxt-child>標(biāo)簽在父頁面組件中相當(dāng)于是子頁面組件的占位符;嵌套中這個(gè)不可少 -->
<nuxt-child keep-alive :foobar="123"></nuxt-child>
</div>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
.box{
margin-top: 20px;
padding: 20px;
border: 2px solid pink;
border-radius: 5px;
}
</style>
pages/parent/index.vue
<template>
<div>
<h2>嵌套子組件中的默認(rèn)頁面index.vue</h2>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
</style>
pages/parent/child.vue
<template>
<div>
<h2>嵌套子組件中的頁面child</h2>
<p>foobar:{{foobar}}</p>
</div>
</template>
<script>
export default {
props:['foobar']
}
</script>
<style scoped>
</style>
pages/parent/child2.vue
<template>
<div>
<h2>嵌套子組件中的頁面child2</h2>
<p>foobar:{{foobar}}</p>
</div>
</template>
<script>
export default {
props: ['foobar']
}
</script>
<style scoped>
</style>
效果如下:


補(bǔ)充知識(shí):nuxt二級(jí)路由
耗費(fèi)了大半天的時(shí)間,終于把頁面的二級(jí)路由配置好了
先看我的目錄

如果沒有登陸頁,根本就不用考慮嵌套路由的問題,主要的menu跳轉(zhuǎn)和<nuxt />可以直接寫到layouts/default.vue中,首頁可以放到pages/index.vue,就可以了。
好了,步入核心的
情景,在中間件middleware/authenticated.js
// 定義了一個(gè)中間件, 如果用戶未登錄, 則跳轉(zhuǎn)登錄頁。
export default function ({
store,
redirect
}) {
if (!store.state.user) {
return redirect('/login')
}
}
首先,需要知道,pages/index.vue這個(gè)文件必須有,這是給路由'/',定義的頁面,但是我真正的首頁是在user/index.vue
pages/index.vue下
<template>
<div style="height:100%;">
</div>
</template>
<script>
export default {
created () {
console.log(this.$router)
this.$router.push('/login') // 頁面加載時(shí)跳轉(zhuǎn)
}
}
</script>
意思是加載二級(jí)路由的pages/users.vue頁面
<template> <div style="height:100%;"> <el-container style="height:100%"> <el-header class="theme-bg-color"> <my-head /> </el-header> <el-container style="height:100%;"> <my-side /> <el-main> <NuxtChild :key="key"/> </el-main> </el-container> </el-container> </div> </template>
<script>
import MySide from '~/components/MySide.vue'
import MyHead from '~/components/MyHead.vue'
export default {
components: {
MySide,
MyHead
},
computed: {
key() {
return this.$route.name !== undefined? this.$route.name +new Date(): this.$route +new Date()
}
}
}
</script>
注意,在pages/users/index.vue頁面中
export default {
name: 'users'
}
其他頁面,比如pages/users/ditch.vue頁面中
export default {
name: 'users-ditch'
}
一定要這樣去寫name,官網(wǎng)上也是這樣說明的。
總結(jié),嵌套路由(二級(jí)路由寫法)
一,頁面有個(gè)user.vue,文件夾也要有個(gè)同名的user;
二,最好有index.vue頁面;
三,name格式。
源碼地址:
https://github.com/besswang/rj-payadmin-nuxt
以上這篇Nuxt 嵌套路由nuxt-child組件用法(父子頁面組件的傳值)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3動(dòng)態(tài)修改打包后的請(qǐng)求路徑的操作代碼
這篇文章主要介紹了vue3動(dòng)態(tài)修改打包后的請(qǐng)求路徑,需要我們創(chuàng)建一個(gè)靜態(tài)資源里的外部文件來實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
vue.js中引入vuex儲(chǔ)存接口數(shù)據(jù)及調(diào)用的詳細(xì)流程
這篇文章主要給大家介紹了關(guān)于在vue.js中引入vuex儲(chǔ)存接口數(shù)據(jù)及調(diào)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
vue3響應(yīng)式Object代理對(duì)象的讀取示例詳解
這篇文章主要為大家介紹了vue3響應(yīng)式Object代理對(duì)象的讀取示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
vue3+vite+antd如何實(shí)現(xiàn)自定義主題
這篇文章主要介紹了vue3+vite+antd如何實(shí)現(xiàn)自定義主題問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue打包通過image-webpack-loader插件對(duì)圖片壓縮優(yōu)化操作
這篇文章主要介紹了vue打包通過image-webpack-loader插件對(duì)圖片壓縮優(yōu)化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue倒計(jì)時(shí)3秒后返回首頁Demo(推薦)
這篇文章主要介紹了Vue倒計(jì)時(shí)3秒后返回首頁Demo,倒計(jì)時(shí)結(jié)束后要清除計(jì)時(shí)器,防止內(nèi)存泄漏,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11
Vue中錯(cuò)誤圖片的處理的實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue中錯(cuò)誤圖片的處理的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

