日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

七十、Vue城市页面Ajax动态渲染和兄弟组件数据传递

發(fā)布時間:2024/10/8 vue 80 豆豆
生活随笔 收集整理的這篇文章主要介紹了 七十、Vue城市页面Ajax动态渲染和兄弟组件数据传递 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2020/10/29、 周四、今天又是奮斗的一天。

@Author:Runsen

寫在前面:我是「Runsen」,熱愛技術(shù)、熱愛開源、熱愛編程。技術(shù)是開源的、知識是共享的。大四棄算法轉(zhuǎn)前端,需要每天的日積月累, 每天都要加油!!!

今天將完成Vue城市頁面動態(tài)渲染和兄弟組件數(shù)據(jù)傳遞。

在之前的項目代碼中,城市頁面的數(shù)據(jù)是寫死的。

文章目錄

  • axios發(fā)json請求
  • City.vue
  • List.vue
  • Alphabet.vue
  • 兄弟組件數(shù)據(jù)傳遞
  • City.vue
  • Alphabet.vue
  • List.vue

axios發(fā)json請求

首先在static準備json文件

在網(wǎng)頁端可以通過路由訪問

City.vue

Ajax動態(tài)渲染需要設(shè)置mounted掛載組件前的鉤子函數(shù),訪問json,在data數(shù)據(jù)中儲存起來。

下面就是在子組件中綁定父組件的數(shù)據(jù)。

<template><div><city-header></city-header><city-search></city-search><city-list :cities="cities" :hot="hotCities"></city-list><city-alphabet :cities="cities"></city-alphabet></div> </template><script> import axios from 'axios' import CityHeader from './components/Header' import CitySearch from './components/Search' import CityList from './components/List' import CityAlphabet from './components/Alphabet'export default {name: 'City',components: {CityHeader,CitySearch,CityList,CityAlphabet},data () {return {cities: {},hotCities: []}},methods: {getCityInfo () {axios.get('/api/city.json').then(this.handleGetCityInfoSucc)},handleGetCityInfoSucc (res) {res = res.dataif (res.ret && res.data) {const data = res.datathis.cities = data.citiesthis.hotCities = data.hotCities}}},mounted () {this.getCityInfo()} } </script><style lang="stylus" scoped></style>

List.vue

<template><div class="list" ref="wrapper"><div><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"><divclass="button-wrapper"v-for="item of hot":key="item.id"><div class="button">{{item.name}}</div></div></div></div><div class="area" v-for="(item, key) of cities" :key="key"><div class="title border-topbottom">{{key}}</div><div class="item-list"><divclass="item border-bottom"v-for="innerItem of item":key="innerItem.id">{{innerItem.name}}</div></div></div></div></div> </template> <script> import Bscroll from 'better-scroll' export default {name: 'CityList',props: {hot: Array,cities: Object},mounted () {this.scroll = new Bscroll(this.$refs.wrapper)} } </script><style lang="stylus" scoped>@import '~styles/varibles.styl'.border-topbottom&:beforeborder-color: #ccc&:afterborder-color: #ccc.border-bottom&:beforeborder-color: #ccc.listoverflow: hiddenposition: absolutetop: 1.58remleft: 0right: 0bottom: 0.titleline-height: .54rembackground: #eeepadding-left: .2remcolor: #666font-size: .26rem.button-listoverflow: hiddenpadding: .1rem .6rem .1rem .1rem.button-wrapperfloat: leftwidth: 33.33%.buttonmargin: .1rempadding: .1rem 0text-align: centerborder: .02rem solid #cccborder-radius: .06rem.item-list.itemline-height: .76rempadding-left: .2rem </style>

Alphabet.vue

<template><ul class="list"><li class="item" v-for="(item, key) of cities" :key="key">{{key}}</li></ul> </template><script> export default {name: 'CityAlphabet',props: {cities: Object} } </script><style lang="stylus" scoped>@import '~styles/varibles.styl'.listdisplay: flexflex-direction: columnjustify-content: centerposition: absolutetop: 1.58remright: 0bottom: 0width: .4rem.itemline-height: .4remtext-align: centercolor: $bgColor </style>

兄弟組件數(shù)據(jù)傳遞

City.vue

<template><div><city-header></city-header><city-search></city-search><city-list:cities="cities":hot="hotCities":letter="letter"></city-list><city-alphabet:cities="cities"@change="handleLetterChange"></city-alphabet></div> </template><script> import axios from 'axios' import CityHeader from './components/Header' import CitySearch from './components/Search' import CityList from './components/List' import CityAlphabet from './components/Alphabet' export default {name: 'City',components: {CityHeader,CitySearch,CityList,CityAlphabet},data () {return {cities: {},hotCities: [],letter: ''}},methods: {getCityInfo () {axios.get('/api/city.json').then(this.handleGetCityInfoSucc)},handleGetCityInfoSucc (res) {res = res.dataif (res.ret && res.data) {const data = res.datathis.cities = data.citiesthis.hotCities = data.hotCities}},handleLetterChange (letter) {this.letter = letter}},mounted () {this.getCityInfo()} } </script><style lang="stylus" scoped></style>

Alphabet.vue

<template><ul class="list"><liclass="item"v-for="item of letters":key="item":ref="item"@touchstart="handleTouchStart"@touchmove="handleTouchMove"@touchend="handleTouchEnd"@click="handleLetterClick">{{item}}</li></ul> </template><script> export default {name: 'CityAlphabet',props: {cities: Object},computed: {letters () {const letters = []for (let i in this.cities) {letters.push(i)}return letters}},data () {return {touchStatus: false,startY: 0,timer: null}},updated () {this.startY = this.$refs['A'][0].offsetTop},methods: {handleLetterClick (e) {this.$emit('change', e.target.innerText)},handleTouchStart () {this.touchStatus = true},handleTouchMove (e) {if (this.touchStatus) {if (this.timer) {clearTimeout(this.timer)}this.timer = setTimeout(() => {const touchY = e.touches[0].clientY - 79const index = Math.floor((touchY - this.startY) / 20)if (index >= 0 && index < this.letters.length) {this.$emit('change', this.letters[index])}}, 16)}},handleTouchEnd () {this.touchStatus = false}} } </script><style lang="stylus" scoped>@import '~styles/varibles.styl'.listdisplay: flexflex-direction: columnjustify-content: centerposition: absolutetop: 1.58remright: 0bottom: 0width: .4rem.itemline-height: .4remtext-align: centercolor: $bgColor </style>

List.vue

<template><div class="list" ref="wrapper"><div><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"><divclass="button-wrapper"v-for="item of hot":key="item.id"><div class="button">{{item.name}}</div></div></div></div><divclass="area"v-for="(item, key) of cities":key="key":ref="key"><div class="title border-topbottom">{{key}}</div><div class="item-list"><divclass="item border-bottom"v-for="innerItem of item":key="innerItem.id">{{innerItem.name}}</div></div></div></div></div> </template><script> import Bscroll from 'better-scroll' export default {name: 'CityList',props: {hot: Array,cities: Object,letter: String},mounted () {this.scroll = new Bscroll(this.$refs.wrapper)},watch: {letter () {if (this.letter) {const element = this.$refs[this.letter][0]console.log(element)this.scroll.scrollToElement(element)}}} } </script><style lang="stylus" scoped>@import '~styles/varibles.styl'.border-topbottom&:beforeborder-color: #ccc&:afterborder-color: #ccc.border-bottom&:beforeborder-color: #ccc.listoverflow: hiddenposition: absolutetop: 1.58remleft: 0right: 0bottom: 0.titleline-height: .54rembackground: #eeepadding-left: .2remcolor: #666font-size: .26rem.button-listoverflow: hiddenpadding: .1rem .6rem .1rem .1rem.button-wrapperfloat: leftwidth: 33.33%.buttonmargin: .1rempadding: .1rem 0text-align: centerborder: .02rem solid #cccborder-radius: .06rem.item-list.itemline-height: .76rempadding-left: .2rem </style>

參考課程:慕課網(wǎng)Vue2.5->2.6->3.0 開發(fā)去哪兒網(wǎng)App 從零基礎(chǔ)入門到實戰(zhàn)項目開發(fā)

總結(jié)

以上是生活随笔為你收集整理的七十、Vue城市页面Ajax动态渲染和兄弟组件数据传递的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产精品7777 | 性欧美lx╳lx╳ | 琪琪原网址 | 黑人vs日本人ⅹxxxhd | 就爱av| 97成人人妻一区二区三区 | 涩涩视频在线免费看 | 日韩经典午夜福利发布 | 天天色影院 | 91免费视频网站 | 天天射夜夜操 | 一级大片儿 | av色图在线 | 久久国产精品久久 | 精品国产污污免费网站入口 | 久久女同互慰一区二区三区 | 男人午夜视频 | 手机在线毛片 | 91视频最新| 欧美大片一区二区 | 日本亚洲黄色 | 成人精品久久 | 青娱乐最新视频 | 中文字幕亚洲精品在线观看 | 亚洲最大在线视频 | 在线观看中文字幕一区 | 黄色a一级 | 国产在线中文字幕 | 欧美精品一区在线发布 | 麻豆视频免费观看 | 一区二区视频在线看 | 风韵多水的老熟妇 | 在线日韩精品视频 | 成人免费高清在线播放 | 天天做天天爱天天操 | 中文字幕乱码人妻无码久久 | 亚洲精品中文字幕乱码无线 | 浴室里强摁做开腿呻吟男男 | 中文不卡在线 | 毛片传媒 | 亚洲成成品网站 | 人人草人人射 | 99精品乱码国产在线观看 | 精品久久久久久中文字幕 | www在线观看免费视频 | 无遮挡在线观看 | 国产热 | 亚洲精品黄色片 | 野花av | 探花视频在线免费观看 | 不卡的日韩av | 欧美偷拍精品 | 久操av| 亚洲欧美一区二区三区在线 | 69视频在线免费观看 | 日韩特一级 | 亚洲国产精品影院 | 污视频在线观看网址 | 在线观看高清视频 | 欧美日韩国产综合在线 | 外国黄色网 | 日韩激情网站 | 色av网| 天天cao | 97人妻精品一区二区三区软件 | 国产欧美视频一区二区三区 | 亚洲av无码久久精品狠狠爱浪潮 | 无码精品人妻一区二区三区湄公河 | 国产一区二区三区四区视频 | 欧美一级二级三级视频 | 欧美经典一区二区三区 | 免费一级特黄特色大片 | 成人免费版 | 日韩黄色小视频 | 裸体一区二区 | 欧美成人免费大片 | 久久久新| 无限国产资源 | 国产又大又黑又粗 | 亚洲综合激情小说 | 一二三区免费视频 | 久热超碰 | 亚洲精品无码久久 | 国产在线观看一区二区三区 | 91精品免费视频 | 91精选视频 | 国产无套视频 | 天堂俺去俺来也www 欧美大片在线播放 | av片一区二区 | 丝袜人妖 | 午夜亚洲aⅴ无码高潮片苍井空 | 国产亚洲色婷婷久久99精品91 | 成人免费视频国产免费麻豆 | 亚洲欧美日本在线 | 国产高清自拍一区 | 美妇湿透娇羞紧窄迎合 | 一本色道久久88加勒比—综合 | 亚洲综合精品 | 日韩精品成人在线观看 |