【问题标题】:Typescript can't use animateMotion打字稿不能使用 animateMotion
【发布时间】:2019-06-07 19:16:18
【问题描述】:

当我尝试在 React 组件中使用 animateMotion 时,Typescript 会报错。

Property 'animateMotion' does not exist on type 'JSX.IntrinsicElements'.

Property 'mpath' does not exist on type 'JSX.IntrinsicElements'.

代码

import * as React from 'react'

type OrbitProps = {
  duration: number;
  radius: number;
}

export const Orbit: React.FunctionComponent<OrbitProps>= ({duration, radius}) => {
  const cx = 50
  const cy = 50
  return (
      <g>
        <path
          d={`M ${cx}, ${cy}
          m -${radius}, 0
          a ${radius},${radius} 0 1,0 ${radius*2},0
          a ${radius},${radius} 0 1,0 -${radius*2},0`}
          id="ellipse1"
        />
        <circle r="3">
          <animateMotion dur={`${duration}s`} repeatCount="indefinite">
            <mpath xlinkHref="#ellipse1" />
          </animateMotion>
        </circle>
      </g>
  )
}

【问题讨论】:

    标签: reactjs typescript svg svg-animate


    【解决方案1】:

    已编辑:

    declare namespace JSX { 
      interface IntrinsicElements { 
        "animateMotion": any,
        "mpath": any, 
      } 
    }
    

    来源:https://www.typescriptlang.org/docs/handbook/jsx.html

    【讨论】:

    • 已经尝试过了,但它不起作用,它是一个“原生”SVG 元素,所以它应该是小写的,就像任何其他 divsvgg 或其他任何东西一样。
    • 你能试试这个吗? declare namespace JSX { interface IntrinsicElements { "animateMotion": any, "mpath": any, } }
    • 成功了,谢谢!添加作为它自己的回复或编辑这个,我会接受它:)
    • 没问题,我编辑了我的答案。也许它可以帮助别人:)
    • @sdkcy 您介意花一点时间解释一下declare namespace JSX 的工作原理吗?尝试搜索文档,但没有找到。
    【解决方案2】:

    你安装npm install @types/react --save了吗?

    【讨论】:

    猜你喜欢
    • 2017-11-28
    • 2021-07-16
    • 1970-01-01
    • 2021-07-01
    • 2022-10-06
    • 1970-01-01
    • 2021-06-30
    • 2018-09-30
    • 2016-12-12
    相关资源
    最近更新 更多