如何设置`vite-plugin-pages',找不到模块`〜pages'

发布于 2025-01-22 04:15:39 字数 1029 浏览 0 评论 0原文

我正在尝试在的帮助下,使用VITE设置基于文件系统的路由。 vite-plugin-pages

我使用YARN创建了项目,用vue-ts作为选项创建vite,并通过yarn添加vite-plugin-pages -dev ,<代码>纱线添加vue-router

在GitHub上的README之后,我已将以下内容添加到我的vite.config.ts

import Pages from 'vite-plugin-pages'

export default {
  plugins: [
    // ...
    Pages(),
  ],
}

但是,在下一步中,in main.ts

import { createRouter } from 'vue-router'
import routes from '~pages'

const router = createRouter({
  // ...
  routes,
})

我似乎无法从〜页导入。我找不到模块。 vue-router本身正常工作,因为我可以创建一个路由器罚款,我可以自己声明路由自己。在A 从“虚拟:生成页面” 导入路由,我不知道这两个工作方式。

因此,问题是,我将如何生成动态路线,从整体上设置vite-plugin-pages

I'm trying to set up file system based routing for a Vue 3 application using Vite with the help of vite-plugin-pages.

I created the project using yarn create vite with vue-ts as the options and added the plugin via yarn add vite-plugin-pages --dev, yarn add vue-router.

Following the readme on the github, I have added the following to my vite.config.ts:

import Pages from 'vite-plugin-pages'

export default {
  plugins: [
    // ...
    Pages(),
  ],
}

However, at the next step, in main.ts:

import { createRouter } from 'vue-router'
import routes from '~pages'

const router = createRouter({
  // ...
  routes,
})

I cannot seem to import from ~pages. I cannot find the module. vue-router itself is working fine, as I can create a router fine, declaring the routes myself. In a vite template, they seem to be using import routes from 'virtual:generated-pages' instead and I have no idea how either works.

So, the question is, how would I go about generating the dynamic routes and as a whole, set up the usage of vite-plugin-pages?

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

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

发布评论

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

评论(3

随遇而安 2025-01-29 04:15:39

您可以尝试这样的尝试:

import Pages from "vite-plugin-pages" 

export default defineConfig({ 
    plugins: [ 
        Pages({
            pagesDir: [
                {dir: 'src/pages', baseRoute: ''},
            ],
            extensions: ['vue'],
            syncIndex: true,
            replaceSquareBrackets: true,
            extendRoute(route) {
                if (route.name === 'about')
                    route.props = route => ({query: route.query.q})

                if (route.name === 'components') {
                    return {
                        ...route,
                        beforeEnter: (route) => {
                            // eslint-disable-next-line no-console
                            // console.log(route)
                        },
                    }
                }
            },
        }), 
    ],
});

然后在main.js

import { createRouter, createWebHistory } from 'vue-router';
import generatedRoutes from 'virtual:generated-pages'; 

const router = createRouter({
  history: createWebHistory(),
  routes: generatedRoutes,
});

You can try like this:

import Pages from "vite-plugin-pages" 

export default defineConfig({ 
    plugins: [ 
        Pages({
            pagesDir: [
                {dir: 'src/pages', baseRoute: ''},
            ],
            extensions: ['vue'],
            syncIndex: true,
            replaceSquareBrackets: true,
            extendRoute(route) {
                if (route.name === 'about')
                    route.props = route => ({query: route.query.q})

                if (route.name === 'components') {
                    return {
                        ...route,
                        beforeEnter: (route) => {
                            // eslint-disable-next-line no-console
                            // console.log(route)
                        },
                    }
                }
            },
        }), 
    ],
});

Then in main.js

import { createRouter, createWebHistory } from 'vue-router';
import generatedRoutes from 'virtual:generated-pages'; 

const router = createRouter({
  history: createWebHistory(),
  routes: generatedRoutes,
});
☆獨立☆ 2025-01-29 04:15:39
// tsconfig.json
"compilerOptions": {
    ...
    "types":  ["vite-plugin-pages/client"]
}
// tsconfig.json
"compilerOptions": {
    ...
    "types":  ["vite-plugin-pages/client"]
}
猫九 2025-01-29 04:15:39

您还可以在任何类型声明中声明参考。

/// <reference types="vite-plugin-pages/client" />

You can also declare the reference in any of your type declarations.

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