Vite創(chuàng)建Vue3項(xiàng)目及Vue3使用jsx詳解
Vite創(chuàng)建Vue3項(xiàng)目
Vite 需要 Node.js 版本 >= 12.0.0。(
node -v
查看自己當(dāng)前的node版本)
- 使用 yarn:
yarn create @vitejs/app
- 使用 npm:
npm init @vitejs/app
1. 輸入項(xiàng)目名稱(chēng)
這里輸入我們的項(xiàng)目名稱(chēng):vite-vue3
2. 選擇框架
這里選擇我們需要集成的框架:vue
- vanilla:原生js,沒(méi)有任何框架集成
- vue:vue3框架,只支持vue3
- react:react框架
- preact:輕量化react框架
- lit-element:輕量級(jí)web組件
- svelte:svelte框架
3. 選擇不同的vue
這里我們選擇:vue
4. 項(xiàng)目創(chuàng)建完成
5. 項(xiàng)目結(jié)構(gòu)
項(xiàng)目結(jié)構(gòu)非常簡(jiǎn)單:
6. 啟動(dòng)項(xiàng)目
- 進(jìn)入項(xiàng)目:
cd vite-vue3
- 安裝依賴(lài):
npm install
- 運(yùn)行項(xiàng)目:
npm run dev
或npx vite
- 編譯項(xiàng)目:
npm run build
或npx vite build
啟動(dòng)速度極快:
Vue3中使用jsx
Vite創(chuàng)建的Vue3項(xiàng)目中是無(wú)法直接使用jsx 的,需要引入插件來(lái)實(shí)現(xiàn)
1. 安裝插件
- 使用 yarn:
yarn add @vitejs/plugin-vue-jsx -D
- 使用 npm:
npm i @vitejs/plugin-vue-jsx -D
2. 注冊(cè)插件
vite.config.js 中:
import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import vueJsx from "@vitejs/plugin-vue-jsx"; // https://vitejs.dev/config/ export default defineConfig({ plugins: [vue(), vueJsx()] })
3. 使用插件
方法一:修改App.vue
不使用 jsx,App.vue
是這樣:
<script setup> import HelloWorld from './components/HelloWorld.vue'; </script> <template> <img alt="Vue logo" src="./assets/logo.png" /> <HelloWorld msg="Hello Vue 3 + Vite" /> </template>
使用 jsx,App.vue是這樣:
<script lang="jsx"> import { defineComponent } from 'vue'; import HelloWorld from './components/HelloWorld.vue'; import logo from './assets/logo.png'; export default defineComponent({ render: () => ( <div> <img alt="Vue logo" src={logo} /> <HelloWorld msg="Hello Vue 3 + Vite" /> </div> ), }); </script>
方法二:刪除App.vue,新建App.jsx
新建App.jsx文件
import { defineComponent } from 'vue'; import HelloWorld from './components/HelloWorld.vue'; import logo from './assets/logo.png'; export default defineComponent({ setup () { return () => { return ( <div> <img alt="Vue logo" src={logo} /> <HelloWorld msg="Hello Vue 3 + Vite" /> </div> ) } } });
再修改main.js的引入
import App from './App.vue' 改為 import App from './App'
import { createApp } from 'vue' import App from './App' createApp(App).mount('#app')
注意
- 不支持eslint在保存時(shí),做eslint校驗(yàn)
- vite的編譯入口不同于webpack,不是js文件,而是以index.html為編譯入口。在index.html中,通過(guò)<script type="module" src="/src/main.js"></script>加載main.js,這時(shí)請(qǐng)求到達(dá)了vite的serve層
總結(jié)
到此這篇關(guān)于Vite創(chuàng)建Vue3項(xiàng)目及Vue3使用jsx的文章就介紹到這了,更多相關(guān)Vite創(chuàng)建Vue3項(xiàng)目?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 如何用Vite構(gòu)建工具快速創(chuàng)建Vue項(xiàng)目
- 使用vite創(chuàng)建vue3項(xiàng)目的詳細(xì)圖文教程
- vue3的介紹和兩種創(chuàng)建方式詳解(cli和vite)
- 利用vite創(chuàng)建vue3項(xiàng)目的全過(guò)程及一個(gè)小BUG詳解
- Vue+vite創(chuàng)建項(xiàng)目關(guān)于vite.config.js文件的配置方法
- 詳解如何創(chuàng)建基于vite的vue項(xiàng)目
- Vue+Vite項(xiàng)目初建(axios+Unocss+iconify)的實(shí)現(xiàn)
相關(guān)文章
100行代碼實(shí)現(xiàn)vue表單校驗(yàn)功能(小白自編)
這篇文章主要介紹了使用100行代碼實(shí)現(xiàn)vue表單校驗(yàn)功能,本文通過(guò)實(shí)例截圖給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11詳解Vue demo實(shí)現(xiàn)商品列表的展示
這篇文章主要介紹了Vue demo實(shí)現(xiàn)商品列表的展示,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05vue使用showdown并實(shí)現(xiàn)代碼區(qū)域高亮的示例代碼
這篇文章主要介紹了vue使用showdown并實(shí)現(xiàn)代碼區(qū)域高亮的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10vue + webpack如何繞過(guò)QQ音樂(lè)接口對(duì)host的驗(yàn)證詳解
這篇文章主要給大家介紹了關(guān)于利用vue + webpack如何繞過(guò)QQ音樂(lè)接口對(duì)host的驗(yàn)證的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07使用Vue CLI創(chuàng)建typescript項(xiàng)目的方法
這篇文章主要介紹了使用Vue CLI創(chuàng)建typescript項(xiàng)目的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Vue3中注冊(cè)全局的組件,并在TS中添加全局組件提示方式
這篇文章主要介紹了Vue3中注冊(cè)全局的組件,并在TS中添加全局組件提示方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07el-select如何獲取當(dāng)前選中的對(duì)象所有(item)數(shù)據(jù)
在開(kāi)發(fā)業(yè)務(wù)場(chǎng)景中我們通常遇到一些奇怪的需求,下面這篇文章主要給大家介紹了關(guān)于el-select如何獲取當(dāng)前選中的對(duì)象所有(item)數(shù)據(jù)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11