我按照一篇文章制作了一个项目日记应用程序。它使用 redux 工具包和 miraj 作为假服务器。它有一个名为 Editor 的组件
const Editor: FC = () => {
const { currentlyEditing: entry, canEdit, activeDiaryId } = useSelector(
(state: RootState) => state.editor
);
const [editedEntry, updateEditedEntry] = useState(entry);
const dispatch = useAppDispatch();
const saveEntry = async () => {
if (activeDiaryId == null) {
return showAlert('Please select a diary.', 'warning');
}
if (entry == null) {
http
.post<Entry, { diary: Diary; entry: Entry }>(
`/diaries/entry/${activeDiaryId}`,
editedEntry
)
.then((data) => {
if (data != null) {
const { diary, entry: _entry } = data;
dispatch(setCurrentlyEditing(_entry));
dispatch(updateDiary(diary));
}
});
作为一个新手,我无法理解以下内容:
enter code here const { currentlyEditing: entry, canEdit, activeDiaryId } = useSelector(
在这一行中以我的理解条目是一个类型。但是文章中没有条目的类型或接口。第二个
.post<Entry, { diary: Diary; entry: Entry }>(
`/diaries/entry/${activeDiaryId}`,
我们正在发出一个发布请求,在 miraj 服务器中,我们将路由定义为:
this.post('/diaries/entry/:id', diary.addEntry);
我不明白这个 :id 部分。我们如何在这里获取 id?
最后一件事:) 在这一行: const { diary, entry: _entry } = data; 调度(setCurrentlyEditing(_entry));为什么输入前有下划线?和上面一样的问题是它是打字稿的类型还是我不知道的一些解构方法。