Vue 2.0+Vue-router構(gòu)建一個(gè)簡單的單頁應(yīng)用(附源碼)
一、介紹
vue.js 是 目前 最火的前端框架,vue.js 兼具 angular.js 和 react.js 的優(yōu)點(diǎn),并剔除它們的缺點(diǎn),并且提供了很多的周邊配套工具 如vue-router 、vue-resource 、vuex等等 ,通過他們我們可以很輕松的構(gòu)建一個(gè)大型單頁應(yīng)用。
目前Vue版本為:Vue2.0
官網(wǎng)地址:http://vuejs.org.cn/
查看API文檔:https://vuefe.cn/v2/api/
對(duì)比其他框架:http://vuejs.org.cn/guide/comparison.html
二、環(huán)境搭建
我們使用vue-cli腳手架工具構(gòu)建
#安裝 vue-cli npm install -g vue-cli #使用vue-cli初始化項(xiàng)目 vue init webpack vue-vuerouter-demo #進(jìn)到目錄 cd vue-vuerouter-demo #安裝依賴 npm install #開始運(yùn)行 npm run dev
瀏覽器訪問http://localhost:8080
構(gòu)建完成之后基本目錄結(jié)構(gòu)如下:

流程說明:
1、首先會(huì)打開首頁 也就是我們看到的index.html文件
2、使用webpack打包之后默認(rèn)加載main.js文件并將其引入到index.html文件中
三、開發(fā)
我們?cè)趍ain.js文件中引入相關(guān)模塊以及組件
import Vue from 'vue' import App from './App' import router from './router' //這里引入的是router目錄,會(huì)默認(rèn)識(shí)別里面的index.js文件(不能是其他名字) // 引入并使用vue-resource網(wǎng)絡(luò)請(qǐng)求模塊 import VueResource from 'vue-resource' Vue.use(VueResource)
實(shí)例化vue對(duì)象配置選項(xiàng)路由及渲染App組件
new Vue({
el: '#app', //這里綁定的是index.html中的id為app的div元素
router,
render: h => h(App)
// 這里的render: h => h(App)是es6的寫法
// 轉(zhuǎn)換過來就是: 暫且可理解為是渲染App組件
// render:(function(h){
// return h(App);
// });
})
App.vue文件是我們的組件入口,之后所有的開發(fā)在這里面進(jìn)行
<template>
<div id="app">
<!-- <hello></hello> -->
<div class="nav">
<!-- 使用 router-link 組件來導(dǎo)航. -->
<!-- 通過傳入 `to` 屬性指定鏈接. -->
<!-- <router-link> 默認(rèn)會(huì)被渲染成一個(gè) `<a>` 標(biāo)簽 -->
<ul>
<li><router-link to="/home">Home</router-link></li>
<li><router-link to="/about">About</router-link></li>
</ul>
</div>
<div class="main">
<!-- 路由匹配到的組件將渲染在這里 -->
<router-view></router-view>
</div>
</div>
</template>
<script>
// import Hello from './components/Hello'
export default {
name: 'app',
components: {
// Hello
}
}
</script>
<style>
body{
background-color: #f8f8ff;
font-family: 'Avenir', Helvetica, Arial, sans-serif;
color: #2c3e50;
}
.nav{
position: fixed;
width: 108px;
left: 40px;
}
.nav ul{
list-style: none;
margin: 0;
padding: 0;
}
.nav ul li{
width: 108px;
height: 48px;
line-height: 48px;
border:1px solid #dadada;
text-align: center;
}
.nav ul li a{
text-decoration: none;
}
.main{
height: 400px;
margin-left: 180px;
margin-right: 25px;
}
</style>
要使用路由我們首先要在router/index.js文件中創(chuàng)建路由并配置路由映射 ,并通過export輸出router到main.js文件中
// 這里面負(fù)責(zé)寫路由映射,便于管理
// 引入路由模塊并使用它
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
// 創(chuàng)建路由實(shí)例并配置路由映射
// path:'*',redirect:'/home' 重定向到path是/home的映射
const router = new VueRouter({
routes:[{
path: '/home', component: require('../components/Home.vue')
},{
path: '/about', component: require('../components/About.vue')
},{
path:'*',redirect:'/home'
}]
})
// 輸出router
export default router;
上面配置了2個(gè)組件映射 分別Hme.vue組件和About組件,配置好之后我們就可以開始使用路由了
<!-- 使用 router-link 組件來導(dǎo)航. --> <!-- 通過傳入 `to` 屬性指定鏈接. --> <!-- <router-link> 默認(rèn)會(huì)被渲染成一個(gè) `<a>` 標(biāo)簽 --> <ul> <li><router-link to="/home">Home</router-link></li> <li><router-link to="/about">About</router-link></li> </ul>
<!-- 路由匹配到的組件將渲染在這里 --> <router-view></router-view>
點(diǎn)擊home和about導(dǎo)航會(huì)映射到對(duì)應(yīng)的組件,然后將組件渲染在</router-view>這里面
到此,整個(gè)流程我們已經(jīng)走通了。
接下來我們使用vue-resource網(wǎng)絡(luò)插件動(dòng)態(tài)加載數(shù)據(jù)并顯示出來
1、先在main.js文件中引入并使用vue-resource網(wǎng)絡(luò)請(qǐng)求模塊
import VueResource from 'vue-resource' Vue.use(VueResource)
2、創(chuàng)建Home.vue組件
我們需要在created鉤子函數(shù)中去請(qǐng)求網(wǎng)絡(luò),這里我們使用豆瓣的API去請(qǐng)求電影列表數(shù)據(jù),請(qǐng)求成功之后我們將其數(shù)據(jù)顯示到頁面中
<template>
<div class="home">
<h1>{{ msg }}</h1>
<ul>
<li v-for="article in articles">
<div class="m-img inl-block"><img v-bind:src="article.images.small"/></div>
<div class="m-content inl-block">
<div>{{article.title}}</div>
<div>年份:{{article.year}}</div>
<div>類型:{{article.subtype}}</div>
</div>
</li>
</ul>
</div>
</template>
<script>
// mounted 鉤子函數(shù) 這里去請(qǐng)求豆瓣數(shù)據(jù)
export default {
name: 'home',
data () {
return {
msg: '電影列表',
articles:[]
}
},
created:function(){ //這里mounted和created生命周期函數(shù)區(qū)別
this.$http.jsonp('https://api.douban.com/v2/movie/top250?count=10', {}, {
headers: {
},
emulateJSON: true
}).then(function(response) {
// 這里是處理正確的回調(diào)
console.log(response);
this.articles = response.data.subjects
// this.articles = response.data["subjects"] 也可以
}, function(response) {
// 這里是處理錯(cuò)誤的回調(diào)
console.log(response)
});
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
ul{
list-style: none;
margin: 0;
padding: 0;
}
ul li{
border-bottom: 1px solid #999;
padding: 10px 0;
}
.inl-block{
display: inline-block;
}
.m-img{
}
.m-content{
margin-left: 20px;
}
</style>
3、最后我們運(yùn)行npm run dev命令查看頁面顯示效果

OK,可以看到我們的數(shù)據(jù)成功加載出來了,可以點(diǎn)擊左側(cè)的導(dǎo)航來進(jìn)行導(dǎo)航內(nèi)容切換
總結(jié)
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
解決vue2中使用axios http請(qǐng)求出現(xiàn)的問題
下面小編就為大家分享一篇解決vue2中使用axios http請(qǐng)求出現(xiàn)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Ant Design Vue日期組件的時(shí)間限制方式
這篇文章主要介紹了Ant Design Vue日期組件的時(shí)間限制方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
vue中的get方法\post方法如何實(shí)現(xiàn)傳遞數(shù)組參數(shù)
這篇文章主要介紹了vue中的get方法\post方法如何實(shí)現(xiàn)傳遞數(shù)組參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue 框架之動(dòng)態(tài)綁定 css 樣式實(shí)例分析
這篇文章主要介紹了Vue 框架之動(dòng)態(tài)綁定 css 樣式的方法,本文通過分享小實(shí)例給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
從0到1構(gòu)建vueSSR項(xiàng)目之node以及vue-cli3的配置
這篇文章主要介紹了從0到1構(gòu)建vueSSR項(xiàng)目之node以及vue-cli3的配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
解決vue刷新頁面以后丟失store的數(shù)據(jù)問題
這篇文章主要介紹了解決vue刷新頁面以后丟失store的數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
在vue中高德地圖引入和軌跡的繪制的實(shí)現(xiàn)
這篇文章主要介紹了在vue中高德地圖引入和軌跡的繪制的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

