vue组件模板加载出现同名的情况如何处理 For recursive components, make sure to provide the "name" option

昨天在用uni-app写h5项目时出现了一个问题:

报错:

Unknown custom element: <swiper-img> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

本身uni-app中自带了名字为swiper的轮播组件,而我又在项目中用npm下载了vue-awesome-swiper包。

这两个组件的组件名都是swiper。这时在html中使用swiper名字加载组件时,用到的会是uni-app的swiper组件,vue-awesome-swiper组件无法使用,因为它们俩重名了。

html:

<swiper :options="swiperOption" @someSwiperEvent="callback">
            <swiper-slide v-for="(e,index) in recommendList" :key="index" class="swiper-wrapper">
                <view @click="toDetails(e)" class="swiperBox">
                    <img class="swiperImg" src="../../assets/images/0.png" alt="">
                    <view class="swiperText">
                        <span>{{e.name}}</span>
                        <view class="swiperR"> > </view>
                    </view>
                </view>
            </swiper-slide>

        </swiper>

 

所以需要给vue-awesome-swiper组件重新取一个名字。

我这里使用的是局部组件,在局部注册组件时用键值对方法改名即可。

js:

 // require styles
    import 'swiper/dist/css/swiper.css'
    import { swiper, swiperSlide } from 'vue-awesome-swiper'

components:{
            //同名组件键值对重新取名
            swiperImg:swiper,
            swiperSlide,
        }
    }

html:

<swiper-img :options="swiperOption" @someSwiperEvent="callback">
            <swiper-slide v-for="(e,index) in recommendList" :key="index" class="swiper-wrapper">
                <view @click="toDetails(e)" class="swiperBox">
                    <img class="swiperImg" src="../../assets/images/0.png" alt="">
                    <view class="swiperText">
                        <span>{{e.name}}</span>
                        <view class="swiperR"> > </view>
                    </view>
                </view>
            </swiper-slide>
        </swiper-img>

 

其实在vue的官方文档中早有相应的解答,只不过当初看文档时没理解,当被坑过后才记住······

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值