如何解决在我的Nuxt.js项目中无法将NuxtLink连接到页面和组件的问题?

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

How can I resolve the issue of being unable to connect NuxtLink to pages and components in my Nuxt.js project?

问题

Screenshot of the project structure

我正在尝试创建NuxtLink组件并将它们连接到页面。具体来说,我想将Navbar组件中的"ContactUs"链接连接到Contact页面。以下是我到目前为止尝试的代码片段:

在Navbar/components中:

<li class="nav-item">
  <NuxtLink to="/contact" class="nav-link click-close">ContactUs</NuxtLink>
</li>

在pages中:

<template>
  <div>
    <section id="contact">
      <h1>Contact</h1>
    </section>
  </div>
</template>

<script>
export default {
  name: 'contact'
}
</script>
英文:

Screenshot of the project structure

I am trying to create NuxtLink components and connect them to pages. Specifically, I want to connect the 'ContactUs' link in my Navbar component to the Contact page. Here's the code snippet I have tried so far

In Navbar/components:

&quot;[&lt;li class=&quot;nav-item&quot;&gt;&lt;NuxtLink to=&quot;/contact&quot; class=&quot;nav-link click-close&quot;&gt;ContactUs&lt;/NuxtLink&gt;&lt;/li&gt;][1]&quot;

In pages:

&quot;&lt;template&gt;
    &lt;div&gt;
        &lt;section id=&quot;contact&quot;&gt;
            &lt;h1&gt;Contact&lt;/h1&gt;
        &lt;/section&gt;
    &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
    name: &#39;contact&#39;
}

&lt;/script&gt;&quot;

答案1

得分: 0

如果你的结构正确,name属性是不需要的,路由是由文件夹和文件的名称定义的。

如果链接的页面存在于文件夹和文件中,链接将起作用。

我假设你想要在每个页面中都有一个带有导航栏的布局。
在导航栏中,你想要链接到页面。

如果是这种情况,这个基本配置将起作用:

app.vue

layout
  default.vue
  
components
  navBar.vue

pages
  index.vue
  contact.vue
  • app.vue
<template>
  <div>
    <NuxtPage />
  </div>
</template>
  • layout/default.vue
<template>
  <div>
    <NavBar />
    <slot />
  </div>
</template>
  • components/navBar.vue
<template>
  <div>
    <NuxtLink to="/">Home</NuxtLink>
    |
    <NuxtLink to="/contact">Contact Us</NuxtLink>
  </div>
</template>
  • pages/index.vue
<template>
  <NuxtLayout>
    <div>This is the homepage</div>
  </NuxtLayout>
</template>
  • pages/contact.vue
<template>
  <NuxtLayout>
    <div>This is the contact page</div>
  </NuxtLayout>
</template>

请注意,我使用了NuxtPageNuxtLayout,它们在任何项目结构中都非常有用。

我为你创建了一个可工作的示例,点击此处查看

希望这对你有所帮助。

英文:

If your structure is correct, the name property is not needed and the routes are defined by the folders and files names.

If the linked page exists by folder and file, the link will work.

I assume you want a layout with a navbar in every page.
In the navbar you want the links to the pages.

If this is the case, this basic configuration will work:

app.vue

layout
  default.vue
  
components
  navBar.vue

pages
  index.vue
  contact.vue
  • app.vue
&lt;template&gt;
  &lt;div&gt;
    &lt;NuxtPage /&gt;
  &lt;/div&gt;
&lt;/template&gt;
  • layout/default.vue
&lt;template&gt;
  &lt;div&gt;
    &lt;NavBar /&gt;
    &lt;slot /&gt;
  &lt;/div&gt;
&lt;/template&gt;
  • components/navBar.vue
&lt;template&gt;
  &lt;div&gt;
    &lt;NuxtLink to=&quot;/&quot;&gt;Home&lt;/NuxtLink&gt;
    |
    &lt;NuxtLink to=&quot;/contact&quot;&gt;Contact Us&lt;/NuxtLink&gt;
  &lt;/div&gt;
&lt;/template&gt;
  • pages/index.vue
&lt;template&gt;
  &lt;NuxtLayout&gt;
    &lt;div&gt;This is the homepage&lt;/div&gt;
  &lt;/NuxtLayout&gt;
&lt;/template&gt;
  • pages/contact.vue
&lt;template&gt;
  &lt;NuxtLayout&gt;
    &lt;div&gt;This is the contact page&lt;/div&gt;
  &lt;/NuxtLayout&gt;
&lt;/template&gt;

Note that I used NuxtPage and NuxtLayout that are very useful in any project structure.

I created a working reproduction for you here

I hope this help.

huangapple
  • 本文由 发表于 2023年6月22日 18:52:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531140.html
匿名

发表评论

匿名网友

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

确定