Nuxt 嵌套路由nuxt-child組件用法(父子頁面組件的傳值)
Nuxt嵌套路由官網(wǎng)上的API詳解:點擊鏈接
看了官網(wǎng)上的api實現(xiàn)了官網(wǎng)的案例你會發(fā)現(xiàn)訪問父頁面中只能顯示父頁面中的內(nèi)容,要想默認的在<nuxt-child>區(qū)域顯示一個頁面內(nèi)容怎么辦?
自己案例代碼:
pages/parent.vue
<template> <div> <h2>父組件的頁面的內(nèi)容</h2> <ul> <!-- 進行切換子頁面,寫法同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)于是子頁面組件的占位符;嵌套中這個不可少 --> <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>嵌套子組件中的默認頁面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>
效果如下:
補充知識:nuxt二級路由
耗費了大半天的時間,終于把頁面的二級路由配置好了
先看我的目錄
如果沒有登陸頁,根本就不用考慮嵌套路由的問題,主要的menu跳轉(zhuǎn)和<nuxt />可以直接寫到layouts/default.vue中,首頁可以放到pages/index.vue,就可以了。
好了,步入核心的
情景,在中間件middleware/authenticated.js
// 定義了一個中間件, 如果用戶未登錄, 則跳轉(zhuǎn)登錄頁。 export default function ({ store, redirect }) { if (!store.state.user) { return redirect('/login') } }
首先,需要知道,pages/index.vue這個文件必須有,這是給路由'/',定義的頁面,但是我真正的首頁是在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') // 頁面加載時跳轉(zhuǎn) } } </script>
意思是加載二級路由的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é),嵌套路由(二級路由寫法)
一,頁面有個user.vue,文件夾也要有個同名的user;
二,最好有index.vue頁面;
三,name格式。
源碼地址:
https://github.com/besswang/rj-payadmin-nuxt
以上這篇Nuxt 嵌套路由nuxt-child組件用法(父子頁面組件的傳值)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue長列表優(yōu)化之虛擬列表實現(xiàn)過程詳解
前端的業(yè)務(wù)開發(fā)中會遇到不使用分頁方式來加載長列表的需求,下面這篇文章主要給大家介紹了關(guān)于vue長列表優(yōu)化之虛擬列表實現(xiàn)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08vue.js實現(xiàn)仿原生ios時間選擇組件實例代碼
本篇文章主要介紹了vue.js實現(xiàn)仿原生ios時間選擇組件實例代碼,具有一定的參考價值,有興趣的可以了解一下。2016-12-12vue3使用vue-i18n的方法詳解(ts中使用$t,?vue3不用this)
所謂的vue-i18n國際化方案就是根據(jù)它的規(guī)則自己建立一套語言字典,對于每一個字(message)都有一個統(tǒng)一的標(biāo)識符,下面這篇文章主要給大家介紹了關(guān)于vue3使用vue-i18n(ts中使用$t,?vue3不用this)的相關(guān)資料,需要的朋友可以參考下2022-12-12淺談Vue使用Cascader級聯(lián)選擇器數(shù)據(jù)回顯中的坑
這篇文章主要介紹了淺談Vue使用Cascader級聯(lián)選擇器數(shù)據(jù)回顯中的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10vue使用@scroll監(jiān)聽滾動事件時,@scroll無效問題的解決方法詳解
這篇文章主要介紹了vue使用@scroll監(jiān)聽滾動事件時,@scroll無效問題的解決方法,結(jié)合實例形式分析了@scroll監(jiān)聽滾動事件無效問題的原因及相應(yīng)的解決方法,需要的朋友可以參考下2019-10-10在vue中使用el-tab-pane v-show/v-if無效的解決
這篇文章主要介紹了在vue中使用el-tab-pane v-show/v-if無效的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08