Angular项目出现TS1109错误:期待表达式,求助解决
解决TS1109: Expression expected错误
错误原因
你遇到的TS1109错误,根源在于第11行的todo: Task[] = [...];写法不符合TypeScript语法。扩展运算符...必须紧跟一个可迭代对象(比如数组、字符串等),单独的...没有任何表达式作为操作数,编译器会报错“期望表达式”。
解决方案
根据你的代码场景,有两种修正方式:
1. 初始化为空数组
如果此时todo数组不需要预设数据,直接和inProgress、done一样用空数组初始化即可:
todo: Task[] = [];
2. 复制已有数组(如果有需要)
如果是想基于某个已存在的Task数组来初始化todo,需要把目标数组放在...后面,比如:
// 假设有一个预设的初始任务数组 const initialTasks: Task[] = [{ id: 1, title: '示例任务' }]; todo: Task[] = [...initialTasks];
修改后的完整代码
import { Component } from '@angular/core'; import { Task } from './task/task'; import { CdkDragDrop, transferArrayItem } from '@angular/cdk/drag-drop'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { todo: Task[] = []; // 修正此处 inProgress: Task[] = []; done: Task[] = []; editTask(list: string, task: Task): void {} drop(event: CdkDragDrop<Task[]>): void { if (event.previousContainer === event.container) { return; } if (!event.container.data || !event.previousContainer.data) { return; } transferArrayItem( event.previousContainer.data, event.container.data, event.previousIndex, event.currentIndex ); } }
内容的提问来源于stack exchange,提问作者Emanuel Epshtein
相关产品推荐
相关产品推荐

