Vue父子組件之間的傳值

1近上、父組件向子組件傳值

父組件中
<template>
  <div>
    <home-swiper :swiperList='swiperList'></home-swiper>
  </div>
</template>
<script>
import HomeSwiper from './components/Swiper'
export default{
  name: 'Home',
  components: {
    HomeSwiper
  },
  data () {
    return {
      swiperList: []
    },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  methods: {
    getHomeInfo () {
      axios.get('/api/index.json?city=' + this.currentCity)
        .then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res) {
      res = res.data
      if (res.ret && res.data) {
        const data = res.data
        this.city = data.city
        this.swiperList = data.swiperList
      }
    }
  },
  mounted () {
    this.lastCity = this.currentCity
    this.getHomeInfo()
  },
  activated () {
    if (this.lastCity !== this.currentCity) {
      this.lastCity = this.currentCity
      this.getHomeInfo()
    }
  }
  
子組件中
<template>
  <div class="wrapper">
  <swiper :options="swiperOption" v-if="showSwiper">
    <!-- slides -->
    <swiper-slide v-for='item of swiperList' :key='item.id'>
      <img
        class="swiper-img"
        :src="item.imgUrl"
      />
    </swiper-slide>
    <!-- Optional controls -->
    <div class="swiper-pagination"  slot="pagination"></div>
  </swiper>
  </div>
</template>
<script type="text/javascript">
export default{
  name: 'HomeSwiper',
  props: {
    swiperList: Array
  },
  data () {
    return {
      swiperOption: {
        pagination: '.swiper-pagination',
        loop: true,
        speed: 400,
        autoplay: 2000
      }
    }
  },
  computed: {
    showSwiper () {
      return this.swiperList.length
    }
  }
}
</script>

2翁脆、子組件向父組件傳值

子組件中
<template>
  <div class="container" @click="handleGallaryClick"> </div>
</template>

<script>
  methods: {
    handleGallaryClick () {
      this.$emit('close')
    }
  }
</script>
父組件中
<template>
  <div>
   <common-gallary :imgs="gallaryImgs" v-show='showGallay' @close="handleGallaryClose"></common-gallary>
  </div>
</template>

<script type="text/javascript">
import CommonGallary from 'common/gallary/Gallary'
export default{
  name: 'DetailBanner',
  props: {
    gallaryImgs: Array
  },
  data () {
    return {
      showGallay: false
    }
  },
  components: {
    CommonGallary
  },
  methods: {
    handleGallaryClose () {
      this.showGallay = false
    }
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末宦焦,一起剝皮案震驚了整個(gè)濱河市青伤,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌异希,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绒瘦,死亡現(xiàn)場(chǎng)離奇詭異宠互,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)椭坚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門予跌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人善茎,你說我怎么就攤上這事券册。” “怎么了?”我有些...
    開封第一講書人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵烁焙,是天一觀的道長航邢。 經(jīng)常有香客問我,道長骄蝇,這世上最難降的妖魔是什么膳殷? 我笑而不...
    開封第一講書人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮九火,結(jié)果婚禮上赚窃,老公的妹妹穿的比我還像新娘。我一直安慰自己岔激,他們只是感情好勒极,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著虑鼎,像睡著了一般辱匿。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上炫彩,一...
    開封第一講書人閱讀 52,682評(píng)論 1 312
  • 那天匾七,我揣著相機(jī)與錄音,去河邊找鬼江兢。 笑死昨忆,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的划址。 我是一名探鬼主播扔嵌,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼限府,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼夺颤!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起胁勺,我...
    開封第一講書人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤世澜,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后署穗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體寥裂,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年案疲,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了封恰。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡褐啡,死狀恐怖诺舔,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤低飒,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布许昨,位于F島的核電站,受9級(jí)特大地震影響褥赊,放射性物質(zhì)發(fā)生泄漏糕档。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一拌喉、第九天 我趴在偏房一處隱蔽的房頂上張望速那。 院中可真熱鬧,春花似錦司光、人聲如沸琅坡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽榆俺。三九已至,卻和暖如春坞淮,著一層夾襖步出監(jiān)牢的瞬間茴晋,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來泰國打工回窘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留诺擅,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓啡直,卻偏偏與公主長得像烁涌,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子酒觅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

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