修改下面代码// pages/TaskListPage.ets
import { TaskItem } from "../View/TaskItem";
import { TaskModel } from "../Model/TaskModel";
import { router } from '@kit.ArkUI';
import CreateTaskModel from "../Model/TaskModel";
@Entry
@Component
struct TaskListPage {
@StorageLink('novelList') taskList: Array<TaskModel> = [];
private listScroller: Scroller = new Scroller();
aboutToAppear() {
this.taskList = CreateTaskModel.getNovelList();
}
build() {
Stack({alignContent: Alignment.BottomEnd}) {
Column() {
Text("小说列表").fontSize(28).margin(16)
List({scroller: this.listScroller}) {
ForEach(this.taskList, (item: TaskModel) => {
ListItem() {
TaskItem({task: item})
}
})
}.layoutWeight(1)
}.width("100%")
Button("添加小说")
.onClick(() => router.pushUrl({url: "pages/AddTaskPage"}))
.margin(20)
}
.width("100%")
.height("100%")
.backgroundColor(0xf1f3f5)
}
}和// ets/view/TaskItem.ets
import { TaskModel } from "../Model/TaskModel";
@Component
export struct TaskItem {
@Prop task: TaskModel;
build() {
Row() {
Column() {
Text(this.task.novelTitle).fontSize(18)
Text(`作者: ${this.task.novelAuthor}`).fontSize(14)
}.width("80%")
}
.width("100%")
.padding(12)
.backgroundColor(Color.White)
.borderRadius(8)
.margin({bottom: 8})
}
}和// ets/viewmodel/TaskModel.ets
import { AppStorageV2 } from '@kit.ArkUI'; // 使用正确的API名称
export class TaskModel {
novelId: string = "";
novelTitle: string = "";
novelAuthor: string = "";
novelContent: string = "";
createDate: string = "";
taskStatus: string = "";
constructor(novelId: string, novelTitle: string, novelAuthor: string,
novelContent: string, createDate: string, taskStatus: string) {
this.novelId = novelId;
this.novelTitle = novelTitle;
this.novelAuthor = novelAuthor;
this.novelContent = novelContent;
this.createDate = createDate;
this.taskStatus = taskStatus;
}
}
export class CreateTaskModel {
private static readonly STORAGE_KEY = 'novelList';
static initStorage() {
if (!AppStorageV2.Has(CreateTaskModel.STORAGE_KEY)) {
AppStorageV2.SetOrCreate(
CreateTaskModel.STORAGE_KEY,
CreateTaskModel.getTaskListData()
);
}
}
static getNovelList(): Array<TaskModel> {
return AppStorageV2.Get(CreateTaskModel.STORAGE_KEY) || [];
}
static addNovel(novel: TaskModel): void {
const currentList = CreateTaskModel.getNovelList();
AppStorageV2.SetOrCreate(
CreateTaskModel.STORAGE_KEY,
[...currentList, novel]
);
}
private static getTaskListData(): Array<TaskModel> {
const authors = ["金庸", "古龙", "琼瑶"];
const titles = ["天龙八部", "射雕英雄传", "神雕侠侣"];
return Array.from({length: 3}, (_, i) => new TaskModel(
i.toString(),
titles[i],
authors[i],
"小说内容示例",
new Date().toLocaleDateString(),
"undone"
));
}
}
// 初始化存储(使用类名调用静态方法)
CreateTaskModel.initStorage();和// pages/AddTaskPage.ets
import { TaskModel } from "../Model/TaskModel";
import { router } from '@kit.ArkUI';
import CreateTaskModel from "../Model/TaskModel";
@Entry
@Component
struct AddTaskPage {
@State novelTitle: string = "";
@State novelAuthor: string = "";
@State novelContent: string = "";
build() {
Column() {
Text("新建小说").fontSize(26).margin({top: 16, bottom: 16})
Column({space: 20}) {
// 小说标题
TextInput({placeholder: "小说标题", text: $$this.novelTitle})
.width("100%").height(40)
// 小说作者
TextInput({placeholder: "小说作者", text: $$this.novelAuthor})
.width("100%").height(40)
// 小说内容
TextInput({placeholder: "小说内容", text: $$this.novelContent})
.width("100%").height(150)
Button("创建小说")
.onClick(() => {
if (this.novelTitle && this.novelAuthor) {
const novel = new TaskModel(
Date.now().toString(),
this.novelTitle,
this.novelAuthor,
this.novelContent,
new Date().toLocaleDateString(),
"undone"
);
CreateTaskModel.addNovel(novel);
router.back();
}
})
}.width("100%").padding(10)
}.width("100%").height("100%").backgroundColor(0xf1f3f5)
}
}实现在Addtask的界面添加小说标题,作者,内容;在TaskList展示添加的小说标题和作者;改正四段代码,输出完整修改后的代码;Model不要导入