nuxtpage vs for nuxt33

发布于 2025-02-08 08:59:41 字数 135 浏览 2 评论 0原文

这两个组件nuxt3中有什么区别,我如何正确使用它们?

如果我想使用pages /... 在这里创建链接并从页面跳到页面的正确方法是什么?

What is the difference between these two components in Nuxt3 and how do I use them correctly?

If I want to use pages/... what is the right approach here to create links and jump from page to page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万水千山粽是情ミ 2025-02-15 08:59:41

文档中的所有内容几乎都在解释: https://v3.nuxtjs.org/迁移/页面和layouts/

您需要在app.vue中使用

<template>
  <nuxt-layout>
    <nuxt-page /> <!-- used to display the nested pages -->
  </nuxt-layout>
</template>

default /layouts/default.vue.vue文件,

<template>
  <div>
    this is coming from the layout
    <slot /> <!-- required here only -->
  </div>
</template>

您将在上/(带有/pages/index.vue

<template>
  <div>index page</div>
</template>

“在此处”

并且使用以下结构,您将获得动态页面

/pages/users/index.vue

<script setup>
definePageMeta({
  layout: false
});

function goToDynamicUser() {
  return navigateTo({
    name: 'users-id',
    params: {
      id: 23
    }
  })
}
</script>

<template>
  <div>
    <p>
      index page
    </p>
    <button @click="goToDynamicUser">navigate to user 23</button>
  </div>
</template>

/pages/users/[id] .vue

<script setup>
definePageMeta({
  layout: false
});

const route = useRoute()
</script>

<template>
  <pre>{{ route.params.id }}</pre>
</template>

我在此处删除了布局以显示如何禁用它,但是您可以完全让默认值在这里,甚至提供自定义一个


因此,nuxt-page要在要在应用中显示页面时使用(替换&lt; nuxt/&gt; and code> and &lt; nuxt; nuxt; nuxt; nuxt; /&gt;)时&lt; slot/&gt;将用于布局(作为使用 插槽 tag )。

Everything is pretty much explained in the documentation: https://v3.nuxtjs.org/migration/pages-and-layouts/

You need to use this in app.vue

<template>
  <nuxt-layout>
    <nuxt-page /> <!-- used to display the nested pages -->
  </nuxt-layout>
</template>

With a default /layouts/default.vue file

<template>
  <div>
    this is coming from the layout
    <slot /> <!-- required here only -->
  </div>
</template>

You will get this on / (with /pages/index.vue)

<template>
  <div>index page</div>
</template>

enter image description here

And with the following structure, you will achieve dynamic pages

enter image description here

/pages/users/index.vue

<script setup>
definePageMeta({
  layout: false
});

function goToDynamicUser() {
  return navigateTo({
    name: 'users-id',
    params: {
      id: 23
    }
  })
}
</script>

<template>
  <div>
    <p>
      index page
    </p>
    <button @click="goToDynamicUser">navigate to user 23</button>
  </div>
</template>

/pages/users/[id].vue

<script setup>
definePageMeta({
  layout: false
});

const route = useRoute()
</script>

<template>
  <pre>{{ route.params.id }}</pre>
</template>

I've removed the layout here to show how to disable it, but you can totally let the default here or even provide a custom one.


So, nuxt-page is to be used when you want to display the pages in your app (replacing <nuxt /> and <nuxt-child />) while <slot /> is to be used in the layout (as any other component using the slot tag).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文