Vue學(xué)習(xí)筆記進(jìn)階篇之vue-router安裝及使用方法
介紹
vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,適合用于構(gòu)建單頁(yè)面應(yīng)用。vue的單頁(yè)面應(yīng)用是基于路由和組件的,路由用于設(shè)定訪問(wèn)路徑,并將路徑和組件映射起來(lái)。傳統(tǒng)的頁(yè)面應(yīng)用,是用一些超鏈接來(lái)實(shí)現(xiàn)頁(yè)面切換和跳轉(zhuǎn)的。在vue-router單頁(yè)面應(yīng)用中,則是路徑之間的切換,也就是組件的切換。
本文是基于上一篇文章(Vue學(xué)習(xí)筆記進(jìn)階篇——vue-cli安裝及介紹 )vue-cli腳手架工具的。
安裝
在終端通過(guò)cd命令進(jìn)入到上一篇文章中創(chuàng)建的my-demo1項(xiàng)目目錄里,然后使用以下命令進(jìn)行安裝:
npm install vue-router --save
--save參數(shù)的作用是在我們的包配置文件package.json文件中添加對(duì)應(yīng)的配置。安裝成功后, 可以查看package.json文件,你會(huì)發(fā)現(xiàn)多了"vue-router": "^2.7.0"的配置。如下:
"dependencies": { "vue": "^2.3.3", "vue-router": "^2.7.0" },
使用
通過(guò)以上步驟,我們已經(jīng)安裝好了vue-router,但是在vue-cli中我們?nèi)绾问褂媚兀?br /> 首先,我們需要在main.js文件中導(dǎo)入并注冊(cè)vue-router:
//ES6語(yǔ)法導(dǎo)入 import VueRouter from 'vue-router' //注冊(cè) Vue.use(VueRouter)
然后便是實(shí)例化:
const router = new VueRouter({ mode: 'history', routes:[ {path: '/', component:DemoHome}, {path: '/about', component:DemoAbout}, {path: '/contact', component:DemoContact} ] })
path: 是路由的路徑。
component: 是該路由需要渲染的組件。
上面代碼中的DemoHome, DemoAbout, DemoContact都是單文件組件,所以我們同樣需要?jiǎng)?chuàng)建上面三個(gè)組件,并導(dǎo)入到當(dāng)前文件。這三個(gè)組件我們只是作為示例來(lái)使用,所以比較簡(jiǎn)單,代碼分別如下:
DemoHome.vue:
<template> <div id="home"> <h2>this is home</h2> </div> </template> <script> export default({ name:'home' }) </script> <style scoped> #home{ width: 100%; height: 500px; background-color: khaki; } </style>
DemoAbout.vue:
<template> <div id="about"> <h2>this is about</h2> </div> </template> <script> export default({ name:'about' }) </script> <style scoped> #about{ width: 100%; height: 500px; background-color: antiquewhite; } </style>
DemoContact.vue:
<template> <div id="contact"> <h2>this is contact</h2> </div> </template> <script> export default({ name:'contact' }) </script> <style scoped> #contact{ width: 100%; height: 500px; background-color: lightskyblue; } </style>
創(chuàng)建好以上組件后,再使用ES6語(yǔ)法導(dǎo)入到main.js:
import DemoHome from './components/DemoHome' import DemoAbout from './components/DemoAbout' import DemoContact from './components/DemoContact'
最后在Vue實(shí)例中加入路由屬性就可以了
new Vue({ el: '#app', router, template: '<App/>', components: { App } })
完整的main.js應(yīng)該是這樣:
import Vue from 'vue' import VueRouter from 'vue-router' import App from './App' import DemoHome from './components/DemoHome' import DemoAbout from './components/DemoAbout' import DemoContact from './components/DemoContact' Vue.use(VueRouter) Vue.config.productionTip = false const router = new VueRouter({ mode: 'history', routes:[ {path: '/', component:DemoHome}, {path: '/about', component:DemoAbout}, {path: '/contact', component:DemoContact} ] }) /* eslint-disable no-new */ new Vue({ el: '#app', router, template: '<App/>', components: { App } })
在這里我們?yōu)榱藢W(xué)習(xí),所以我們簡(jiǎn)單的做個(gè)布局。接下來(lái),我會(huì)再創(chuàng)建兩個(gè)組件,一個(gè)叫DemoHeader, 一個(gè)叫DemoFooter。DemoHeader里面我放一個(gè)logo的圖片,和導(dǎo)航,而這個(gè)導(dǎo)航的路由也將會(huì)使用我們前面定義的路由;DemoFooter就比較簡(jiǎn)單了,放一些footer信息。
下面我們看下這兩個(gè)組件的代碼:
DemoHeader.vue:
<template> <div id="header" class="wrap"> <div class="header"> <h1 class="logo"> <router-link to="/">  </router-link> </h1> </div> <div class="top-nav"> <div id="navList" class="navlist-wrap"> <div class="navlist clearfix"> <span class="nav-btn"> <router-link to="/">首頁(yè)</router-link> </span> <span class="nav-btn"> <router-link to="/about">關(guān)于</router-link> </span> <span class="nav-btn"> <router-link to="/contact">聯(lián)系方式</router-link> </span> </div> </div> </div> </div> </template> <script> export default({ name:'header', data:function () { return { 'nav-btn': 'nav-btn' } } }) </script> <style scoped> .header{width:1105px;margin:0 auto;height:111px;padding:12px 0 18px;position:relative;*z-index:1} .header .logo{height:86px;width:256px;margin-top:25px} .top-nav .navlist-wrap{width:1050px;margin:0 auto;position:relative} .top-nav .navlist{position:absolute;right:130PX;top:-40PX} .top-nav .navlist .nav-btn { float:left; margin-left:60px; color:#666; vertical-align: middle; text-decoration:none; font-size: large; } </style>
在上面的代碼中,我們看到了一個(gè)陌生的標(biāo)簽,<router-link>這個(gè)是什么玩意呢?其實(shí)他就是vue-router集成的一個(gè)組件,渲染出來(lái)的是一個(gè)<a>標(biāo)簽。而他的屬性to其實(shí)就是一個(gè)props屬性,這里面的意思就是路由的路徑,與前面定義的路由path對(duì)應(yīng)。關(guān)于router-link的更多介紹可以看官網(wǎng)router-link API文檔
DemoFooter.vue:
<template> <div id="footer"> <span>Copyright © <a rel="external nofollow" >Chain</a>. All rights reserved</span> </div> </template> <script> export default({ name:'footer' }) </script> <style scoped> #footer { height:50px; position:fixed; bottom:0px; left: 0px; background-color: #eeeeee; width: 100%; padding-top: 10px; } </style>
我們的組件都已經(jīng)創(chuàng)建好了,接下來(lái)的事情就是把他們組合到一起。這個(gè)組合,我們就用App.vue來(lái)實(shí)現(xiàn)吧。
先整理下我們的思路啊:
在我們的頁(yè)面上,我們需要把DemoHeader, DemoFooter放進(jìn)去,而我們的DemoHeader里面定義了導(dǎo)航,我們希望把導(dǎo)航出來(lái)的組件放到header和footer之間。所以大致應(yīng)該是這個(gè)樣組合:
<demo-header></demo-header> <!-- 根據(jù)路由顯示的組件 --> <!-- TO DO --> <demo-footer></demo-footer>
下面看下完整的代碼吧:
<template> <div id="app"> <demo-header></demo-header> <router-view></router-view> <demo-footer></demo-footer> </div> </template> <script> import DemoHeader from './components/DemoHeader' import DemoFooter from './components/DemoFooter' export default { name: 'app', components: { DemoHeader, DemoFooter } } </script> <style> #app { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; background-color: aliceblue; } </style>
同樣的道理,我們要是想使用一個(gè)組件,導(dǎo)入和注冊(cè)的步驟是少不了的。
導(dǎo)入:
import DemoHeader from './components/DemoHeader' import DemoFooter from './components/DemoFooter'
注冊(cè):
components: { DemoHeader, DemoFooter }
在上面的代碼中我們又發(fā)現(xiàn)了個(gè)陌生標(biāo)簽<router-view>這個(gè)標(biāo)簽同樣是vue-router的一個(gè)內(nèi)部組件,實(shí)際上它是一個(gè)是一個(gè) functional 組件。具體信息可以去官網(wǎng)router-viewAPI文檔詳細(xì)了解。它的作用就是渲染路由導(dǎo)航過(guò)來(lái)的組件,也就是這個(gè)標(biāo)簽內(nèi)就是我們放置DemoHome, DemoAbout, DemoContact的地方。
因?yàn)樗彩莻€(gè)組件,所以可以配合 <transition> 和 <keep-alive> 使用。如果兩個(gè)結(jié)合一起用,要確保在內(nèi)層使用 <keep-alive>, 添加上述兩個(gè)標(biāo)簽后的template代碼如下:
<template> <div id="app"> <demo-header></demo-header> <transition name="fade" mode="out-in"> <keep-alive> <router-view></router-view> </keep-alive> </transition> <demo-footer></demo-footer> </div> </template>
再添加一個(gè)簡(jiǎn)單的淡入淡出的樣式:
.fade-enter-active, .fade-leave-active{ transition: all .3s; } .fade-enter, .fade-leave-to{ opacity: 0; }
通過(guò)上面的代碼,我們發(fā)現(xiàn)之前學(xué)過(guò)的過(guò)渡這里都可以使用,可參考Vue學(xué)習(xí)筆記進(jìn)階篇——單元素過(guò)度
最后我們看下我們做了半天的成果吧:
首頁(yè)
關(guān)于
聯(lián)系方式
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue?+?ElementUI表格內(nèi)實(shí)現(xiàn)圖片點(diǎn)擊放大效果的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Vue?+?ElementUI表格內(nèi)實(shí)現(xiàn)圖片點(diǎn)擊放大效果的兩種實(shí)現(xiàn)方式,第一種使用el-popover彈出框來(lái)實(shí)現(xiàn)而第二種使用v-viewer插件實(shí)現(xiàn),需要的朋友可以參考下2024-08-08vue.extend與vue.component的區(qū)別和聯(lián)系
這篇文章主要介紹了vue.extend與vue.component的區(qū)別和聯(lián)系,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-09-09Vue Vine實(shí)現(xiàn)一個(gè)文件中寫(xiě)多個(gè)組件的方法(最近很火)
Vue Vine提供了全新Vue組件書(shū)寫(xiě)方式,主要的賣(mài)點(diǎn)是可以在一個(gè)文件里面寫(xiě)多個(gè)vue組件,Vue Vine是一個(gè)vite插件,vite解析每個(gè)模塊時(shí)都會(huì)觸發(fā)插件的transform鉤子函數(shù),本文介紹Vue Vine是如何實(shí)現(xiàn)一個(gè)文件中寫(xiě)多個(gè)組件,感興趣的朋友一起看看吧2024-07-07