组件之间传递数据失败
我是在slider组件中获取的页面数据。然后我想传递数据到另外一个子组件detail中。
传递路径slider=>index(父组件)=>detail。但是将数据从index传递给detail时失败了。
slider.js
<template> <div class="slider-wrapper"> <MeLoading v-if="!sliders.length"></MeLoading> <me-slider :direction="direction" :loop="loop" :interval="interval" :pagination="pagination" :data="sliders" v-else> <swiper-slide v-for="(item,index) in sliders" v-bind:key="index"> <a href="JavaScript:;" class="slider-link"> <img :src="item" class="slider-img"> </a> </swiper-slide> </me-slider> </div> </template> <script> import MeSlider from 'base/slider'; import MeLoading from 'base/loading'; import { swiper,swiperSlide } from "vue-awesome-swiper"; import { getProductDetail } from "api/product.js"; export default { name:'productSlider', components: { MeSlider, MeLoading, swiper, swiperSlide }, data() { return { direction: 'horizontal', loop: true, interval: 0, pagination: true, sliders: [] } }, props: { productId: { type: [String,Number], default:'' } }, created() { console.log('幻灯片组件接收商品ID成功'+this.productId); this.getDetail(this.productId); }, methods: { getDetail(id) { getProductDetail(id).then(data => { console.log(data); this.sliders = data.slider; this.$emit('loaded', data);//将数据传递给父组件 }) } } } </script> <style lang="scss" scoped> .slider-wrapper { // width: 100%; height: 360px; } .slider-link { display: block; } .slider-link, .slider-img { width: 100%; height: 100%; } </style>
index.js
<template> <div class="product"> <header class="g-header-container"> <product-header/> </header> <me-scroll> <product-slider :productId="productId" @loaded="getDetail"></product-slider> <product-detail :productData="productData"></product-detail> </me-scroll> </div> </template> <script> import productHeader from './header'; import productSlider from './slider'; import productDetail from './detail'; import MeScroll from 'base/scroll'; export default { name: 'Product', components: { productHeader, productSlider, productDetail, MeScroll }, data() { return { productId: this.$route.params.id, productData: null } }, methods: { getDetail(data) {//接收子组件传递过来的数据 console.log('父组件接收数据成功'); console.log(data); this.productData = data; } }, created() { console.log('接收商品id成功:'+this.$route.params.id); } } </script> <style lang="scss" scoped> @import '~assets/scss/mixins'; .product { overflow: hidden; position: absolute; top:0; left: 0; z-index: $product-z-index; width: 100%; height: 100%; background-color: $bgc-theme; } </style>
detail.js
<template> <div class="detail"> <div class="detail-price-count"> <p class="detail-price"><span>¥</span></p> <p class="detail-count"></p> </div> <div class="detail-info"> <h2 class="detail-title"></h2> <div class="detail-infoBox"> <span class="detail-express"></span> <span class="detail-monthCount"></span> <span class="detail-site"></span> </div> </div> <div class="detail-comment"> <h4 class="detail-commentTitle"></h4> <div class="detail-keyword"> <a class="detail"></a> </div> <div class="detail-userInfo"> <img class="detail-userIcon" src=""><span class="detail-userName"></span> </div> <p class="detail-userComment"></p> <p class="detail-userTimeType"> <span class="detail-userTime"></span> <span class="detail-userType"></span> </p> </div> <div class="detail-shopInfo"> <img src="" alt="" class="detail-shopIcon"> <h4 class="detail-shopTitle"></h4> <i class="detail-shopMark"></i> </div> <p class="detail-service"> <span class="detail-describe"></span> <span class="detail-sellerService"></span> <span class="detail-logistics"></span> </p> </div> </template> <script> export default { name:'productDetail', props: { productData: { type:[Array, Object] } }, mounted() { console.log('detail组件获取到数据!'); console.log(this.productData); } } </script> <style lang="scss" scoped> </style>
使用$emit将数据从slider传递给index.js成功。但是从index传递给detail又失败了。是因为生命周期函数错误还是什么?获取数据在index获取好还是在slider中获取好啊?
37
收起
正在回答 回答被采纳积分+1
5回答
Edward666
2020-04-02 17:12:50
和最开始的代码相比主要是detail组件的改变,请老师看一下
detail
<template> <div class="detail"> <div class="detail-price-count"> <p class="detail-price"><span>¥</span>{{productData.priceText}}</p> <p class="detail-count">{{productData.sellCount}}件已售</p> </div> <div class="detail-info"> <h2 class="detail-title">{{productData.title}}</h2> <div class="detail-infoBox"> <span class="detail-express">{{productData.postage}}</span> <span class="detail-monthCount">月销量{{productData.review.totalCount}}件</span> <span class="detail-site">{{productData.from}}</span> </div> </div> <div class="detail-comment"> <h4 class="detail-commentTitle">商品评价</h4> <div class="detail-keyword"> <a href="JavaScript:;" v-for="(item,index) in productData.review.keywords" :key="index">{{item.word}}</a> </div> <div class="detail-userInfo"> <img class="detail-userIcon" :src="productData.review.rateList[0].headPic"> <span class="detail-userName">{{productData.review.rateList[0].userName}}</span> </div> <p class="detail-userComment">{{productData.review.rateList[0].content}}</p> <p class="detail-userTimeType"> <span class="detail-userTime">{{productData.review.rateList[0].dateTime}}</span> <span class="detail-userType">{{productData.review.rateList[0].skuInfo}}</span> </p> </div> <div class="detail-shopInfo"> <img :src="productData.seller.shopIcon" class="detail-shopIcon"> <div class="detail-shopNameMark"> <h4 class="detail-shopName">{{productData.seller.shopName}}</h4> <img class="detail-shopMark" :src="productData.seller.creditLevelIcon"> </div> </div> <p class="detail-service"> <span class="detail-describe">{{productData.seller.evaluates[0].title}}{{productData.seller.evaluates[0].score}}</span> <span class="detail-sellerService">{{productData.seller.evaluates[1].title}}{{productData.seller.evaluates[1].score}}</span> <span class="detail-logistics">{{productData.seller.evaluates[2].title}}{{productData.seller.evaluates[2].score}}</span> </p> </div> </template> <script> export default { name:'productDetail', data() { return { } }, props: { productData: { type: [Array, Object], default:null } }, mounted() { setTimeout(() => { console.log('detail组件获取到数据!'); console.log(this.productData); },1000) } } </script> <style lang="scss" scoped> .detail { &-price-count { background-color: #ef348a; height: 45px; display: flex; justify-content: space-between; align-items: center; padding: 0px 10px; color: #fff; } &-price { font-size: 14px; span { font-size: 10px; } } &-count { padding: 10px; background-color: #cc2e46; border-radius: 5px; } &-info { padding: 10px; background-color: #fff; margin-bottom: 10px; } &-title { margin-bottom: 10px; color: #051b28; } &-infoBox { display: flex; justify-content: space-between; color: #999999; } //评论 &-comment { background-color: #fff; padding: 10px; margin-bottom: 10px; } &-commentTitle { margin-bottom: 10px; } &-keyword { a{ display: inline-block; padding: 10px; background-color: #ffeeee; margin-right: 10px; margin-bottom: 10px; border-radius: 10px; } } &-userIcon { height: 24px; width: 24px; border-radius: 50%; } &-userName { vertical-align: -5px; } &-userComment { margin: 8px 0; color: #051b28; line-height: 1.2; } &-userTimeType { color: #999999; } //商家信息 &-shopInfo { padding: 10px; background-color: #fff; display: flex; } &-shopIcon { height: 50px; width: 50px; margin-right: 8px; } &-shopNameMark { display: flex; flex-direction: column; justify-content: center; } &-shopMark { height: 14px; width: 29px; margin-top: 8px; } &-service { background-color: #fff; padding-bottom: 10px; display: flex; justify-content: space-around; } } </style>
出错信息是这样老师你看我的想法对吗?该怎么处理啊?
4.Vue与React高级框架开发
- 参与学习 人
- 提交作业 239 份
- 解答问题 10739 个
本阶段带你深入前端开发的肌理,通过ES6基础知识和前端主流高级框架的学习,助你快速构建企业级移动webAPP应用,进入职场的终极battle
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星