简体   繁体   English

NextJS Link 没有呈现锚标记

[英]NextJS Link isn't rendering an anchor tag

I'm using React + NextJS, and I'm trying to render a list of products, much like you'd see on a typical ecommerce category page.我正在使用 React + NextJS,我正在尝试呈现产品列表,就像您在典型的电子商务类别页面上看到的一样。 Each product is in a p , and that p should link to the appropriate detail page (so it should be surrounded by an anchor a tag).每个产品都在一个p中,并且该p应该链接到适当的详细信息页面(因此它应该被一个锚点a标签包围)。

Unfortunately, the links work but they don't render actual anchor tags.不幸的是,链接可以工作,但它们不会呈现实际的锚标记。 Can anyone explain what I'm missing?谁能解释我错过了什么?

Scenario A: a normal text link (works as expected)场景 A:普通文本链接(按预期工作)


input:输入:

 <Link href="wherever">word holmes</Link>

output: output:

 <a href="wherever">word holmes</a>

Scenario B: linking a block of whatever场景 B:链接任意块


input:输入:

 <Link href="wherever"> <p>word holmes</p> </Link>

output: output:

 <p>word holmes</p>

desired output:所需的 output:

 <a href="wherever"><p>word holmes</p></a>
  • where'd the <a /> tag go?? <a />标签 go 在哪里?
  • mousing over the block does not get a pointer cursor, but clicking it does send you to the href target将鼠标悬停在块上不会获得指针 cursor,但单击它确实会将您发送到href目标

Scenario C: adding my own a to the block of whatever场景 C:将我自己a添加到任何块中


input:输入:

 <Link href="wherever"> <a> <p>word holmes</p> </a> </Link>

output: output:

 <a href="wherever"><p>word holmes</p></a>
  • ok this works, but it feels hacky..好的,这可行,但感觉很hacky..
  • why does this work?为什么这行得通? Is this the "right" way to do it?这是“正确”的做法吗?

According to https://github.com/vercel/next.js/blob/canary/packages/next/client/link.tsx the a tag is added automatically if the child is a string.根据https://github.com/vercel/next.js/blob/canary/packages/next/client/link.tsx如果子项是字符串,则会自动添加a标签。 Otherwise it just returns the child.否则它只会返回孩子。 So in your case the child is a p tag, so that's all that is returned.所以在你的情况下,孩子是一个p标签,所以这就是返回的全部内容。 Seems like you could just wrap that in an a tag and that should work.似乎您可以将其包装在a标签中,并且应该可以。

But there should be a way to prevent this.但应该有办法防止这种情况发生。 I don't think that it is legitimate way of doing this...我认为这样做不是合法的方式......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM