TLDR: SlateJS序列化为HTML Property 'children' does not exist on type 'Node[]'的错误消息。
在SlateJS序列化文档上开播,但在tsx。
import React, { useCallback, useMemo, useState } from "react";
import escapeHtml from 'escape-html';
import { Editor, createEditor, Node, Text } from 'slate';
import { withHistory } from 'slate-history';
const serializeHTML = (node: Node[]) => {
if (Text.isText(node)) {
return escapeHtml(node.text)
};
const children = node.children.map((n: Node[]) => serializeHTML(n)).join('');
switch (node.type) {
case 'link':
return `<a href="${escapeHtml(node.url)}">${children}</a>`
case 'list-item':
return ``
case 'paragraph':
return `<p>${children}</p>`
case 'quote':
return `<blockquote>${children}</blockquote>`
default:
return children
};
};对于子、类型和url,我得到以下属性错误。
Property 'children' does not exist on type 'Node[]'Property 'type' does not exist on type 'Node[]'Property 'url' does not exist on type 'Node[]'在我丰富的编辑中,我有:
const RichTextEditor = (props: RichTextEditorProps) => {
const editor = useMemo(() => withImages(withHistory(withReact(createEditor()))), []);
const [value, setValue] = useState<Node[]>(initialValue);
const html = serializeHTML(value);
return (
<Slate editor={editor} value={value} onChange={newValue => setValue(newValue)}>
...
</Slate>
)我已经有了类型依赖项。
"@types/slate-react": "^0.22.9""@types/slate": "^0.47.7",发布于 2020-08-26 13:51:15
serializeHTML()方法只需要一个节点,并从调用它的地方获得迭代器。
函数
const serializeHTML = (node: Node) => {
...
const children = node.children.map((n: Node) => serializeHTML(n)).join('');
...
};调用
const RichTextEditor = (props: RichTextEditorProps) => {
const editor = useMemo(() => withImages(withHistory(withReact(createEditor()))), []);
const [value, setValue] = useState<Node[]>(initialValue);
const html = value.map(v => serializeHTML(v)).join('');
return (
<Slate editor={editor} value={value} onChange={newValue => setValue(newValue)}>
...
</Slate>
)
};发布于 2020-09-27 21:42:27
看起来,您正在尝试访问Node而不是Node上的属性。
还有几件事值得一提:
Node )导入Node类型( import { Node } from 'slate' )。有一个全局可用的Node类型,如果您不使用它,它不是您想要的类型(这似乎不是您的问题,但是对于其他遇到类似错误的人来说,这一点值得一提)https://stackoverflow.com/questions/63569491
复制相似问题