账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    TypeScript 使用 Swiper 4.x thumbs 报错
    17
    0

    问题描述

    TypeScript 使用 Swiper 4.x thumbs 报错、但不影响使用、但无法通过编译

    TS 环境是 vue-cli 3.x 模版生成的、Swiper 也已经在 vue.config.js 中配置、配置代码如下

    module.exports = {
        baseUrl: '/',
        outputDir: 'dist',
        configureWebpack: {
            module: {
                rules: [
                    {
                        test: /\.js$/, // Check for all js files
                        exclude: /node_modules\/(?!(dom7|ssr-window|swiper)\/).*/,
                        loader: 'babel-loader'
                    }
                ],
            },
        },
        chainWebpack: config => {
            config.resolve.alias
                .set('assets', resolve('src/assets'))
                .set('style', resolve('src/style'))
        },
    };

    错误信息

    Argument of type '{ observer: true; observeParents: true; speed: number; navigation: { nextEl: string; prevEl: stri...' is not assignable to parameter of type 'SwiperOptions | undefined'.
    Object literal may only specify known properties, and 'thumbs' does not exist in type 'SwiperOptions | undefined'.

    问题出现的环境背景及自己尝试过哪些方法

    尝试添加断言 ! 无效

    相关代码

    <div class="swiper-top">
        <div class="swiper-wrapper">
            <div class="swiper-slide" v-for="(slide, index) in data" :key="index">
                <img :src="slide.src">
            </div>
        </div>
    </div>
    
    <div class="box">
        <div class="swiper-prev"><i class="icon icon-arrow_left"></i></div>
        <div class="swiper-bottom">
            <div class="swiper-wrapper">
                <div class="swiper-slide" v-for="(slide, index) in data" :key="index">
                    <img :src="slide.src">
                </div>
            </div>
        </div>
        <div class="swiper-next"><i class="icon icon-arrow_right"></i></div>
    </div>
    let thumbs = {
        swiper: {
            el: '.swiper-bottom',
            observer: true,
            observeParents: true,
            slidesPerView: 4,
            spaceBetween: 10,
        }
    };
    this.swiper = new Swiper('.swiper-top', {
        observer: true,
        observeParents: true,
        speed: 500,
    
        navigation: {
            nextEl: '.swiper-next',
            prevEl: '.swiper-prev',
        },
    
        thumbs, // 这行被标注错误、我确定它能正常工作、暂时不清楚 TS 错误信息如何清除
    
    });
    .swiper-top {
        width: 700px;
        height: 500px;
        overflow: hidden;
    }
    .box {
        width: 700px;
        height: 90px;
        margin-top: 20px;
        .flex(space-between);
        .swiper-bottom {
            width: 600px;
            height: 100%;
            overflow: hidden;
        }
    }
    
    .swiper-next,
    .swiper-prev {
        width: 30px;
        height: 100%;
        background: rgba(255, 255, 255, .5);
        .flex();
    }
    
    img {
        width: 100%;
        height: 100%;
        object-fit: cover;
    }
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 悠然南山行 普通会员 1楼

      Swiper 4.x默认使用Swiper原生提供的thumbs组件,如果需要自定义Thumbs组件,需要使用第三方库。

      以下是一个使用iview引入第三方库的例子:

      1. 在你的项目中安装iview:

      npm install @vue/install-vue

      1. 在你的main.js中引入iview库:

      ```javascript import Vue from 'vue' import App from './App.vue'

      import VueRouter from 'vue-router' import Home from './components/Home.vue'

      Vue.use(VueRouter) Vue.use(VueRouter(VueRouter.addRoute({ path: '/Home', component: Home }))

      const router = new VueRouter({ routes: [ { path: '/', component: Home }, // 更多路由... ] })

      new Vue({ router, render: h => h(App) }).$mount('#app') ```

      1. 在你的Home组件中使用第三方库:

      ```html

      ```

      注意,你需要根据你的实际情况修改iview的配置选项,如图片的路径、滑动的宽度和高度等。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部