我是靠谱客的博主 激动唇膏,这篇文章主要介绍vue 一个简单的项目 之二 城市选择页面 step6 兄弟组件数据传递,现在分享给大家,希望可以做个参考。

前面我们动态地获取了城市选择页面的数据。下面,我们希望,点击字母表,左边列表能自动滑到相应字母部分。这需要兄弟组件间传递数据啦。

首先,创建一个分支 city-components ,pull 下来,在这个分支实现兄弟组件通信。

好啦,首先,我们打开,Alphabet.vue 代码,给每一 li 一个click 事件。

然后我们把数据 从 Alphabet 传递给父组件 City,再由City 转发给List 组件。

Alphabet.vue 代码如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<template> <ul class="list"> <li class="item" v-for="(item,key) of cities" :key="key" @click="handleLetterClick" > {{key}} </li> </ul> </template> <script> export default { name: 'CityAlphabet', props: { cities: Object }, methods: { handleLetterClick (e) { this.emit('change',e.target.innerText) } } } </script> <style lang="stylus" scoped> @import '~styles/variables.styl' .list display: flex flex-direction: column justify-content: center position: absolute top: 1.5rem right: 0 bottom: 0 width: .4rem .item line-height: .4rem text-align: center color: $bgColor </style>

City.vue 代码如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<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.vue' import CitySearch from './components/Search.vue' import CityList from './components/List.vue' import CityAlphabet from './components/Alphabet.vue' 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.data if (res.ret && res.data) { const data = res.data this.cities = data.cities this.hotCities = data.hotCities } }, handleLetterChange (letter) { this.letter = letter } }, mounted () { this.getCityInfo() } } </script> <style lang="stylus" scoped> </style>

然后在List.vue 中接收父组件传递的数据。[better-scroll 还是挺好用的]

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<template> <div class="list" ref="wrapper"> <div> <div class="area"> <div class="title border-topbottom">当前城市</div> <div class="button-list"> <div class="button-wraper"> <div class="button">北京</div> </div> </div> </div> <div class="area"> <div class="title border-topbottom">热门城市</div> <div class="button-list"> <div class="button-wraper" 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" :ref="key" > <div class="title border-topbottom">{{key}}</div> <div class="item-list"> <div class="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: { cities: Object, hot: Array, letter: String }, mounted () { this.scroll = new Bscroll(this.$refs.wrapper) }, watch: { letter () { if (this.letter) { const element = this.$refs[this.letter][0] this.scroll.scrollToElement(element) } } } } </script>

接下来,我们要做一个右侧字母表拖拽时,左边也会滑动的效果。

改一下Alphabet.vue 给它增加滚动事件的监听。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<template> <ul class="list"> <li class="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 }, data () { return { touchStatus: false } }, computed: { letters () { const letters = [] for (let i in this.cities) { letters.push(i) } return letters } }, methods: { handleLetterClick (e) { this.$emit('change', e.target.innerText) }, handleTouchStart () { this.touchStatus = true }, handleTouchMove (e) { if (this.touchStatus) { const startY = this.$refs['A'][0].offsetTop const touchY = e.touches[0].clientY - 79 const index = Math.floor((touchY - startY) / 20) if (index >= 0 && index < this.letters.length) { this.$emit('change', this.letters[index]) } } }, handleTouchEnd () { this.touchStatus = false } } } </script> <style lang="stylus" scoped> @import '~styles/variables.styl' .list display: flex flex-direction: column justify-content: center position: absolute top: 1.5rem right: 0 bottom: 0 width: .4rem .item line-height: .4rem text-align: center color: $bgColor </style>

Done!

最后

以上就是激动唇膏最近收集整理的关于vue 一个简单的项目 之二 城市选择页面 step6 兄弟组件数据传递的全部内容,更多相关vue内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部