日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 尤物视频免费在线观看 | 欧美三级影院 | 怡红院一区二区三区 | 国产日韩欧美一区二区东京热 | 久久久久久久久久久久久av | 欧美精品久久久久久久久 | 毛片一区二区 | 中国少妇初尝黑人巨大 | 久久久久麻豆 | 国产99在线 | 一道本久久 | 91精品视频在线看 | 一区二区三区在线观看免费 | 米奇色| 韩日av在线 | 善良的公与媳hd中文字 | 99在线视频免费 | 青青草原av在线 | 精品91自产拍在线观看二区 | 荡女精品导航 | 色播在线播放 | 玖玖爱在线观看 | 国产精品一区二区免费在线观看 | 亚洲色图婷婷 | 亚洲丝袜天堂 | 久久久久亚洲av成人网人人软件 | 午夜在线观看免费视频 | 超碰免费在线观看 | 操色网 | 日本精品专区 | 欧美成人免费在线观看 | 人人av在线 | 特a级黄色片 | 无码一区二区三区视频 | 国产精品白嫩白嫩大学美女 | 久久精品国产99精品国产亚洲性色 | 亚洲精品大片www | 午夜一级黄色片 | 亚洲一区二区三区高清在线 | 在线视频中文字幕 | 欧美激情一区二区三区p站 欧美mv日韩mv国产网站app | 性国产精品| 久久久久免费观看 | www.人人草 | 牛牛在线 | 色肉色伦交av色肉色伦 | 国产精品1区2区3区 在线看黄的网站 | 日韩欧美亚洲国产 | 国产在线观看www | av电影在线观看网址 | 一个人看的www日本高清视频 | 人人澡人人爱 | 亚洲欧美天堂网 | 在线观看免费视频黄 | 9人人澡人人爽人人精品 | 午夜精品久久久久久久91蜜桃 | 亚洲国产av一区二区 | 日韩电影一二三区 | 99国产精品免费视频 | 免费成年人视频在线观看 | 无码h黄肉3d动漫在线观看 | 男人的av| 波多在线播放 | 亚洲精品一级片 | 国产美女www爽爽爽 www.国产毛片 | 99爱国产| 色窝窝无码一区二区三区成人网站 | 中文字幕亚洲在线 | 亚洲精品粉嫩小泬20p | 一本之道高清无码视频 | 国产一区二区在线精品 | 欧美黑粗大 | 综合网激情 | 91大神在线免费观看 | 天天艹 | 日韩精品一区在线观看 | 国产21区 | 亚洲无码精品一区二区三区 | 日韩理论在线观看 | 国产在线观看黄 | 成人免费黄色大片 | 91丝袜国产在线观看 | 欧美放荡性医生videos | 国产精自产拍久久久久久蜜 | 韩日午夜在线资源一区二区 | 国产午夜不卡 | 免费欧美日韩 | 欧美成人91| 放荡的美妇在线播放 | 成人少妇影院yyyy | 日本三级一区二区三区 | 99爱爱视频 | 爽爽淫人| 91欧美激情一区二区三区成人 | 99蜜桃臀久久久欧美精品网站 | 亚洲精品成人无码毛片 | 激情午夜天 | 日韩av电影手机在线观看 | 老司机性视频 |