Vue通過vue-router實(shí)現(xiàn)頁面跳轉(zhuǎn)的全過程
一、準(zhǔn)備工作
1、創(chuàng)建一個Vue-cli程序
2、安裝vue-router
npm install vue-router --save-dev

3、刪除多余的東西

二、創(chuàng)建router
1、在src下創(chuàng)建router包

2、創(chuàng)建跳轉(zhuǎn)的component
分別創(chuàng)建一個Content.vue和Main.vue文件

3、在router包下創(chuàng)建index.js文件
index.js文件中包含了所需的路由信息,詳細(xì)操作如下代碼,注釋詳解。
import Vue from 'vue'//導(dǎo)入vue的語法
import VueRouter from 'vue-router'//導(dǎo)入vue-router
import Content from "../components/Content";//導(dǎo)入創(chuàng)建的組件
import Main from "../components/Main";
//安裝路由
Vue.use(VueRouter);//通過此語句使導(dǎo)入的VueRouter路由生效
//配置導(dǎo)出路由,注意VueRouter名要一致
export default new VueRouter({
routes:[{
//路由路徑 @RequestMapping相似
path: '/content',
//名字
name:'content',
//跳轉(zhuǎn)的組件
component:Content
//以上語句說明,當(dāng)我們訪問到'/content'路由時,就會跳轉(zhuǎn)到Content組件,顯示該vue頁面
},{
//路由路徑
path: '/main',
//名字
name:'main',
//跳轉(zhuǎn)的組件
component:Main
}
]
})三、router跳轉(zhuǎn)
上面把我們需要做的東西裝備好之后,現(xiàn)在來實(shí)現(xiàn)一下路由跳轉(zhuǎn)的功能。
流程:

main.js代碼:
import Vue from 'vue'
import App from './App'
//文件在當(dāng)前目錄下的router下,自動掃秒里面的路由配置
import router from './router'
Vue.config.productionTip = false
new Vue({
el: '#app',
//配置路由,以便全局使用
router,
components:{App},
template:'<App/>'
})App.vue代碼:
<template>
<div id="app">
<h1>Vue-Router</h1>
<!-- 控制路由 -->
<router-link to="/main">首頁</router-link>
<router-link to="/content">內(nèi)容頁</router-link>
<!-- 控制頁面展示 -->
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>效果:



四、總結(jié)
這部分內(nèi)容是比較簡單的了,但是我個人覺得對于原來是后端開發(fā)的想學(xué)一些關(guān)于vue頁面跳轉(zhuǎn),數(shù)據(jù)交互的小伙伴來說還是有點(diǎn)幫助的。
以上就是Vue通過vue-router實(shí)現(xiàn)頁面跳轉(zhuǎn)的全過程的詳細(xì)內(nèi)容,更多關(guān)于Vue vue-router頁面跳轉(zhuǎn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue實(shí)現(xiàn)商品加減計(jì)算總價的實(shí)例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)商品加減計(jì)算總價的實(shí)例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08
Vue如何實(shí)現(xiàn)驗(yàn)證碼輸入交互
這篇文章主要介紹了Vue實(shí)現(xiàn)驗(yàn)證碼輸入交互的示例,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2020-12-12
elementUI table表格動態(tài)合并的示例代碼
這篇文章主要介紹了elementUI table表格動態(tài)合并的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

