gpt4 book ai didi

javascript - Typescript 错误 "Property does not exist on type ' JSX.IntrinsicElements'"使用 native Web 组件时

转载 作者:行者123 更新时间:2023-11-30 11:06:28 31 4
gpt4 key购买 nike

我正在处理一个使用 React 和 Typescript 的项目,但我想开始在我的项目中使用原生 Web 组件以逐步淘汰我的一些 React 组件。

当我尝试在我的某些 JSX 中使用 person-info 组件时出现此错误。

Property does not exist on type 'JSX.IntrinsicElements'

我查看了其他一些存在这些问题的问题,但它们似乎都与 native Web 组件无关。

当我在我的项目中使用我的网络组件时,如何让 Typescript 和 React 很好地发挥作用?

个人信息.mjs

const css = `
<style>
:host([hidden]) { display: none; }
:host {
align-items: center;
display: grid;
font-weight: normal;
grid-gap: var(--spacing-size-a) var(--spacing-size-a);
grid-template-areas:
'picture heading'
'picture sub-heading';
grid-template-columns: auto 1fr;
justify-items: start;
}
div {
grid-area: picture;
}
h1, h2 {
margin: 0;
padding: 0;
}
h1 {
align-self: end;
font-size: var(--l-text-size);
font-weight: normal;
grid-area: heading;
text-transform: capitalize;
}
h2 {
align-self: start;
font-size: var(--m-text-size);
grid-area: sub-heading;
}
ion-icon {
font-size: 56px;
}
</style>
`

const html = `
<div>
<ion-icon name="md-contact"></ion-icon>
</div>
<h1>Heading</h1>
<h2>Sub-heading</h2>
`

class PersonInfo extends HTMLElement {
static get observedAttributes () {
return [
'heading',
'subHeading',
'size'
]
}

constructor () {
super()

this.attachShadow({mode: 'open'})
this.shadowRoot.appendChild(template.content.cloneNode(true))
}

connectedCallback () {
this.shadowRoot.querySelector('h1').innerText = this.getAttribute('heading')
this.shadowRoot.querySelector('h2').innerText = this.getAttribute('subHeading')
}

get heading () {
return this.getAttribute('heading')
}
set heading (newValue) {
this.setAttribute('heading', newValue)
this.shadowRoot.querySelector('h1').innerText = newValue
}

get subHeading () {
return this.getAttribute('subHeading')
}
set subHeading (newValue) {
this.setAttribute('subHeading', newValue)
this.shadowRoot.querySelector('h2').innerText = newValue
}

get size () {
return this.getAttribute('size')
}
set size (newValue) {
this.setAttribute('size', newValue)
}
}

const template = document.createElement('template')
template.innerHTML = `${css}${html}`

window.customElements.define('person-info', PersonInfo)

导入语句

import '../../common/WebComponents/PersonInfo.mjs'

在 JSX 中的使用

<main>
<person-info
heading='Bruce Wayne'
subHeading="I'M BATMAN!"
/>
</main>

最佳答案

去了之后想通了here如何消除此特定错误。

import * as React from 'react'

declare global {
namespace JSX {
interface IntrinsicElements {
'person-info': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
}
}
}

但是,由于我在组件上使用的自定义属性,此后我又遇到了另一个错误。感谢 Shanon 的评论,我也想出了如何解决这个问题,并最终得到了我刚刚导入到我的 App.tsx 文件中的最终代码。

import * as React from 'react'

declare global {
namespace JSX {
interface IntrinsicElements {
'person-info': PersonInfoProps
}
}
}

interface PersonInfoProps extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement> {
heading: string,
subHeading: string,
size?: string
}

关于javascript - Typescript 错误 "Property does not exist on type ' JSX.IntrinsicElements'"使用 native Web 组件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55424417/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com