Vue通過vue-router實現(xiàn)頁面跳轉(zhuǎ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文件中包含了所需的路由信息,詳細操作如下代碼,注釋詳解。
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
//以上語句說明,當我們訪問到'/content'路由時,就會跳轉(zhuǎn)到Content組件,顯示該vue頁面
},{
//路由路徑
path: '/main',
//名字
name:'main',
//跳轉(zhuǎn)的組件
component:Main
}
]
})三、router跳轉(zhuǎn)
上面把我們需要做的東西裝備好之后,現(xiàn)在來實現(xiàn)一下路由跳轉(zhuǎn)的功能。
流程:

main.js代碼:
import Vue from 'vue'
import App from './App'
//文件在當前目錄下的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ù)交互的小伙伴來說還是有點幫助的。
以上就是Vue通過vue-router實現(xiàn)頁面跳轉(zhuǎn)的全過程的詳細內(nèi)容,更多關(guān)于Vue vue-router頁面跳轉(zhuǎn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決vue 按鈕多次點擊重復(fù)提交數(shù)據(jù)問題
這篇文章主要介紹了vue 按鈕多次點擊重復(fù)提交數(shù)據(jù)的問題,本文通過實例結(jié)合的形式給大家介紹的非常詳細,需要的朋友可以參考下2018-05-05
vue關(guān)于點擊詳情頁面keep-alive的緩存問題
這篇文章主要介紹了vue關(guān)于點擊詳情頁面keep-alive的緩存問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
vue3+vite項目中按需引入vant報錯:Failed?to?resolve?import的解決方案
最近在vue項目中引入vant的時候發(fā)現(xiàn)報錯了,經(jīng)過嘗試發(fā)現(xiàn)了問題,現(xiàn)將完整引入流程提供給大家參考,下面這篇文章主要給大家介紹了關(guān)于vue3+vite項目中按需引入vant報錯:Failed?to?resolve?import的解決方案,需要的朋友可以參考下2022-12-12

