英文:
Cannot start nuxt: Cannot read properties of undefined (reading 'sitemap')
问题
在你的项目中,安装了 @nuxtjs/sitemap 插件后,运行时遇到了错误。可能是由于版本不匹配引起的。请确保你的依赖版本与插件的要求相符。
英文:
I'm working on my first nuxt js project and I've a small problem. I installed a sitemap in my project using "npm install @nuxtjs/sitemap" and I've an error that I don't know how to fix.
package.json
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
{
"name": "nuxt-app",
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare"
},
"devDependencies": {
"nuxt": "^3.3.3",
"nuxt-simple-sitemap": "^2.4.8",
"sitemap": "^7.1.1"
},
"dependencies": {
"@nuxtjs/bootstrap-vue": "^2.0.4",
"@nuxtjs/sitemap": "^2.4.0",
"@nuxtjs/tailwindcss": "^6.1.3",
"bootstrap-vue": "^2.23.1"
}
}
<!-- end snippet -->
nuxt.config.ts
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
modules: [
'@nuxtjs/tailwindcss',
'@nuxtjs/sitemap',
],
})
<!-- end snippet -->
This is the error I've when I try to run the project
Thanks for your help !
答案1
得分: 1
@nuxtjs/sitemap 现在不再与 Nuxt3 兼容。
对于简单的静态网站
您可以使用 Nuxt3 的新 simple-sitemap
模块快速生成您的站点地图。您可以按照它们的指示在此处进行操作。
如果您有动态路由(如 /blog/[slug].vue)
您可以仅使用 sitemapJS 插件生成您的 sitemap.xml。
npm install --save-dev sitemap
在您的 nuxt.config 中添加以下代码行
export default defineNuxtConfig({
// ...
nitro: {
prerender: {
routes: ['/sitemap.xml']
}
}
})
现在,您需要创建一个文件,以在运行 nuxi generate / build 时执行。将该文件命名为 sitemap.xml.ts
,放在 /server/routes/
目录下。
在您刚刚创建的文件中,您需要编写此代码片段 以生成动态路由。
英文:
@nuxtjs/sitemap is no longer compatible with Nuxt3.
For a simple static website
You can quickly generate your sitemap using the new simple-sitemap
module for Nuxt3. You can follow their instructions here.
If you have dynamic routes (as /blog/[slug].vue)
You can generate your sitemap.xml using only the sitemapJS plugin.
npm install --save-dev sitemap
Add these lines of code in your nuxt.config
export default defineNuxtConfig({
// ...
nitro: {
prerender: {
routes: ['/sitemap.xml']
}
}
})
Now you have to create a file to execute when you run the nuxi generate / build. Name that file sitemap.xml.ts
inside the directory /server/routes/
.
Inside the file, you just created you have to write this code snippet in order to generate the dynamic routes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论