vue项目实战-1初始化项目

本文介绍如何使用Vite和Vue初始化项目,包括安装依赖、配置路由、设置文件别名等步骤,并演示如何引入Element Plus和Windicss。
  • 项目初始化 ,npm 7+,需要额外的双短横线

 npm  init vite@latest <project-name> -- --template vue
  • 引入elementPlus,在项目内打开命令行

npm install element-plus --save

  如果你对打包后的文件大小不是很在乎,那么使用完整导入会更方便。

// main.ts
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'

const app = createApp(App)

app.use(ElementPlus)
app.mount('#app')
  •  引入Windi CSS ,在项目内打开命令行

npm i -D vite-plugin-windicss windicss

然后,在你的 Vite 配置中添加插件:

vite.config.js

import WindiCSS from 'vite-plugin-windicss'

export default {
  plugins: [
    WindiCSS(),
  ],
}

最后,在你的 Vite 入口文件中导入 virtual:windi.css

main.js

import 'virtual:windi.css'

 使用  组件用class = ".btn"   在stytle中 .btn{@apply bg-purple-500 text-indigo-50 }

  • 引入vue router,在项目内打开命令行

npm install vue-router@4

新建router文件夹,新建index.js文件

import { 
    createRouter,
    createWebHashHistory
 } from "vue-router";

 const routes= []

 const router = createRouter({
    history:createWebHashHistory(),
    routes
 })

 export default router

在main.js中使用

// 创建并挂载根实例
const app = Vue.createApp({})
//确保 _use_ 路由实例使
//整个应用支持路由。
app.use(router)

app.mount('#app')

 在vite.config.js中设置文件夹别名,使用 ~ 代替 src

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import WindiCSS from 'vite-plugin-windicss'
import path from  'path'
// https://vitejs.dev/config/
export default defineConfig({
  resolve:{
    alias:{
      "~":path.resolve(__dirname,"src")
    }
  },
  plugins: [vue(),WindiCSS()]
})

然后新建pages文件夹,在pages中新建页面index.vue,about.vue,404.vue ,到routes/index.js配置路由

import Index from "~/pages/index.vue"
 import About from "~/pages/about.vue"
 import NotFound from "~/pages/404.vue"

 const routes= [{ path: '/',  component: Index},
                { path: '/about', component: About},
                { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound }]

 const router = createRouter({
    history:createWebHashHistory(),
    routes
 })

项目完成初始化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值