swiper渲染总是报错

swiper渲染总是报错

home/index.vue
<template>
  <div class="home">
    <header class="g-header-container">
      <home-header/>
    </header>
    <div>
      <home-slider/>
    </div>
    <div class="g-backtop-container"></div>
<!--    <router-view></router-view>-->
  </div>
</template>

<script>
  import HomeHeader from './header';
  import HomeSlider from './slider';

  export default {
    name: 'Home',
    components: {
      HomeHeader,
      HomeSlider
    }
  };
</script>

<style lang="scss" scoped>
  @import "~assets/scss/mixins";

  .home {
    overflow: hidden;
    width: 100%;
    height: 100%;
    background-color: $bgc-theme;
  }
</style>
home/slider.vue
<template>
  <div class="slider-wrapper">
    <me-slider
      :direction="direction"
      :loop="loop"
      :interval="interval"
      :pagination="pagination"
    >
      <swiper-slide
        v-for="(item,index) in sliders"
        :key="index"
      >
        <a :href="item.linkUrl" class="slider-link">
          <img :src="item.picUrl" class="slider-img">
        </a>
      </swiper-slide>
    </me-slider>
  </div>
</template>

<script>
  import MeSlider from 'base/slider';
  import {swiperSlide} from 'vue-awesome-swiper';
  import {sliderOptions} from './config';

  export default {
    name: 'HomeSlider',
    components: {
      MeSlider,
      swiperSlide
    },
    data() {
      return {
        direction: sliderOptions.direction,
        loop: sliderOptions.loop,
        interval: sliderOptions.interval,
        pagination: sliderOptions.pagination,
        sliders: [
          {
            'linkUrl': 'https://www.imooc.com',
            'picUrl': require('./1.jpg')
          },
          {
            'linkUrl': 'https://www.imooc.com',
            'picUrl': require('./2.jpg')
          },
          {
            'linkUrl': 'https://www.imooc.com',
            'picUrl': require('./3.jpg')
          }, {
            'linkUrl': 'https://www.imooc.com',
            'picUrl': require('./4.jpg')
          }
        ]
      };
    }
  };
</script>

<style lang="scss" scoped>
  .slider-wrapper{
    width: 100%;
    height: 183px;
  }
</style>
base/slider/index.vue
<template>
  <swiper :options="swiperOption">
    <slot></slot>
    <div class="swiper-pagination" v-if="pagination" slot="pagination"></div>
  </swiper>
</template>

<script>
  import {swiper} from 'vue-awesome-swiper';

  export default {
    name: 'MeSlider',
    components: {
      swiper
    },
    props: {
      direction: {
        type: String,
        default: 'horizontal',
        validator(value) {
          return [
            'hotizontal',
            'vertical'
          ].indexOf(value) > -1;
        }
      },
      interval: {
        type: Number,
        default: 3000,
        validator(value) {
          return value >= 0;
        }
      },
      loop: {
        type: Boolean,
        default: true
      },
      pagination: {
        type: Boolean,
        default: true
      }
    },
    data() {
      return {
        swiperOption: {
          watchOverflow: true, // 只有1个slide时,就不轮播
          direction: this.direction,
          autoplay: this.interval ? {
            delay: this.interval,
            disableOnInteraction: false // 与轮播有交互时,暂停轮播
          } : false,
          slidesPerView: 1, // 设置slider容器同时显示几张slide
          loop: this.loop,
          pagination: {
            el: this.pagination ? '.swiper-pagination' : null
          }
        }
      };
    }
  };
</script>

<style lang="scss" scoped>
  .swiper-container {
    width: 100%;
    height: 100%;
  }

  .slider-link {
    display: block;
  }

  .slider-link,
  .slider-img {
    width: 100%;
    height: 100%;
  }
</style>

找了半天也不知道为啥,一个提示direction有问题,一个提示swiper注册问题,一个提示swiper-slide注册问题,但是对比视频里的和下载的源码都差不多啊,就是出问题

vue.runtime.esm.js?439f:619 [Vue warn]: Invalid prop: custom validator check failed for prop "direction".


found in


---> <MeSlider> at src/base/slider/index.vue

       <HomeSlider> at src/pages/home/slider.vue

         <Home> at src/pages/home/index.vue

           <App> at src/App.vue

             <Root>

vue.runtime.esm.js?439f:619 [Vue warn]: Unknown custom element: <swiper> - did you register the component correctly? For recursive components, make sure to provide the "name" option.


found in


---> <MeSlider> at src/base/slider/index.vue

       <HomeSlider> at src/pages/home/slider.vue

         <Home> at src/pages/home/index.vue

           <App> at src/App.vue

             <Root>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
好帮手慕言 2020-03-26 19:05:25

同学你好,按照官网引入即可。或许和版本有关系,解决了问题就好,继续往下学习吧,祝学习愉快~

好帮手慕言 2020-03-26 16:57:25

同学你好,将同学提供的代码放到源码中测试,没有出现和同学类似的报错,看报错是因为注册组件有问题,同学可以排查下,另外:下方代码同学是在写在哪个文件中的呢?

http://img1.sycdn.imooc.com//climg/5e7c6e60093a2fb903360451.jpg

可以把这个页面的全部代码粘贴上来,祝学习愉快~


  • 提问者 猫有一只猫 #1
    是src/pages/home/slider.vue webstorm总是莫名其妙报错找不到引入的module,就这个import MeSlider from 'base/slider';还有类似的报错 <template> <div class="slider-wrapper"> <me-slider :direction="direction" :loop="loop" :interval="interval" :pagination="pagination" > <swiper-slide v-for="(item,index) in sliders" :key="index" > <a :href="item.linkUrl" class="slider-link"> <img :src="item.picUrl" class="slider-img"> </a> </swiper-slide> </me-slider> </div> </template> <script> import MeSlider from 'base/slider'; import {swiperSlide} from 'vue-awesome-swiper'; import {sliderOptions} from './config'; export default { name: 'HomeSlider', components: { MeSlider, swiperSlide }, data() { return { direction: sliderOptions.direction, loop: sliderOptions.loop, interval: sliderOptions.interval, pagination: sliderOptions.pagination, sliders: [ { 'linkUrl': 'https://www.imooc.com', 'picUrl': require('./1.jpg') }, { 'linkUrl': 'https://www.imooc.com', 'picUrl': require('./2.jpg') }, { 'linkUrl': 'https://www.imooc.com', 'picUrl': require('./3.jpg') }, { 'linkUrl': 'https://www.imooc.com', 'picUrl': require('./4.jpg') } ] }; } }; </script> 底下的style写不下了,跟视频一样的
    2020-03-26 17:06:53
  • 提问者 猫有一只猫 #2
    这个是渲染后在浏览器呈现的样子,会不会是import {swiper} from 'vue-awesome-swiper';有问题?MeSlider被引入后能正常渲染,MeSlider里的swiper不能被渲染成<div></div>,在浏览器中就是<swiper></swiper> <template> <swiper :options="swiperOption"> <slot></slot> <div class="swiper-pagination" v-if="pagination" slot="pagination"></div> </swiper> </template>
    2020-03-26 17:48:04
  • 提问者 猫有一只猫 #3
    我刚查了vue-awesome-swiper官网,官网的本地注册写法是import { Swiper, SwiperSlide, directive } from 'vue-awesome-swiper',然后我在home/slider.vue中import {SwiperSlide} from 'vue-awesome-swiper';这样引入,slider/index.vue中import {Swiper} from 'vue-awesome-swiper';这样引入,就是把教学视频中头个小写s换成大写的就能渲染成功了,这是不是引入必须严格按照导出的名字来引入啊??那为啥视频中的能小写?
    2020-03-26 18:16:44
好帮手慕言 2020-03-26 12:03:47

同学你好,测试同学提供的代码,第一个报错是base/slider/index.vue文件中的单词拼写错误,可参考下方修改:

http://img1.sycdn.imooc.com//climg/5e7c291f09191f4504800390.jpg

并没有出现第二个错误,同学可以修改上方的代码之后,清除浏览器的缓存再测试下代码。

如果我的回答帮到了你,欢迎采纳,祝学习愉快~

  • 提问者 猫有一只猫 #1
    找到拼写错误了,但是还是提示swiper和swiper-slide错误, 就渲染成这样, <div class=slider-wrapper> <swiper> <swiper-slide></swiper-slide> <swiper-slide></swiper-slide> <swiper-slide></swiper-slide> <swiper-slide></swiper-slide> </swiper> </div>
    2020-03-26 12:38:52
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师