[英]SlateJS Typscript node.children url and type property does not exist
TLDR: Error message for SlateJS serializing to HTML Property 'children' does not exist on type 'Node[]'
. TLDR:SlateJS 序列化为 HTML
Property 'children' does not exist on type 'Node[]'
错误消息Property 'children' does not exist on type 'Node[]'
。
Going off on SlateJS Serializing Docs but in tsx.开始使用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
};
};
I'm getting the following property error for children , type and url .对于children 、 type和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[]'
In my rich editor I have:在我的丰富编辑器中,我有:
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>
)
I already have the type dependencies.我已经有了类型依赖。
"@types/slate-react": "^0.22.9"
"@types/slate": "^0.47.7",
serializeHTML()
method can just take a single Node and have the iterator from where it's called. serializeHTML()
方法可以只接受一个 Node 并从它被调用的地方获得迭代器。
Function :功能:
const serializeHTML = (node: Node) => {
...
const children = node.children.map((n: Node) => serializeHTML(n)).join('');
...
};
Call :电话:
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>
)
};
It looks like you are trying to access properties on a Node Array instead of a Node.看起来您正在尝试访问节点阵列而不是节点上的属性。
A couple of other things worth mentioning:还有一些值得一提的事情:
Node
type from Slate ( import { Node } from 'slate'
).Node
类型( import { Node } from 'slate'
)。 There is a globally available Node
type that will be used if you don't and it is not the one you want (this doesn't seem to be your issue but worth mentioning for anyone else who runs into similar errors)Node
类型,如果你不这样做,它将被使用,它不是你想要的(这似乎不是你的问题,但对于遇到类似错误的其他人来说值得一提)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.