vue項目實戰(zhàn)(二)- 旅游網(wǎng)站城市列表頁面開發(fā)

一袒哥、城市選頁-路由配置

打開router目錄下的index.js走诞,寫入配置:

import City from '@/pages/city/city'
export default new Router({
  routes: [{
    path: '/',
    name: 'Home',
    component: Home
  }, {
    path: '/city',
    name: 'City',
    component: City
  }]
})

在pages下新建city目錄,新建city.vue文件

<template>
  <div>
    <city-header></city-header>
  </div>
</template>
<script>
import CityHeader from './components/Header'
export default {
  name: 'City',
  components: {
    CityHeader,
  }
}
</script>
<style lang="stylus" scoped>
</style>

二妥曲、搜索框實現(xiàn)

<template>
  <div class="search">
    <input class="search-input" type="text" placeholder="輸入城市名或拼音" />
  </div>
</template>
<script>
export default {
  name: 'CitySearch'
}
</script>
<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .search
    height: .72rem
    padding: 0 .1rem
    background: $bgColor
    .search-input
      box-sizing: border-box
      width: 100%
      height: .62rem
      padding: 0 .1rem
      line-height: .62rem
      text-align: center
      border-radius: .06rem
      color: #666
</style>

三、列表布局

<template>
  <div class="list">
    <div class="area">
      <div class="title border-topbottom">您的位置</div>
      <div class="button-list">
        <div class="button-wrapper">
          <div class="button">北京</div>
        </div>
      </div>
    </div>
    <div class="area">
      <div class="title border-topbottom">熱門城市</div>
      <div class="button-list">
        <div class="button-wrapper">
          <div class="button">北京</div>
        </div>
        <div class="button-wrapper">
          <div class="button">北京</div>
        </div>
        <div class="button-wrapper">
          <div class="button">北京</div>
        </div>
        <div class="button-wrapper">
          <div class="button">北京</div>
        </div>
        <div class="button-wrapper">
          <div class="button">北京</div>
        </div>
      </div>
    </div>
    <div class="area">
      <div class="title border-topbottom">A</div>
      <div class="item-list">
        <div class="item border-bottom">阿拉爾</div>
        <div class="item border-bottom">阿拉爾</div>
        <div class="item border-bottom">阿拉爾</div>
        <div class="item border-bottom">阿拉爾</div>
        <div class="item border-bottom">阿拉爾</div>
      </div>
      <div class="title border-topbottom">B</div>
      <div class="item-list">
        <div class="item border-bottom">阿拉爾</div>
        <div class="item border-bottom">阿拉爾</div>
        <div class="item border-bottom">阿拉爾</div>
        <div class="item border-bottom">阿拉爾</div>
        <div class="item border-bottom">阿拉爾</div>
      </div>
      <div class="title border-topbottom">C</div>
      <div class="item-list">
        <div class="item border-bottom">阿拉爾</div>
        <div class="item border-bottom">阿拉爾</div>
        <div class="item border-bottom">阿拉爾</div>
        <div class="item border-bottom">阿拉爾</div>
        <div class="item border-bottom">阿拉爾</div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'CityList'
}
</script>
<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .border-topbottom
    &:before
      border-color: #777
    &:after
      border-color: #777
  .border-bottom
    &:before
      border-color: #777
  .list
    overflow: hidden
    position: absolute
    top: 1.58rem
    left: 0
    right: 0
    bottom: 0
    .title
      line-height: .4rem
      background: #EEEEEE
      padding-left: .2rem
      color: #666666
      font-size: .24rem
    .button-list
      overflow: hidden
      padding: .1rem .6rem .1rem .1rem
      .button-wrapper
        float: left
        width: 33%
        .button
          margin: .1rem
          padding: .1rem 0
          text-align: center
          border: .02rem solid #CCC
          border-radius: .06rem
    .item-list
      .item
        line-height: .76rem
        padding-left: .2rem
</style>

頁面效果:

image.png

四钦购、BetterScoll的使用

安裝:

npm install better-scoll --save

使用:

<div class="list" ref="wrapper">
import BScroll from 'better-scroll'
export default {
  name: 'CityList',
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  }
}

五檐盟、字母表布局

<template>
  <div class="list">
    <ul>
      <li class="item">A</li>
      <li class="item">A</li>
      <li class="item">A</li>
      <li class="item">A</li>
      <li class="item">A</li>
      <li class="item">A</li>
      <li class="item">A</li>
      <li class="item">A</li>
      <li class="item">A</li>
    </ul>
  </div>
</template>
<script>
export default {
  name: 'CityAlphabet'
}
</script>
<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .list
    display: flex
    flex-direction: column
    justify-content: center
    position: absolute
    top: 1.58rem
    right: 0
    bottom: 0
    width: .4rem
    .item
      line-height: .4rem
      text-align: center
      color: $bgColor
</style>

六、 兄弟組件傳值

不使用bus總線押桃,子組件通過$emit傳值給父組件葵萎,父組件再轉發(fā)給另外一個子組件

七、城市搜索功能實現(xiàn)

<template>
  <div>
    <div class="search">
      <input v-model="keyword" class="search-input" type="text" placeholder="輸入城市名或拼音" />
    </div>
    <div class="search-content" ref="search"
      v-show="keyword"
    >
      <ul>
        <li class="search-item border-bottom"
          v-for="item of list"
          :key="item.id">
          {{item.name}}
        </li>
        <li class="search-item border-bottom" v-show="hasNoData">
          沒有找到匹配數(shù)據(jù)
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import Bscroll from 'better-scroll'
export default {
  name: 'CitySearch',
  props: {
    cities: Object
  },
  data () {
    return {
      keyword: '',
      list: [],
      timer: null
    }
  },
  computed: {
    hasNoData () {
      return !this.list.length
    }
  },
  watch: {
    keyword () {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      if (!this.keyword) {
        this.list = []
        return
      }
      this.timer = setTimeout(() => {
        const result = []
        for (let i in this.cities) {
          this.cities[i].forEach((value) => {
            if (value.spell.indexOf(this.keyword) > -1 ||
              value.name.indexOf(this.keyword) > -1) {
              result.push(value)
            }
          })
        }
        this.list = result
      }, 100)
    }
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.search)
  }
}
</script>
<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .search
    height: .72rem
    padding: 0 .1rem
    background: $bgColor
    .search-input
      box-sizing: border-box
      width: 100%
      height: .62rem
      padding: 0 .1rem
      line-height: .62rem
      text-align: center
      border-radius: .06rem
      color: #666
  .search-content
    z-index: 1
    overflow:hidden
    position: absolute
    top: 1.58rem
    left: 0
    right: 0
    bottom: 0
    background: #eee
    .search-item
      line-height: .62rem
      padding-left: .2rem
      background: #FFFFFF
      color: #666666
</style>

八、vuex

vuex是什么:https://vuex.vuejs.org/zh/

九陌宿、導航

https://router.vuejs.org/zh/

編程式導航:

this.$router.push('/')
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末锡足,一起剝皮案震驚了整個濱河市波丰,隨后出現(xiàn)的幾起案子壳坪,更是在濱河造成了極大的恐慌,老刑警劉巖掰烟,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件爽蝴,死亡現(xiàn)場離奇詭異,居然都是意外死亡纫骑,警方通過查閱死者的電腦和手機蝎亚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來先馆,“玉大人发框,你說我怎么就攤上這事∶呵剑” “怎么了梅惯?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長仿野。 經(jīng)常有香客問我铣减,道長,這世上最難降的妖魔是什么脚作? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任葫哗,我火速辦了婚禮,結果婚禮上球涛,老公的妹妹穿的比我還像新娘劣针。我一直安慰自己,他們只是感情好亿扁,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布捺典。 她就那樣靜靜地躺著,像睡著了一般魏烫。 火紅的嫁衣襯著肌膚如雪辣苏。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天哄褒,我揣著相機與錄音稀蟋,去河邊找鬼。 笑死呐赡,一個胖子當著我的面吹牛退客,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼萌狂,長吁一口氣:“原來是場噩夢啊……” “哼档玻!你這毒婦竟也來了?” 一聲冷哼從身側響起茫藏,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤误趴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后务傲,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凉当,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年售葡,在試婚紗的時候發(fā)現(xiàn)自己被綠了看杭。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡挟伙,死狀恐怖楼雹,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情尖阔,我是刑警寧澤贮缅,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站诺祸,受9級特大地震影響携悯,放射性物質發(fā)生泄漏。R本人自食惡果不足惜筷笨,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一憔鬼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧胃夏,春花似錦轴或、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至答恶,卻和暖如春饺蚊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背悬嗓。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工污呼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人包竹。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓燕酷,卻偏偏與公主長得像籍凝,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子苗缩,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內容