詳解windows下vue-cli及webpack 構(gòu)建網(wǎng)站(四) 路由vue-router的使用
windows下vue-cli及webpack 構(gòu)建網(wǎng)站(一)環(huán)境安裝
windows下vue-cli及webpack 構(gòu)建網(wǎng)站(二)導(dǎo)入bootstrap樣式
windows下vue-cli及webpack 構(gòu)建網(wǎng)站(三)使用組件
1、本篇文章是建立在以上三篇文章的基礎(chǔ)上的。
2、安裝 vue-router 插件,運(yùn)行cmd進(jìn)入到項(xiàng)目目錄下面,運(yùn)行以下命令:
cnpm install vue-router --save-dev

3、在src文件夾下面新建一個(gè)文件夾page用于存放模板文件,然后分別在這個(gè)文件夾下面新建 index.vue、list.vue兩個(gè)文件,然后打開index.vue粘貼以下代碼:
<template>
<div class="jumbotron">
<h1>這里是首頁!</h1>
</div>
</template>
保存之后再打開list.vue粘貼以下代碼:
<template>
<div class="list-group">
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item active">
這里是列表頁
</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item">Dapibus ac facilisis in</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item">Morbi leo risus</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item">Porta ac consectetur ac</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item">Vestibulum at eros</a>
</div>
</template>
好了,兩個(gè)頁面的內(nèi)容都準(zhǔn)備好了,接下來我們修改入口文件app.vue的內(nèi)容吧
4、打開src文件夾下面的app.vue文件,修改代碼為
<template>
<div>
<HtmlHeader></HtmlHeader>
<router-view
class="view"
keep-alive
transition
transition-mode="out-in">
</router-view>
<HtmlFooter></HtmlFooter><span style="white-space:pre"> </span>
</div>
<span style="white-space:pre"> </span>
</template>
<script>
import HtmlHeader from './components/header'
import HtmlFooter from './components/footer'
export default {
components: {
HtmlHeader,
HtmlFooter
}
}
</script>
這里用了 router-view 來把剛才新建的兩個(gè)頁面加載到這里來
修改了入口文件接下來就是要進(jìn)行路由規(guī)則的配置了。
5、在src文件夾下面新建一個(gè)文件夾config用來存放路由配置,在config文件夾下面新建routes.js文件并打開,然后粘貼以下代碼并保存:
//加載模板文件
import index from '../page/index'
import list from '../page/list'
//路由規(guī)則設(shè)置
export default [
{
path: '/',
component: index
},
{
path: '/list',
component: list
}
]
現(xiàn)在路由配置文件也已經(jīng)配置好了,我們接下來就是要打開sec文件夾下面的main.js文件設(shè)置路由使用了
6、打開main.js 文件,在頭部加入以下代碼
// 引用路由插件
import VueRouter from 'vue-router'
// 試用路由插件
Vue.use(VueRouter)
//引入路由配置文件
import routes from './config/routes'
// 使用配置文件規(guī)則
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: routes })
這個(gè)是引入路由插件并且使用,然后加載路由規(guī)則
接著把
new Vue({
el: '#app',
template: '<App/>',
components: { App }
})
修改為
const app = new Vue({
router: router,
render: h => h(App)
}).$mount('#app')

設(shè)置完之后整個(gè)頁面代碼如圖
7、加載開始運(yùn)行 npm run dev 查看效果吧,打開http://localhost:8080 和http://localhost:8080/list 就可以看到不同的效果了
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3使用MD5加密實(shí)戰(zhàn)案例(清晰明了)
MD5是一種信息摘要算法(對(duì)稱加密),一種被廣泛使用的密碼散列函數(shù),可以產(chǎn)生出一個(gè)128位(16字節(jié))的散列值,用來確保信息傳輸完整一致性,這篇文章主要給大家介紹了關(guān)于Vue3使用MD5加密的相關(guān)資料,需要的朋友可以參考下2023-05-05
element?ui?watch?el-input賦值之后無法刪除或修改問題
這篇文章主要介紹了element?ui?watch?el-input賦值之后無法刪除或修改問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
vue通過element樹形控件實(shí)現(xiàn)樹形表格
這篇文章主要為大家介紹了vue?element樹形控件實(shí)現(xiàn)樹形表格,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11
vue react中的excel導(dǎo)入和導(dǎo)出功能
當(dāng)我們把信息化系統(tǒng)給用戶使用時(shí),用戶經(jīng)常需要把以前在excel里錄入的數(shù)據(jù)導(dǎo)入的信息化系統(tǒng)里,這樣為用戶提供了很大的方便,這篇文章主要介紹了vue中或者react中的excel導(dǎo)入和導(dǎo)出,需要的朋友可以參考下2023-09-09
electron-vue中報(bào)錯(cuò)Cannot?use?import?statement?outside?a?m
Electron 是一個(gè)使用 JavaScript、HTML 和 CSS 構(gòu)建桌面應(yīng)用程序的框架,下面這篇文章主要給大家介紹了關(guān)于electron-vue中報(bào)錯(cuò)Cannot?use?import?statement?outside?a?module的解決方案,需要的朋友可以參考下2023-02-02
ElementUI?復(fù)雜頂部和左側(cè)導(dǎo)航欄實(shí)現(xiàn)示例
本文主要介紹了ElementUI?復(fù)雜頂部和左側(cè)導(dǎo)航欄實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
vue radio單選框,獲取當(dāng)前項(xiàng)(每一項(xiàng))的value值操作
這篇文章主要介紹了vue radio單選框,獲取當(dāng)前項(xiàng)(每一項(xiàng))的value值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法
這篇文章主要介紹了VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法,需要的朋友參考下2018-02-02
Vue+elementui 實(shí)現(xiàn)復(fù)雜表頭和動(dòng)態(tài)增加列的二維表格功能
這篇文章主要介紹了Vue+elementui 實(shí)現(xiàn)復(fù)雜表頭和動(dòng)態(tài)增加列的二維表格功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09

