一文教會你搭建vite項目并配置路由和element-plus
1.創(chuàng)建項目
npm init vite@latest m-component -- --template vue-ts
2.安裝vite
npm i
3.啟動項目
npm run dev
4.可在vite.config.ts文件下修改端口號,默認為3030,我們可以改成習慣用的8080
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
server:{
port:8080
}
})
5.安裝路由router和element-plus
npm i -S vue-router@next element-plus
可在package.json中查看下載的路由和element-plus配置信息
6.在src目錄下新建views和router文件夾,然后在router目錄下新建index.ts文件,在index.ts文件下配置路由
import { createRouter,createWebHistory,RouteRecordRaw } from "vue-router";
import Home from "../views/Home.vue"
const routes:RouteRecordRaw[] = [
{
path:'/',
component:Home
}
]
const router = createRouter({
routes,
history:createWebHistory()
})
export default router
在views目錄下新建一個Home.vue文件
<template>
<div>
首頁
</div>
</template>
<script lang="ts" setup>
</script>
<style lang="scss" scoped>
</style>
6.然后在main.ts中引入
import { createApp } from 'vue'
import App from './App.vue'
import router from './router/index'
const app =createApp(App)
app.use(router)
app.mount('#app')
7.使用element-plus
在main.ts中引入使用
import { createApp } from 'vue'
import App from './App.vue'
import router from './router/index'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
const app =createApp(App)
app.use(router).use(ElementPlus)
app.mount('#app')
在Home.vue中
<template>
<div>
<el-button>按鈕</el-button>
</div>
</template>
<script lang="ts" setup>
</script>
<style lang="scss" scoped>
</style>
8.在App.vue中編寫樣式
<template>
<router-view></router-view>
</template>
<style>
*{
margin: 0;
padding: 0;
}
</style>
這里使用的scss,需要先安裝sass 和sass-loader(*這是css的預處理器)
npm i -D sass sass-loader
附:vite引入element-plus修改主題色報錯

原因:引入文件路徑不對
解決:~改成node_modules/,安裝scss --dev,然后引入時去掉.scss/.css,完美運行
$--color-primary: #62c28c; /* 改變 icon 字體路徑變量,必需 */ $--font-path: "node_modules/element-plus/lib/theme-chalk/fonts"; @import "node_modules/element-plus/packages/theme-chalk/src/index";
總結
到此這篇關于搭建vite項目并配置路由和element-plus的文章就介紹到這了,更多相關vite搭建并配置路由element-plus內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解vuex中action何時完成以及如何正確調用dispatch的思考
這篇文章主要介紹了詳解vuex中action何時完成以及如何正確調用dispatch的思考,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
Vue局部組件數(shù)據(jù)共享Vue.observable()的使用
隨著組件的細化,就會遇到多組件狀態(tài)共享的情況,今天我們介紹的是 vue.js 2.6 新增加的 Observable API ,通過使用這個 api 我們可以應對一些簡單的跨組件數(shù)據(jù)狀態(tài)共享的情況,感興趣的可以了解一下2021-06-06
van-uploader保存文件到后端回顯后端接口返回的數(shù)據(jù)
前端開發(fā)想省時間就是要找框架呀,下面這篇文章主要給大家介紹了關于van-uploader保存文件到后端回顯后端接口返回的數(shù)據(jù),文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-06-06
vue checkbox 全選 數(shù)據(jù)的綁定及獲取和計算方法
下面小編就為大家分享一篇vue checkbox 全選 數(shù)據(jù)的綁定及獲取和計算方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02

