“约见”面试官系列之常见面试题第二十四篇之vue-router使用(建议收藏)
生活随笔
收集整理的這篇文章主要介紹了
“约见”面试官系列之常见面试题第二十四篇之vue-router使用(建议收藏)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
開發(fā)的時候有時候會遇到一種情況,比如 :點(diǎn)擊這個鏈接跳轉(zhuǎn)到其他組件的情況,通常會跳轉(zhuǎn)到新的頁面,蛋是,我們不想跳轉(zhuǎn)到新頁面,只在當(dāng)前頁面切換著顯示,那么就要涉及到路由的嵌套了,也可以說是子路由的使用。
?
以餓了么訂餐的情景來說吧,在同個頁面,切換顯示不同組件的相應(yīng)內(nèi)容,同時地址欄的地址是會變的
怎么實(shí)現(xiàn)它呢?
首先?我們在導(dǎo)航組件navbar.vue中寫了三個導(dǎo)航鏈接,他們對應(yīng)地址分別為:/food,/rating,/seller,點(diǎn)擊每個導(dǎo)航鏈接會跳轉(zhuǎn)到不同的組件,并且加上<router-view></router-view>這個標(biāo)簽
navbar.vue:
<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>然后,我們在index.vue引入navbar.vue:
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">店內(nèi)免費(fèi)涮煲,(蔬菜、小料、主食、糕點(diǎn)、涼菜、水果、免費(fèi)吃)聞香識辣,入口知麻,一鍋兩吃,獨(dú)具特色!!!外賣米飯請自點(diǎn)!!評價問題商家會一一看,可能不能及時回復(fù)。有問題詳詢18232966036</p></div><!--在這里引入navbar組件在這里引入navbar組件在這里引入navbar組件在這里引入navbar組件--><navbar></navbar><!--在這里引入navbar組件在這里引入navbar組件在這里引入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>?
最后,路由都是怎么配的呢,在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}]},本面試題為前端??济嬖囶},后續(xù)有機(jī)會繼續(xù)完善。我是歌謠,一個沉迷于故事的講述者。
歡迎一起私信交流。
“睡服“面試官系列之各系列目錄匯總(建議學(xué)習(xí)收藏)?
總結(jié)
以上是生活随笔為你收集整理的“约见”面试官系列之常见面试题第二十四篇之vue-router使用(建议收藏)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 074-enable-right-cli
- 下一篇: JPL星历文件de405下载方法