這篇文章主要介紹了vue中的router-view組件的使用教程,非常不錯,具有一定的參考借鑑價值,需要的朋友可以參考下
開發的時候有時候會遇到比如 點擊這個鏈接跳轉到其他組件的情況,氮素,我們不想跳轉到新頁面,只在當前頁面切換着顯示,那麼就要涉及到路由的嵌套了,也可以說是子路由的使用。
比如我們在一個導航組件中寫了三個導航鏈接,他們的地址分別爲:/food,/rating,/seller,點擊每個導航鏈接都會跳轉到不同的組件,我們通過<router-view></router-view>
<template> <div class="navbar"> <ul id="main"> <li><router-link to="/food" >商品</router-link></li> <li><router-link to="/rating">評價</router-link></li> <li><router-link to="/seller">商家</router-link></li> </ul> <!-- 路由匹配到的組件將渲染在這裏 --> <router-view></router-view> </div> </template>
顯示粗來的navbar就是這樣的,在同個頁面顯示,地址欄也是變的, 我們在index這個組件引入navbar組件,頭部那些不相干的logo啊基本信息可以忽略一下下
那麼他們的路由都是怎麼配的呢,在index.js中:
{ path: '/', name: 'index', component: index, redirect:'/food', children:[ { path: 'food', name: 'food', component: food }, { path: 'seller', name: 'seller', component: seller }, { path: 'rating', name: 'rating', component: rating } ] },
多加張圖解釋一下哈
上張圖片註釋的單詞打錯了,是“index”,不是“iindex”,個最後順便附上index.vue的代碼,這樣好理解一點
<template> <div class="index"> <div class="nav"></div> <div class="shop-header"> <div class="imgbox"><img src="../../static/img/56.jpg" alt="" /></div> <h2>黃蜀郞雞公煲<span class="ico"></span></h2> <p class="info1"><span>*4.6</span><span>月售738</span><span>商家配送約44分鐘</span><span>距離345m</span></p> <p class="info2">店內免費涮煲,(蔬菜、小料、主食、糕點、涼菜、水果、免費吃)聞香識辣,入口知麻,一鍋兩吃,獨具特色!!!外賣米飯請自點!!評價問題商家會一一看,可能不能及時回覆。有問題詳詢18232966036</p> </div> <navbar></navbar> </div> </template> <script>
import navbar from '@/components/navbar' import food from '@/components/food' export default { name: 'HelloWorld', data() { return { msg:[] } }, components: { navbar } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style lang="stylus"> @import '../../static/css/index.styl'; </style>
總結
以上所述是小編給大家介紹的vue中的router-view組件的使用教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對神馬文庫網站的支持!