首页父子组组件间传值

首页父子组组件间传值

1)父组件通过属性传值

Home.vue

//建立data存储页面各种数据
data () {
        return{
            city:’’, //城市
            swiperList:[], //轮播图
            iconList:[],//热点Icon
            recommendList:[],//热销推荐
            weekendList:[],//周末去哪儿
        }
},
//修改methods,添加json数据绑定
methods:{
        getHomeInfo (){
            axios.get('/api/index.json').then(this.getHomeInfoSucc)
        },
        getHomeInfoSucc (res) {  
            res = res.data
            if(res.ret && res.data){
                const data = res.data
                //添加跟json数据绑定
                this.city = data.city //城市
                this.swiperList = data.swiperList //轮播图
                this.iconList = data.iconList //热点Ico
                this.recommendList = data.recommendList //热销推荐
                this.weekendList = data.weekendList //周末去哪儿
            }
        }
    },
//通过属性给子组件传值
<home-header :city="city"></home-header>
<home-swiper :list="swiperList"></home-swiper>
<home-icons :list="iconList"></home-icons>
<home-recommend :list="recommendList"></home-recommend>
<home-weekend :list="weekendList"></home-weekend>

2)子组件通过props接收数据

Header.vue

props:{
        city:String //要区分大小写
    },
#元素内容修改
//元素内容的“城市”换成
{{this.city}}

Swiper.vue

设置完轮播图以后,因为一开始swiperList是一个空数组,所以渲染完以后显示的是数组最后一项。

给swiper标签设置v-if,里面传入swiperList数组的长度:一开始是空数组的时候它就不会被渲染,当它里面有东西了才被渲染,就可以解决这个问题了。

但是一般不在标签里写逻辑性代码,所以在computed里写一个showSweiper方法

//先去掉之前swiperList数组里的数据取而代之添加props
props:{
        list:Array
    },
//添加计算属性,解决轮播图默认显示为最后一张图的问题
computed: {
        showSweiper () {
            return this.list.length
        }
}
//添加计算属性方法:v-if=“showSweiper",解决轮播图默认显示为最后一张图的问题
<swiper :options="swiperOptions" v-if="showSweiper">
    //循环的v-for="item of swiperList" 替换成v-fo="item of list"
    <swiper-slide v-for="item of list" :key="item.id”>…</swiper-slide>
</swiper>

Icons.vue

//先去掉之前iconList数组里的数据取而代之添加props
props:{
        list:Array
    },
//修改计算属性里
computed:{
    pages(){
            const pages = []
            //修改this.iconlist.forEach为:this.list.forEach
            this.list.forEach((item,index) => {...}
}
#元素内容修改
//循环的v-for="item of iconList" 替换成v-fo="item of list"
<div class="icon" v-for="item of list" :key="item.id”>

Recommend.vue

//先去掉之前recommendList数组里的数据取而代之添加props
props:{
        list:Array 
    },
#元素内容修改
//循环的v-for="item of recommendList" 替换成v-fo="item of list"
<li class="item border-bottom" v-for="item of list" :key="item.id">

Weekend.vue

//先去掉之前weekendList数组里的数据取而代之添加props
props:{
        list:Array
    },
#元素内容修改
//循环的v-for="item of weekendList" 替换成v-fo="item of list"
<li class="item border-bottom" v-for="item of list" :key="item.id">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 中,父子组件之可以使用 props 和 emit 两种方式。 1. props:父组件通过 props 属性向子组件递数据,子组件通过 props 来接收数据。具体而言,可以在子组件中声明 props,然后在父组件中使用 v-bind 来绑定数据。例如: ```html <!-- 父组件 --> <template> <child-component :message="parentMessage"></child-component> </template> <!-- 子组件 --> <template> <div>{{ message }}</div> </template> <script> export default { props: { message: String } } </script> ``` 2. emit:子组件通过 emit 方法向父组件发送事件,并递数据。父组件可以通过 v-on 来监听子组件发送的事件,并接收数据。具体而言,可以在子组件中使用 $emit 方法来发送事件,然后在父组件中使用 v-on 来监听事件。例如: ```html <!-- 父组件 --> <template> <div> <child-component @my-event="handleChildEvent"></child-component> <p>{{ message }}</p> </div> </template> <script> export default { data() { return { message: '' } }, methods: { handleChildEvent(data) { this.message = data } } } </script> <!-- 子组件 --> <template> <button @click="handleClick">Click me</button> </template> <script> export default { methods: { handleClick() { this.$emit('my-event', 'hello from child') } } } </script> ``` 以上两种方式都可以用于父子组件之,具体使用哪种方式,需要根据具体场景来选择。如果是单向数据流,建议使用 props;如果是需要在子组件中触发事件,建议使用 emit。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值