简体   繁体   English

我正在制作图像轮播,但出现错误:赋值表达式的左侧可能不是可选的属性访问

[英]i'm making a carousel of images and i've the error : The left-hand side of an assignment expression may not be an optional property access

I am making an Image Carousel in Angular that receives a model to iterate the images, but when I try to take the first position it throws me an error.我正在 Angular 中制作一个图像轮播,它接收一个模型来迭代图像,但是当我尝试占据第一个位置时,它会抛出一个错误。

The left-hand side of an assignment expression may not be an optional property access.ts(2779)赋值表达式的左侧可能不是可选属性 access.ts(2779)

The error is here错误在这里

export class CarouselComponent implements OnInit {
@Input() height = 500;
@Input() isFullScreen = false;
@Input() items: ICarouselItem[] = [];

public finalHeight: string | number = 0;
public currentPosition = 0;

constructor() {
 this.finalHeight = this.isFullScreen ? '100vh' : `${this.height}px`;
}

ngOnInit(): void {
this.items.map((i, index) =>{
  i.id = index;
  i.marginLeft = 0;
});
}

setCurrentPosition(position: number){
debugger
this.currentPosition = position;
this.items.find(i => i.id === 0)?.marginLeft = -100 * position;

}

setNext(){
debugger
let finalPercentage = 0;
let nextPosition = this.currentPosition + 1;
if(nextPosition <= this.items.length - 1){
  finalPercentage = -100 * nextPosition;
}else{
  nextPosition = 0;
}
this.items.find(i => i.id === 0)?.marginLeft = finalPercentage;
this.currentPosition = nextPosition;
}

setBack(){
let finalPercentage = 0;
let backPosition = this.currentPosition -1;
if(backPosition >= 0){
  finalPercentage = -100 * backPosition;
}else{
  backPosition = this.items.length - 1;
  finalPercentage = -100 * backPosition;
}
this.items.find(i => i.id === 0)?.marginLeft = finalPercentage;
this.currentPosition = backPosition;
}
}

I think this error is display because you are using find function and directly assign value to this object Like我认为显示此错误是因为您正在使用 find 函数并直接为该对象赋值

this.items.find(i => i.id === 0)?.marginLeft = -100 * position

Suppose you didn't find the object then it will return null then How can assign value in marginLeft.假设您没有找到该对象,那么它将返回 null 那么如何在 marginLeft 中赋值。 I think first you have to check if You get any value using the find function and then perform set value to marginLeft.我认为首先您必须检查您是否使用 find 函数获得任何值,然后对 marginLeft 执行设置值。

I think this can be an issue, you are using this code 3 times.我认为这可能是一个问题,您正在使用此代码 3 次。 After these changes, if you can't fix let's comment here will check further.在这些更改之后,如果您无法修复,请在此处发表评论,我们将进一步检查。

You can try :你可以试试 :

let item = this.items.find(i => i.id === 0);
if(item){
 item.marginLeft = -100 * position;
 // you can modify object here and do other things 

}

It is invalid to try to assign to the result of an optional chaining expression:尝试分配给可选链接表达式的结果是无效的:

const object = {};
object?.property = 1; // Uncaught SyntaxError: Invalid left-hand side in assignment

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#optional_chaining_not_valid_on_the_left-hand_side_of_an_assignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#optional_chaining_not_valid_on_the_left-hand_side_of_an_assignment

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 错误-“赋值表达式的左侧可能不是可选属性 access.ts”? - Angular error- "The left-hand side of an assignment expression may not be an optional property access.ts"? 尝试初始化嵌套数组时出错“赋值表达式的左侧可能不是可选的属性访问。” - Getting error when trying to initialize nested array "The left-hand side of an assignment expression may not be an optional property access." 赋值表达式的左侧必须是变量或属性访问 - The left-hand side of an assignment expression must be a variable or a property access 角度错误:赋值表达式的左侧必须是变量或属性访问 - Angular error: The left-hand side of an assignment expression must be a variable or a property access 赋值表达式的左侧必须是angular2中的变量或属性访问 - The left-hand side of an assignment expression must be a variable or a property access in angular2 赋值表达式的左侧在Angular2中不能为常量或只读属性 - Left-hand side of assignment expression cannot be a constant or a read-only property in Angular2 赋值表达式的左侧不能是常量或只读属性 - Left-hand side of assignment expression cannot be a constant or a read-only property 错误:ReferenceError:分配中的左侧无效 - Error: ReferenceError: Invalid left-hand side in assignment 使用减号运算符时出现错误数据透视表同步融合 ej2-angular - 错误语法错误:后缀操作中的左侧表达式无效 - Error Pivot Table syncfusion ej2-angular when I use minus operator - ERROR SyntaxError: Invalid left-hand side expression in postfix operation Jade Angular2作业左侧无效 - Jade Angular2 Invalid left-hand side in assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM