首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >属性“slidesPerView”的类型不兼容

属性“slidesPerView”的类型不兼容
EN

Stack Overflow用户
提问于 2018-08-04 20:31:21
回答 1查看 1.4K关注 0票数 3

考虑一个带有NGX包装插件的角形6应用程序.

在SwiperComponent模板文件中调用Swiper插件:

代码语言:javascript
复制
<swiper [config]="customConfig">
...
</swiper>

为了保持组件文件的可读性,可以从服务中获取swiper配置:

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { DataService } from '../../services/data.service';
import { SwiperConfigInterface } from 'ngx-swiper-wrapper';
...
export class SwiperComponent implements OnInit {
  customConfig: SwiperConfigInterface;

  constructor(private dataService: DataService) { }

  ngOnInit() {
    this.customConfig = this.dataService.getCustomConfig();
  }

}

以下是服务:

代码语言:javascript
复制
import { Injectable } from '@angular/core';
...
export class DataService {

  constructor() { }

  getCustomConfig() {
    return {
      observer: true,
      direction: 'vertical',
      slidesPerView: 'auto',
      freeMode: true,
      ...
    };
  }

  // Lots of other configs here
  ...

}

这里出现了一个错误:

错误TS2322:键入{.slidesPerView: string;…‘不能指定键入“SwiperConfigInterface”。属性'slidesPerView‘的类型是不兼容的。键入'string‘不能指定键入'number \“auto’‘。

可以以一种残酷的方式省略此错误,只需将customConfig变量的类型从SwiperConfigInterface更改为any。但有谁知道解决这个问题的更好方法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-04 21:20:56

slidesPerView属性SwiperConfigInterface需要一个number类型的值或一个'auto'类型的值

代码语言:javascript
复制
slidesPerView?: number | 'auto',

根据错误消息,类型记录认为'auto'是此对象中的string类型:

代码语言:javascript
复制
return {
  observer: true,
  direction: 'vertical',
  slidesPerView: 'auto',
  freeMode: true,
  ...
};

您可以强制编译器将其视为'auto'类型的值,方法是:

代码语言:javascript
复制
slidesPerView: 'auto' as 'auto',
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51689343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档