Angular 财产类型';slidesPerView';不相容
考虑使用NGX Swiper包装插件的Angular 6应用程序。Angular 财产类型';slidesPerView';不相容,angular,typescript,angular6,swiper,Angular,Typescript,Angular6,Swiper,考虑使用NGX Swiper包装插件的Angular 6应用程序。 Swiper插件在SwipComponent模板文件中调用,使用: <swiper [config]="customConfig"> ... </swiper> 服务内容如下: import { Injectable } from '@angular/core'; ... export class DataService { constructor() { } getCustomConfig
Swiper插件在SwipComponent模板文件中调用,使用:
<swiper [config]="customConfig">
...
</swiper>
服务内容如下:
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;…”不正确
可分配给类型“SwipConfigInterface”。属性类型
“SlideService”不兼容。类型“string”不可分配给
键入“编号”|“自动”
可以用一种粗暴的方式忽略此错误,只需将
customConfig
变量的类型从swipoconfiginterface
更改为any
。但是有人知道解决此问题的更好方法吗?swipoconfiginterface
的slideservice
属性需要一个number
类型的值>或类型为'auto'
的值:
slidesPerView?: number | 'auto',
根据错误消息,Typescript将'auto'
视为此对象中的字符串类型:
return {
observer: true,
direction: 'vertical',
slidesPerView: 'auto',
freeMode: true,
...
};
通过强制转换,可以强制编译器将其视为类型为'auto'
的值:
slidesPerView: 'auto' as 'auto',
只需添加返回类型注释:getCustomConfig():SwiperConfigInterface{…
。另请参见@artem尝试,但错误仍然相同。您可以尝试使用SlideService视图:“自动”作为“自动”,
。
slidesPerView: 'auto' as 'auto',