英文:
Preact router Property 'path' does not exist on type 'IntrinsicAttributes'
问题
我在制作我的网站时遇到了一个问题,使用preact router。每当我尝试在路由器中放置某些内容时,会出现一个错误,错误消息是“属性'path'在类型'IntrinsicAttributes'上不存在”。但这并不会阻止网站运行,但每当我将URL更改为'/shop'时,它不会显示任何内容。
shop函数
import { Item } from '../../Types/types'
import './item.css'
import Navbar from '../Navbar'
export default function Items(items:Array<Item>) {
return (
<>
<Navbar />
<div className='container'>
{items.map(item => (
<div className='item'>
<div>
<h3>
{item.name}
</h3>
</div>
<div className='itemimage'>
<img src={item.picture} alt={item.name} />
</div>
<div>
<strong>{item.price}</strong>
</div>
</div>
))}
</div>
</>
)
}
我的路由器函数
import Router from 'preact-router'
import { App } from '../app'
import Items from './shop/items'
export const Route = () => {
return (
<Router>
<App path='/' />
<Items path='/shop' />
</Router>
)
}
我尝试按照preact router的教程操作,但没有成功。然后我尝试查找这个问题,但没有找到与preact-router的这个确切问题相关的信息。
英文:
I am currently going through a problem whilst making a website of mine with preact router. whenever I try to put something in the router it has an error of Property 'path' does not exist on type 'IntrinsicAttributes'. But it doesn't stop the site from running but whenever I change the url to '/shop' it doesn't show anything.
shop function
import { Item } from '../../Types/types'
import './item.css'
import Navbar from '../Navbar'
export default function Items(items:Array<Item>) {
return (
<>
<Navbar />
<div className='container'>
{
items.map( item => {
return (
<div className='item'>
<div>
<h3>
{item.name}
</h3>
</div>
<div className='itemimage'>
<img src={item.picture} alt={item.name} />
</div>
<div>
<strong>{item.price}</strong>
</div>
</div>
)})
}
</div>
</>
)
}
and my router function
import Router from 'preact-router'
import {App} from '../app'
import Items from './shop/items'
export const Route = () => {
return (
<Router>
<App path='/' />
<Items path='/shop' />
</Router>
)
}
I tried following the tutorial on preact router. It did not work. I then tried to look up this problem there was nothing about this exact problem with preact-router.
答案1
得分: 2
这是TS的固有限制,不过有一个替代方案是从preact-router
导出的<Route>
组件:
import { Route, Router } from 'preact-router'
import { App } from '../app'
import Items from './shop/items'
export const RouteX = () => {
return (
<Router>
<Route path="/" component={<App />} />
<Route path="/shop" component={<Items />} />
</Router>
)
}
基本上,TS的严格性意味着<Router>
不能将其子组件额外定义为接受path
属性。因此,您需要使用包装器来将路径应用于组件,而不是直接将路径应用于组件。
但它不会阻止网站运行,但无论何时更改url为'/shop',都不会显示任何内容。
它不显示任何内容,因为您a)没有正确使用props,b)没有将items传递给<Items>
。
export default function Items({ items: Array<Item> }) {
<Route path="/shop" component={<Items items={...} />} />
Props始终是一个对象,因此您需要解构或访问props对象上的.items
。但是,由于您没有将items
提供给<Items>
,因此您的组件没有可用的数据来处理。
英文:
This is an inherent limitation of TS, though there is an alternative which is the <Route>
component, exported from preact-router
:
import { Route, Router } from 'preact-router'
import { App } from '../app'
import Items from './shop/items'
export const RouteX = () => {
return (
<Router>
<Route path="/" component={<App />} />
<Route path="/shop" component={<Items />} />
</Router>
)
}
Essentially, the strictness of TS means that <Router>
can't define its children as additionally taking a path
prop. So instead of applying your path to the component directly, you will need to use a wrapper to appease TS.
> But it doesn't stop the site from running but whenever I change the url to '/shop' it doesn't show anything
It doesn't show anything as you're a) not using props correctly and b) not passing items to <Items>
.
export default function Items({ items: Array<Item> }) {
<Route path="/shop" component={<Items items={...} />} />
Props are always an object, so you'll need to destructure or access .items
on your props object. As you're not providing items
to <Items>
though, your component has no data to work with.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论