欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Nuxt 嵌套路由nuxt-child組件用法(父子頁面組件的傳值)

 更新時間:2020年11月05日 14:32:21   作者:muzidigbig  
這篇文章主要介紹了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)文章

最新評論