将 Vuetify 2 添加到 vite 项目

发布于 2025-01-09 11:49:51 字数 2306 浏览 1 评论 0原文

我的团队使用基于类的组件和打字稿在 Vue 2 中开发应用程序。

现在,我们想迁移到 Vite - 因为它具有所有优势。

我已遵循本指南(我只能推荐) https://vueschool.io/ articles/vuejs-tutorials/how-to-migrate-from-vue-cli-to-vite/

简而言之 - 它不起作用。浏览器甚至无法从index.html获取“/src/main.js” 但是如果我输入“/src/main.ts”,它会获取它,但会显示错误,表明 Vuetify 甚至没有安装。 我知道.ts文件是浏览器无法读取的,我只是在调试了2个小时后尝试了一下

此外,我在Vuetify文档中看到“第一方Vite支持”仍有待发布。 https://vuetifyjs.com/en/introduction/roadmap/#in-development

我的问题是 - 是否可以在 Vite 中添加 Vuetify 应用程序

package.json(仅依赖项)

  "dependencies": {
    "vue": "^2.6.12",
    "vue-class-component": "^7.2.6",
    "vue-property-decorator": "^9.1.2",
    "vuetify": "^2.4.0",
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.18.0",
    "@typescript-eslint/parser": "^4.18.0",
    "@vitejs/plugin-vue": "^1.6.1",
    "@vue/cli-plugin-typescript": "^4.5.15",
    "@vue/eslint-config-typescript": "^7.0.0",
    "eslint": "^8.9.0",
    "eslint-plugin-vue": "^8.5.0",
    "sass": "~1.32.0",
    "typescript": "~4.1.5",
    "vite": "^2.6.13",
    "vite-plugin-vue2": "^1.9.2",
    "vue-cli-plugin-vuetify": "^2.4.5",
    "vue-template-compiler": "^2.6.14",
    "vuetify-loader": "^1.7.0"
  },

tsconfig.json

{
  "compilerOptions": {
    // ...
    "target": "esnext",
    "module": "esnext",
    "isolatedModules": true,
    "useDefineForClassFields": true,
    "types": [
      "webpack-env",
      "vite/client"
    ],

vite.config.js

import { defineConfig } from "vite";
import { createVuePlugin as vue } from "vite-plugin-vue2";
const path = require("path");

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
});

顶部呈现的空 html,以及“no vuetify”错误指示器 当我将 .ts 导入 index.html

<script type="module" src="/src/main.ts"></script>

在此处输入图像描述

My team developed application in Vue 2 with class based components and typescript.

Now, we want to migrate to Vite - because of all the advantages it carries.

I have followed this guide (which I can only recommend)
https://vueschool.io/articles/vuejs-tutorials/how-to-migrate-from-vue-cli-to-vite/

In short - it don't work. Browser can't even fetch "/src/main.js" from index.html
But if I put "/src/main.ts" it fetches it, but shows errors which indicates Vuetify is not even installed.
I know .ts files are not readable by browser, I just tried it after 2 hours of debugging

Also, I saw in Vuetify documentation that "First party Vite support" is still to be released.
https://vuetifyjs.com/en/introduction/roadmap/#in-development

My question is - is it even possible to add Vuetify in Vite application?

package.json (dependencies only)

  "dependencies": {
    "vue": "^2.6.12",
    "vue-class-component": "^7.2.6",
    "vue-property-decorator": "^9.1.2",
    "vuetify": "^2.4.0",
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.18.0",
    "@typescript-eslint/parser": "^4.18.0",
    "@vitejs/plugin-vue": "^1.6.1",
    "@vue/cli-plugin-typescript": "^4.5.15",
    "@vue/eslint-config-typescript": "^7.0.0",
    "eslint": "^8.9.0",
    "eslint-plugin-vue": "^8.5.0",
    "sass": "~1.32.0",
    "typescript": "~4.1.5",
    "vite": "^2.6.13",
    "vite-plugin-vue2": "^1.9.2",
    "vue-cli-plugin-vuetify": "^2.4.5",
    "vue-template-compiler": "^2.6.14",
    "vuetify-loader": "^1.7.0"
  },

tsconfig.json

{
  "compilerOptions": {
    // ...
    "target": "esnext",
    "module": "esnext",
    "isolatedModules": true,
    "useDefineForClassFields": true,
    "types": [
      "webpack-env",
      "vite/client"
    ],

vite.config.js

import { defineConfig } from "vite";
import { createVuePlugin as vue } from "vite-plugin-vue2";
const path = require("path");

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
});

empty html rendered in the top, and "no vuetify" error indicator
when I put .ts import in index.html

<script type="module" src="/src/main.ts"></script>

enter image description here

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

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

发布评论

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

评论(4

娇俏 2025-01-16 11:49:51

支持 Vuetify 2 链接

首先安装插件

npm i unplugin-vue-components -D

然后在你的 vite.config.ts 文件中:

import { defineConfig } from 'vite'
import { createVuePlugin } from 'vite-plugin-vue2'
import { VuetifyResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    createVuePlugin(/* options */),
    Components({
      resolvers: [
        // Vuetify
        VuetifyResolver(),
      ],
    }),
  ]
})

There is support for Vuetify 2 link

first install the plugin

npm i unplugin-vue-components -D

Then in your vite.config.ts file:

import { defineConfig } from 'vite'
import { createVuePlugin } from 'vite-plugin-vue2'
import { VuetifyResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    createVuePlugin(/* options */),
    Components({
      resolvers: [
        // Vuetify
        VuetifyResolver(),
      ],
    }),
  ]
})
菊凝晚露 2025-01-16 11:49:51

安装 unplugin-vue-components

npm install -D unplugin-vue-components

然后在 vite.config.ts 中添加插件

import { defineConfig } from 'vite'
import vue2 from '@vitejs/plugin-vue2'
import Components from 'unplugin-vue-components/vite'
import { VuetifyResolver } from 'unplugin-vue-components/resolvers'

defineConfig({
  plugins: [
     vue2(),
     Components({
        resolvers: [VuetifyResolver()],
     })
  ]
})

如果你也使用 vuetify 自定义 sass 变量

defineConfig({
  plugins: [
    vue2(),
    Components({
      resolvers: [VuetifyResolver()],
    }),
  ],
  css: {
    // https://vitejs.dev/config/#css-preprocessoroptions
    preprocessorOptions: {
      sass: {
        additionalData: [
          // vuetify variable overrides
          '@import "@/styles/variables.scss"',
          '',
        ].join('\n'),
      },
    },
  },
})

你可以参考我的 Vite 3 + Vuetify 2 + Vue 2.7 的入门模板
它还包括 Vuetify 2 组件中的 TypeScript 支持和 IntelliSense

https://github.com/kingyue737/vitify-admin

Install unplugin-vue-components.

npm install -D unplugin-vue-components

then add the plugin in vite.config.ts

import { defineConfig } from 'vite'
import vue2 from '@vitejs/plugin-vue2'
import Components from 'unplugin-vue-components/vite'
import { VuetifyResolver } from 'unplugin-vue-components/resolvers'

defineConfig({
  plugins: [
     vue2(),
     Components({
        resolvers: [VuetifyResolver()],
     })
  ]
})

If you also use vuetify custom sass variable

defineConfig({
  plugins: [
    vue2(),
    Components({
      resolvers: [VuetifyResolver()],
    }),
  ],
  css: {
    // https://vitejs.dev/config/#css-preprocessoroptions
    preprocessorOptions: {
      sass: {
        additionalData: [
          // vuetify variable overrides
          '@import "@/styles/variables.scss"',
          '',
        ].join('\n'),
      },
    },
  },
})

You can refer to my starter template with Vite 3 + Vuetify 2 + Vue 2.7
It also includes TypeScript support and IntelliSense in Vuetify 2 components

https://github.com/kingyue737/vitify-admin

俯瞰星空 2025-01-16 11:49:51

我已经创建了 startar 模板。

https://github.com/logue/vite-vue2-vuetify-ts- starter

最初是为使用 vue-property-decorator 的项目制作的,它还支持组合 api。

如果您想使用合成 API 访问 vuetify 函数,可以使用 useVuetify() 访问它。

I've created startar template.

https://github.com/logue/vite-vue2-vuetify-ts-starter

Originally made for projects using vue-property-decorator, it also supports composition api.

If you want to access the vuetify function with the composition api, you can access it with useVuetify().

甜心 2025-01-16 11:49:51

我创建了存储库,将默认的 vue2/vuetify2 官方模板从 vue-cli 迁移到 vite 5

它主要使用:

  • @vitejs/plugin-vue2vue:^2.7.0(这是vue 2所需的最低版本)
  • unplugin-vue-components for vuetify 2

请参阅:https://github.com/sbernard31/vuetify2-vite-template

I created repo where I migrate default vue2/vuetify2 official template from vue-cli to vite 5.

It used mainly :

  • @vitejs/plugin-vue2 and vue:^2.7.0 (this is minimal version required) for vue 2
  • unplugin-vue-components for vuetify 2

See : https://github.com/sbernard31/vuetify2-vite-template

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