Vue-旅游網(wǎng)站

1.項(xiàng)目實(shí)現(xiàn)功能

首頁、詳情頁面坡慌、搜索頁面、城市選擇頁面

項(xiàng)目目錄

F:.
│  .babelrc
│  .editorconfig
│  .eslintignore
│  .eslintrc.js
│  .gitignore
│  .postcssrc.js
│  index.html
│  package-lock.json
│  package.json
│  README.en.md
│  README.md
│
├─build
│      build.js
│      check-versions.js
│      logo.png
│      utils.js
│      vue-loader.conf.js
│      webpack.base.conf.js
│      webpack.dev.conf.js
│      webpack.prod.conf.js
│
├─config
│      dev.env.js
│      index.js
│      prod.env.js
│
├─src
│  │  App.vue
│  │  main.js
│  │
│  ├─assets
│  │  └─styles
│  │      │  border.css
│  │      │  iconfont.css
│  │      │  mixins.styl
│  │      │  reset.css
│  │      │  varibles.styl
│  │      │
│  │      └─iconfont
│  │              iconfont.eot
│  │              iconfont.svg
│  │              iconfont.ttf
│  │              iconfont.woff
│  │
│  ├─common
│  │  ├─fade
│  │  │      FadeAnimation.vue
│  │  │
│  │  └─gallary
│  │          Gallary.vue
│  │
│  ├─pages
│  │  │  testGit.js
│  │  │
│  │  ├─city
│  │  │  │  City.vue
│  │  │  │
│  │  │  └─components
│  │  │          Alphabet.vue
│  │  │          Header.vue
│  │  │          List.vue
│  │  │          Search.vue
│  │  │
│  │  ├─detail
│  │  │  │  Detail.vue
│  │  │  │
│  │  │  └─components
│  │  │          Banner.vue
│  │  │          Header.vue
│  │  │          List.vue
│  │  │
│  │  └─home
│  │      │  Home.vue
│  │      │
│  │      └─components
│  │              Header.vue
│  │              Icons.vue
│  │              Recommend.vue
│  │              Swiper.vue
│  │              Weekend.vue
│  │
│  ├─router
│  │      index.js
│  │
│  └─store
│          index.js
│          mutations.js
│          state.js
│
└─static
        .gitkeep

2.實(shí)現(xiàn)細(xì)節(jié)和一些難點(diǎn)

數(shù)據(jù)獲取

獲取首頁數(shù)據(jù)使用的是axios
安裝 axios:
npm install axios --save

在 Home.vue 發(fā)送 Ajax 請(qǐng)求是最好的選擇藻三,這個(gè)組件獲取 Ajax 數(shù)據(jù)之后洪橘,可以把數(shù)據(jù)傳給每個(gè)子組件

把一些靜態(tài)的文件放置在static目錄下,通過 http://localhost:8080/static/mock/index.json 可以訪問到

static
│  .gitkeep
│
└─mock
        city.json
        detail.json
        index.json

Home.vue 部分代碼

<template>
  <div>
    <home-header></home-header>
    <home-swiper :list="swiperList"></home-swiper>
    <home-icons :list="iconList"></home-icons>
    <home-recommend :list="recommendList"></home-recommend>
    <home-weekend :list="weekendList"></home-weekend>
  </div>
</template>
<script>
import HomeHeader from ‘./components/Header’
import HomeSwiper from ‘./components/Swiper’
import HomeIcons from ‘./components/Icons’
import HomeRecommend from ‘./components/Recommend’
import HomeWeekend from ‘./components/Weekend’
import axios from ‘a(chǎn)xios’
import { mapState } from ‘vuex’
export default {
name: ‘Home’,
components: {
HomeHeader,
HomeSwiper,
HomeIcons,
HomeRecommend,
HomeWeekend
},
data () {
return {
lastCity: ‘’,
swiperList: [],
iconList: [],
recommendList: [],
weekendList: []
}
},
computed: {
…mapState([‘city’])
},
methods: {
getHomeInfo () {
axios.get(’/api/index.json?city=’ + this.city)
.then(this.getHomeInfoSucc)
},
getHomeInfoSucc (res) {
res = res.data
if (res.ret && res.data) {
const data = res.data
this.swiperList = data.swiperList
this.iconList = data.iconList
this.recommendList = data.recommendList
this.weekendList = data.weekendList
}
}
},
mounted () {
this.lastCity = this.city
this.getHomeInfo()
}
}
</script>
<style>
</style>

父子組件之間進(jìn)行通訊

父組件通過 props 傳遞數(shù)據(jù)給子組件棵帽,子組件通過 emit 發(fā)送事件傳遞數(shù)據(jù)給父組件

以 List 組件 為例(List.vue 部分代碼)

<template>
  <div>
    <div class="title">熱銷推薦</div>
    <ul>
      <router-link
        tag="li"
        class="item border-bottom"
        v-for="item of list"
        :key="item.id"
        :to="'/detail/' + item.id"
      >
        <img class="item-img" :src="item.imgUrl" />
        <div class="item-info">
          <p class="item-title">{{item.title}}</p>
          <p class="item-desc">{{item.desc}}</p>
          <button class="item-button">查看詳情</button>
        </div>
      </router-link>
    </ul>
  </div>
</template>
<script>
export default {
name: ‘HomeRecommend’,
props: {
list: Array
}
}
</script>

輪播圖

安裝 vue-awesome-swiper 插件

npm install vue-awesome-swiper@2.6.7 --save

輪播在多個(gè)組件中使用
以 home-components-Swiper.vue 為例

<template>
  <div class="wrapper">
    <swiper :options="swiperOption" v-if="showSwiper">
      <swiper-slide v-for="item of list" :key="item.id">
        <img class="swiper-img" :src="item.imgUrl" />
      </swiper-slide>
      <div class="swiper-pagination"  slot="pagination"></div>
    </swiper>
  </div>
</template>
<script>
export default {
name: ‘HomeSwiper’,
props: {
list: Array
},
data () {
return {
swiperOption: {
pagination: ‘.swiper-pagination’,
loop: true
}
}
},
computed: {
showSwiper () {
return this.list.length
}
}
}
</script>

Better-scroll

安裝

npm install better-scroll --save

使用

<div class="wrapper">
  <ul class="content">
    <li>...</li>
    <li>...</li>
    ...
  </ul>


import BScroll from '[@better-scroll](/user/better-scroll)/core'
let wrapper = document.querySelector('.wrapper')
let scroll = new BScroll(wrapper)

使用vuex實(shí)現(xiàn)數(shù)據(jù)共享

安裝vuex

npm install vuex --save

希望在 城市列表頁面 點(diǎn)擊城市熄求,首頁右上角城市可以 進(jìn)行相應(yīng)的改變。

具體描述為:

項(xiàng)目中是為了實(shí)現(xiàn)城市選擇列表頁面和首頁的數(shù)據(jù)傳遞逗概,并且沒有公用的組件弟晚,city/components/List.vue 、home/components/Header.vue逾苫、Home.vue組件卿城,都需要獲取到數(shù)據(jù)。

因?yàn)檫@個(gè)項(xiàng)目沒有需要進(jìn)行異步的操作铅搓,也不需要對(duì)數(shù)據(jù)進(jìn)行額外的處理瑟押,所以項(xiàng)目中只用到了 state 和 mutations。在 state 中存儲(chǔ)了 city 數(shù)據(jù)星掰,然后在 mutation 里定義事件類型和函數(shù) changeCity

store
    index.js
    mutations.js
    state.js

state.js

let defaultCity = '上海'
try {
  if (localStorage.city) {
    defaultCity = localStorage.city
  }
} catch (e) {}
export default {
city: defaultCity
}

mutations.js

export default {
  changeCity (state, city) {
    state.city = city
    try {
      localStorage.city = city
    } catch (e) {}
  }
}

index.js

import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import mutations from './mutations'

Vue.use(Vuex)

export default new Vuex.Store({
  state,
  mutations
})

Home.vue 組件多望,在計(jì)算屬性中,this.$store.state.xxx氢烘,在這個(gè)項(xiàng)目中是 this.$store.state.city可以獲取到 state 數(shù)據(jù)怀偷。當(dāng)然,為了使代碼更加簡(jiǎn)潔播玖,用 mapState 將 this.xxx 映射為 this.$store.state.xxx椎工。

在 List.vue 中,通過 commit 來觸發(fā) mutations 里面的方法進(jìn)行數(shù)據(jù)的修改。同樣晋渺,為了使代碼更加簡(jiǎn)潔镰绎,引入 mapMutations 將 this.changeCity(city) 映射為 this.$store.commit('changeCity', city)

【city/List.vue 具體是】

import { mapState, mapMutations } from 'vuex'

這樣就實(shí)現(xiàn)了這幾個(gè)組件的數(shù)據(jù)共享木西。

3.項(xiàng)目收獲

  1. 理解整個(gè)vue項(xiàng)目的開發(fā)流程畴栖,上手中小vue項(xiàng)目的開發(fā),技術(shù)點(diǎn)如下:
    Vue Router 來做多頁面的路由
    Vuex 多個(gè)組件的數(shù)據(jù)共享
    插件swiper實(shí)現(xiàn)頁面輪播效果
    Axios 來進(jìn)行 Ajax 數(shù)據(jù)的獲取

  2. 移動(dòng)端頁面布局技巧

  3. stylus 編寫前端的樣式

  4. 公用組件的拆分

  5. 規(guī)范的代碼編寫

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末八千,一起剝皮案震驚了整個(gè)濱河市吗讶,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌恋捆,老刑警劉巖照皆,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異沸停,居然都是意外死亡膜毁,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門愤钾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來瘟滨,“玉大人,你說我怎么就攤上這事能颁≡尤常” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵伙菊,是天一觀的道長(zhǎng)败玉。 經(jīng)常有香客問我,道長(zhǎng)镜硕,這世上最難降的妖魔是什么运翼? 我笑而不...
    開封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮谦疾,結(jié)果婚禮上南蹂,老公的妹妹穿的比我還像新娘。我一直安慰自己念恍,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開白布晚顷。 她就那樣靜靜地躺著峰伙,像睡著了一般。 火紅的嫁衣襯著肌膚如雪该默。 梳的紋絲不亂的頭發(fā)上瞳氓,一...
    開封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音栓袖,去河邊找鬼匣摘。 笑死店诗,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的音榜。 我是一名探鬼主播庞瘸,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼赠叼!你這毒婦竟也來了擦囊?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤嘴办,失蹤者是張志新(化名)和其女友劉穎瞬场,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體涧郊,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贯被,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了妆艘。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片刃榨。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖双仍,靈堂內(nèi)的尸體忽然破棺而出枢希,到底是詐尸還是另有隱情,我是刑警寧澤朱沃,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布苞轿,位于F島的核電站,受9級(jí)特大地震影響逗物,放射性物質(zhì)發(fā)生泄漏搬卒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一翎卓、第九天 我趴在偏房一處隱蔽的房頂上張望契邀。 院中可真熱鬧,春花似錦失暴、人聲如沸坯门。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽古戴。三九已至,卻和暖如春矩肩,著一層夾襖步出監(jiān)牢的瞬間现恼,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留叉袍,地道東北人始锚。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像喳逛,于是被迫代替她去往敵國(guó)和親瞧捌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容