首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SlateJS脚本node.children url和类型属性不存在

SlateJS脚本node.children url和类型属性不存在
EN

Stack Overflow用户
提问于 2020-08-24 22:27:27
回答 2查看 1.6K关注 0票数 1

TLDR: SlateJS序列化为HTML Property 'children' does not exist on type 'Node[]'的错误消息。

SlateJS序列化文档上开播,但在tsx。

代码语言:javascript
复制
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[]'

在我丰富的编辑中,我有:

代码语言:javascript
复制
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",
EN

回答 2

Stack Overflow用户

发布于 2020-08-26 13:51:15

serializeHTML()方法只需要一个节点,并从调用它的地方获得迭代器。

函数

代码语言:javascript
复制
const serializeHTML = (node: Node) => {
  ...

  const children = node.children.map((n: Node) => serializeHTML(n)).join('');
  ...
};

调用

代码语言:javascript
复制
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>
  )
};
票数 0
EN

Stack Overflow用户

发布于 2020-09-27 21:42:27

看起来,您正在尝试访问Node而不是Node上的属性。

还有几件事值得一提:

  1. 较晚版本的板岩(.5x)是用TypeScript编写的,不应该需要显式类型安装。您列出的那些@type/xxx依赖项不是正确的类型。这是给.47x的。
  2. 确保从板岩( Node )导入Node类型( import { Node } from 'slate' )。有一个全局可用的Node类型,如果您不使用它,它不是您想要的类型(这似乎不是您的问题,但是对于其他遇到类似错误的人来说,这一点值得一提)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63569491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档