Vue2.0史上最全入坑教程(完)—— 實戰(zhàn)案例

我們書接上文,已經定義了兩個組件(list、commonFooter、homeHeader.vue),接下來寫home.vue:
<template lang="html">
  <div class="container">
    <home-header></home-header>   <!--  展示引入的header組件 -->
    <div class="content">
      <ul class="cont-ul">
          <!-- list組件展示區(qū),并用v-for來將數(shù)據遍歷,:xx="xxx" 是用來給子組件傳遞數(shù)據的 -->
        <list v-for="item in items" :price="item.price" :title="item.title" :img="item.img"></list>
      </ul>
    </div>
    <common-footer></common-footer>  <!--  展示引入的footer組件 -->
  </div>
</template>
<script>
import HomeHeader from '../components/HomeHeader'   /* 本頁面中用到了HomeHeader組件,所以就需要在這里引入一下 */
import CommonFooter from '../components/commonFooter'
import List from '../components/list'
export default {
  data () {
      return {
          items: []   /* 定義一個空數(shù)組數(shù)據items */
      }
  },
  components: {
    HomeHeader,
    CommonFooter,
    List
  },
  created () {   /* 這個是vue的鉤子函數(shù),當new Vue()實例創(chuàng)建完畢后執(zhí)行的函數(shù) */
      this.$http.get('/api/goods').then((data) => {   /* 調用vue的ajax來請求數(shù)據,promise語法,并用es6的箭頭函數(shù) */
      this.items = data.body.data;
      })
  }
}
</script>

<style lang="css" scoped>
  .container {
    width: 100%;
    margin: 0 auto;
  }
  .content {
    margin-bottom: 1.8rem;
  }
  .cont-ul {
    padding-top: 0.5rem;
    background-color: #ccc;
  }
  .cont-ul::after {
    content: '';
    display: block;
    clear: both;
    width: 0;
    height: 0;
  }
</style>

3. 商品詳情頁goodsDetail.vue:

a). DetailHeader.vue

<template lang="html">
  <div class="head">
    <div class="header">
      <a href="javascript:;" class="go-back" @click="goBack">返回</a>
      <h4 class="header-cont">商品詳情頁</h4>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    goBack () {
      window.history.back();
    }
  }
}
</script>

<style lang="css" scoped>
  .head{
    height: 2rem;
  }
  .header{
    width: 100%;
    height: 2rem;
    position: fixed;
    left: 0;
    top: 0;
    background-color: #fff;
    border-bottom: 2px solid #ccc;
  }
  .header .go-back {
    width: 2rem;
    height: 2rem;
    text-align: center;
    color: #ccc;
    font-size: 0.8rem;
    float: left;
    line-height: 2rem;
    margin-left: 1rem;
    position: absolute;
    left: 0;
    top: 0;
  }
  .header .header-cont {
    width: 100%;
    text-align: center;
    line-height: 2rem;
    font-size: 1.2rem;
  }
</style>

b). goodsDetail.vue

<template lang="html">
  <div class="detail">
      <detail-header></detail-header>
      <p class="site-title">樹懶果園 泰國進口大金煌芒果</p>
      <p class="site-cont">5斤裝,約2-4個果,大?。?!甜!??!</p>
      <common-footer></common-footer>
  </div>
</template>

<script>
import DetailHeader from '../components/DetailHeader'
import CommonFooter from '../components/commonFooter'

export default {
  data () {
    return {

    }
  },
  components: {
    DetailHeader,
    CommonFooter
  }
}
</script>

<style lang="css">
  .detail {
    padding: 0.25rem;
    font-size: 12px;
  }
  .detail > img {
    display: block;
    width: 80%;
    margin:  0 auto 0.5rem;
  }
  .detail > p {
    font-size: 1.1rem;
    line-height: 1.5rem;
    text-align: left;
    padding-bottom: 0.5rem;
  }
  .detail > p.site-title {
    color: #ff8000;
  }
  .detail > p.site-cont {
    color: #666;
    font-size: 0.9rem;
  }
</style>

至此一個簡單的基于vue但頁面應用,編寫完畢。下面我就簡單說下例子過程中用到的一些語法,以便加深大家的理解

demo的github地址,覺得不錯的給個star

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 書接上文 前言:經過前兩節(jié)的學習,我們已經可以創(chuàng)建一個vue工程了。下面我們將一起來學習制作一個簡單的實戰(zhàn)案例。 ...
    諾奕閱讀 104,034評論 89 121
  • 在這個陌生的環(huán)境下,更多的不安全感,沒日沒夜的侵襲著精神的層面,當開始干活的那一刻,突然發(fā)現(xiàn)你原來什么也不是,...
    雨中落塵閱讀 301評論 0 0
  • 關于安妮: 在電影即將下架的前一天,終于還是跑去看了它,沖著陳可辛,為了安妮。也可能,只是單純?yōu)榱俗约骸?在片尾字...
    楊矗矗閱讀 1,199評論 2 13
  • 【打卡始于2017.10.14持續(xù)于2017.10.24】 【知~學習】 《六項精進》3遍,能背誦 《大學》0遍。...
    lovelyfener閱讀 287評論 0 0

友情鏈接更多精彩內容