Vite 多种前端框架的构建

Vite 是一个快速的前端构建工具,最初是为 Vue 设计的,但现在已经支持多种前端框架的构建。它通过强大的插件系统可以支持现代流行的框架,如 React、Vue、Svelte、Preact、Lit 等。

下面我会展示 Vite 支持的一些常见前端框架的构建,并对比其使用示例。

1. Vue 3 项目构建

Vite 最初是为 Vue 3 设计的,因此对 Vue 支持非常完善。

初始化 Vue 3 项目
pnpm create vite my-vue-app --template vue

或者使用 npm:

npm init @vitejs/app my-vue-app --template vue
主要配置示例

vite.config.js 中默认会自动集成 Vue 的插件:

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
  plugins: [vue()],
});
Vue 3 示例代码
<template>
  <div>{{ message }}</div>
</template>

<script setup>
import { ref } from 'vue';

const message = ref('Hello Vue 3 with Vite!');
</script>

2. React 项目构建

Vite 对 React 的支持也非常成熟,官方提供了 React 的插件。

初始化 React 项目
pnpm create vite my-react-app --template react

或者使用 npm:

npm init @vitejs/app my-react-app --template react
主要配置示例

vite.config.js 会自动引入 React 插件:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
});
React 示例代码
import { useState } from 'react';

function App() {
  const [message, setMessage] = useState('Hello React with Vite!');

  return <div>{message}</div>;
}

export default App;

3. Svelte 项目构建

Vite 也很好地支持 Svelte,通过 @sveltejs/vite-plugin-svelte 插件来集成。

初始化 Svelte 项目
pnpm create vite my-svelte-app --template svelte

或者使用 npm:

npm init @vitejs/app my-svelte-app --template svelte
主要配置示例

vite.config.js 配置 Svelte 插件:

import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';

export default defineConfig({
  plugins: [svelte()],
});
Svelte 示例代码
<script>
  let message = 'Hello Svelte with Vite!';
</script>

<main>
  <h1>{message}</h1>
</main>

4. Preact 项目构建

Preact 是一个轻量级的 React 替代品,Vite 支持通过 @preact/preset-vite 插件来构建 Preact 项目。

初始化 Preact 项目
pnpm create vite my-preact-app --template preact

或者使用 npm:

npm init @vitejs/app my-preact-app --template preact
主要配置示例

vite.config.js 配置 Preact 插件:

import { defineConfig } from 'vite';
import preact from '@preact/preset-vite';

export default defineConfig({
  plugins: [preact()],
});
Preact 示例代码
import { useState } from 'preact/hooks';

function App() {
  const [message, setMessage] = useState('Hello Preact with Vite!');

  return <div>{message}</div>;
}

export default App;

5. Lit 项目构建

Lit 是用于构建 Web Components 的库,Vite 可以通过简单的配置支持它。

初始化 Lit 项目
pnpm create vite my-lit-app --template vanilla

或者使用 npm:

npm init @vitejs/app my-lit-app --template vanilla
主要配置示例

不需要额外的插件,Vite 默认支持构建。

vite.config.js

import { defineConfig } from 'vite';

export default defineConfig({});
Lit 示例代码
import { LitElement, html, css } from 'lit';

class MyComponent extends LitElement {
  static styles = css`
    div {
      color: blue;
    }
  `;

  render() {
    return html`<div>Hello Lit with Vite!</div>`;
  }
}

customElements.define('my-component', MyComponent);

6. Vanilla JavaScript 项目构建

Vite 对原生 JavaScript 项目的支持也是极其简单的,适合没有框架的轻量应用。

初始化 Vanilla 项目
pnpm create vite my-vanilla-app --template vanilla

或者使用 npm:

npm init @vitejs/app my-vanilla-app --template vanilla
主要配置示例

不需要插件,默认支持。

vite.config.js

import { defineConfig } from 'vite';

export default defineConfig({});
Vanilla 示例代码
document.getElementById('app').innerHTML = `<h1>Hello Vanilla JS with Vite!</h1>`;

7. 对比使用示例

框架初始化命令Vite 插件示例代码 (展示 “Hello World”)
Vuepnpm create vite my-vue-app --template vue@vitejs/plugin-vue<template>{{ message }}</template><script setup>import { ref } from 'vue'; const message = ref('Hello Vue!');</script>
Reactpnpm create vite my-react-app --template react@vitejs/plugin-reactconst [message, setMessage] = useState('Hello React!'); return <div>{message}</div>
Sveltepnpm create vite my-svelte-app --template svelte@sveltejs/vite-plugin-svelte<script>let message = 'Hello Svelte!';</script><h1>{message}</h1>
Preactpnpm create vite my-preact-app --template preact@preact/preset-viteconst [message, setMessage] = useState('Hello Preact!'); return <div>{message}</div>
Litpnpm create vite my-lit-app --template vanilla无需插件class MyComponent extends LitElement { render() { return html\
Hello Lit!` }}`
Vanilla JSpnpm create vite my-vanilla-app --template vanilla无需插件document.getElementById('app').innerHTML = '<h1>Hello Vanilla JS!</h1>';

总结

Vite 支持多种前端框架构建,包括 Vue、React、Svelte、Preact、Lit 等。不同的框架通过简单的配置即可使用 Vite 高效地进行开发。Vite 的插件生态可以很好地支持每种框架,并提供极致的开发体验和快速的热重载能力。在对比中可以看到,使用 Vite 各框架的配置和开发流程都非常简洁高效。

### 解决 IntelliJ IDEA 中 `@Autowired` 注解导致的红色波浪线错误 在使用 Spring 框架时,如果遇到 `@Autowired` 注解下的依赖注入对象显示为红色波浪线错误或者黄色警告的情况,通常是由以下几个原因引起的: #### 1. **Spring 插件未启用** 如果 Spring 支持插件未被激活,则可能导致 IDE 无法识别 `@Autowired` 或其他 Spring 特定的功能。可以通过以下方式解决问题: - 打开设置菜单:`File -> Settings -> Plugins`。 - 确认已安装并启用了名为 “Spring Framework Support” 的官方插件[^1]。 #### 2. **项目配置文件缺失或不正确** Spring 需要通过 XML 文件、Java Config 类或其他形式来定义 Bean 定义。如果没有正确加载这些配置文件,可能会导致 `@Autowired` 报错。 - 确保项目的 `applicationContext.xml` 或者基于 Java 的配置类(带有 `@Configuration` 和 `@Bean` 注解)已被正确定义和引入。 - 对于 Spring Boot 项目,确认是否存在 `spring.factories` 文件以及是否包含了必要的组件扫描路径[^3]。 #### 3. **模块依赖关系问题** 当前模块可能缺少对 Spring Core 或 Context 组件库的有效引用。这可能是由于 Maven/Gradle 构建工具中的依赖项声明不足造成的。 - 检查 `pom.xml` (Maven) 或 `build.gradle` (Gradle),确保包含如下核心依赖之一: ```xml <!-- For Maven --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> ``` ```gradle // For Gradle implementation 'org.springframework:spring-context:${springVersion}' ``` - 更新项目依赖树以应用更改:右键点击项目根目录 -> `Maven -> Reload Project` 或运行命令 `./gradlew build --refresh-dependencies`。 #### 4. **IDE 缓存损坏** Intellij IDEA 的缓存机制有时会因各种因素而失效,从而引发误报错误。清除缓存可以有效缓解此类情况。 - 使用快捷组合键 `Ctrl + Alt + Shift + S` 进入项目结构对话框;也可以尝试执行操作序列:`File -> Invalidate Caches / Restart... -> Invalidate and Restart`. #### 5. **启动异常影响正常解析** 若之前存在类似 `com.intellij.diagnostic.PluginException` 的严重初始化失败日志记录,则表明某些关键服务未能成功加载,进而干扰到后续功能表现[^2]。建议重新下载最新稳定版本的 IDEA 并按照标准流程完成初次部署工作。 ```java // 示例代码片段展示如何正确运用 @Autowired 注解实现自动装配 @Service public class StudentService { private final Repository repository; public StudentService(@Qualifier("specificRepository") Repository repo){ this.repository = repo; } } @Component class SpecificComponent{ @Autowired private transient StudentService studentService; // 此处应无任何编译期告警现象发生 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值