Vite建立Vue3專案
Vite 需要 Node.js 版本 >= 12.0.0。 (
node -v
看看自己目前的node版本)
#使用yarn:
yarn create @vitejs/app
#使用npm:
npm init @vitejs/app
1. 輸入專案名稱
這裡輸入我們的專案名稱:vite-vue3
#2. 選擇框架
這裡選擇我們需要整合的框架:vue
vanilla:原生js,沒有任何框架整合
vue:vue3框架,只支援vue3
react:react框架
#preact:輕量化react框架
lit -element:輕量級web元件
svelte:svelte框架
3. 選擇不同的vue
這裡我們選擇:vue
4.專案建立完成
5.專案結構
專案結構非常簡單:
6. 啟動專案
#進入專案:
cd vite-vue3
安裝依賴:
npm install
#執行專案:
npm run dev
#或npx vite
# 專案:
npm run build
或npx vite build
#啟動速度極快:
Vue3中使用jsx
Vite創建的Vue3專案中是無法直接使用jsx 的,需要引入插件來實現
1. 安裝外掛程式
使用yarn:
yarn add @vitejs /plugin-vue-jsx -D
使用npm:
npm i @vitejs/plugin-vue-jsx -D
#2. 註冊外掛程式
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 src="/static/imghwm/default1.png" data-src="./assets/logo.png" class="lazy" alt="Vue logo" /> <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 src="/static/imghwm/default1.png" data-src="/src/main.js" class="lazy" 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 src="/static/imghwm/default1.png" data-src="/src/main.js" class="lazy" 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在儲存時,做eslint校驗
與Webpack不同,Vite的編譯入口不是Javascript文件,而是以index.html作為編譯入口。在index.html中,透過載入main.js,這時請求到達了vite的serve層
以上是Vite建立Vue3專案及Vue3使用jsx的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Vue.js適合小型到中型項目,而React更適用於大型、複雜應用。 1.Vue.js的響應式系統通過依賴追踪自動更新DOM,易於管理數據變化。 2.React採用單向數據流,數據從父組件流向子組件,提供明確的數據流向和易於調試的結構。

Vue.js適合中小型項目和快速迭代,React適用於大型複雜應用。 1)Vue.js易於上手,適用於團隊經驗不足或項目規模較小的情況。 2)React的生態系統更豐富,適合有高性能需求和復雜功能需求的項目。

實現 Vue 中 a 標籤跳轉的方法包括:HTML 模板中使用 a 標籤指定 href 屬性。使用 Vue 路由的 router-link 組件。使用 JavaScript 的 this.$router.push() 方法。可通過 query 參數傳遞參數,並在 router 選項中配置路由以進行動態跳轉。

Vue 中實現組件跳轉有以下方法:使用 router-link 和 <router-view> 組件進行超鏈接跳轉,指定 :to 屬性為目標路徑。直接使用 <router-view> 組件顯示當前路由渲染的組件。使用 router.push() 和 router.replace() 方法進行程序化導航,前者保存歷史記錄,後者替換當前路由不留記錄。

Vue 中 div 元素跳轉的方法有兩種:使用 Vue Router,添加 router-link 組件。添加 @click 事件監聽器,調用 this.$router.push() 方法跳轉。

Vue.js提供了三種跳轉方式:原生 JavaScript API:使用 window.location.href 進行跳轉。 Vue Router:使用 <router-link> 標籤或 this.$router.push() 方法進行跳轉。 VueX:通過 dispatch action 或 commit mutation 來觸發路由跳轉。

在 Vue 中設置頁面跳轉有多種方法,包括:使用 router-link 組件創建可點擊鏈接。使用 router.push() 方法手動添加新路由到歷史記錄堆棧。使用 router.replace() 方法替換當前路由。直接使用 location.href 重定向到新頁面。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

記事本++7.3.1
好用且免費的程式碼編輯器

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3漢化版
中文版,非常好用