Astro教程跟随时出现引用错误。

huangapple go评论105阅读模式
英文:

Astro reference error when I follow the tutorial

问题

I have the following index.astro:

import Navigation from '../components/Navigation.astro';

<html lang="de">

<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.ico" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator}>
<title>Pagetitle</title>
</head>

<body>
    <Navigation />
</body>

</html>

and following Navigation.astro:

<a href="/">Home</a>
<a href="/about/">About</a>
<a href="/blog/">Blog</a>

but I keep getting the error that Navigation is an unknown element pre-compile warning and a reference error when npm run dev.
Can someone please elaborate?

I tried running the above code and expected the Navigation to be rendered on the index page. But instead, I get a reference error telling me Navigation is undefined.

英文:

I have the following index.astro:

import Navigation from &#39;../components/Navigation.astro&#39;;
&lt;html lang=&quot;de&quot;&gt;

&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot; /&gt;
&lt;link rel=&quot;icon&quot; type=&quot;image/svg+xml&quot; href=&quot;/favicon.ico&quot; /&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width&quot; /&gt;
&lt;meta name=&quot;generator&quot; content={Astro.generator}&gt;
&lt;title&gt;Pagetitle&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;Navigation /&gt;
&lt;/body&gt;
&lt;/html&gt;

and following Navigation.astro

&lt;a href=&quot;/&quot;&gt;Home&lt;/a&gt;
&lt;a href=&quot;/about/&quot;&gt;About&lt;/a&gt;
&lt;a href=&quot;/blog/&quot;&gt;Blog&lt;/a&gt;

but I keep getting the error that Navigation is an unknown element pre coimpile warning and a reference error when npm run dev.
Can some1 pls ellaborate?

I tried running the above code and expected the Navigation to be rendered in the index page. But instead I get a reference error telling me Navigation is undefined.

答案1

得分: 0

Navigation.astro必须包含在正文中的HTML部分,而不是在frontmatter部分。

正确写法:

---
---
&lt;a href=&quot;/&quot;&gt;Home&lt;/a&gt;
&lt;a href=&quot;/about/&quot;&gt;About&lt;/a&gt;
&lt;a href=&quot;/blog/&quot;&gt;Blog&lt;/a&gt;

错误写法,缺少了2行---

&lt;a href=&quot;/&quot;&gt;Home&lt;/a&gt;
&lt;a href=&quot;/about/&quot;&gt;About&lt;/a&gt;
&lt;a href=&quot;/blog/&quot;&gt;Blog&lt;/a&gt;

这里有一个参考教程,可以再次查看:https://docs.astro.build/en/tutorial/3-components/1/#create-a-navigation-component

关于代码围栏(frontmatter)部分的更多细节,请参阅此处:https://docs.astro.build/en/core-concepts/astro-components/#component-structure

基本上,这是将代码与HTML Astro语法分开的方法。

英文:

Navigation.astro must include the HTML section in the body not in the frontmatter section

right :

---
---
&lt;a href=&quot;/&quot;&gt;Home&lt;/a&gt;
&lt;a href=&quot;/about/&quot;&gt;About&lt;/a&gt;
&lt;a href=&quot;/blog/&quot;&gt;Blog&lt;/a&gt;

wrong missing x2 lines with ---

&lt;a href=&quot;/&quot;&gt;Home&lt;/a&gt;
&lt;a href=&quot;/about/&quot;&gt;About&lt;/a&gt;
&lt;a href=&quot;/blog/&quot;&gt;Blog&lt;/a&gt;

here is a reference to the tutorial to check again https://docs.astro.build/en/tutorial/3-components/1/#create-a-navigation-component

see more details about the code fence (frontmatter) section here https://docs.astro.build/en/core-concepts/astro-components/#component-structure

It is basically a separation of code from html astro syntax.

huangapple
  • 本文由 发表于 2023年6月8日 23:05:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定