搭建vite项目

文章目录

      • [Vite 是一个基于 Webpack 的开发服务器,用于开发 Vue 3 和 Vite 应用程序](#Vite 是一个基于 Webpack 的开发服务器,用于开发 Vue 3 和 Vite 应用程序)
  • 一、创建一个vite项目
  • [二、集成Vue Router](#二、集成Vue Router)
    • [1.安装 vue-router@next插件](#1.安装 vue-router@next插件)
    • [2.在 src 目录下创建一个名为 router 的文件夹,并在其中创建一个名为 index.js 的文件。在这个文件中,我们将设置路由](#2.在 src 目录下创建一个名为 router 的文件夹,并在其中创建一个名为 index.js 的文件。在这个文件中,我们将设置路由)
    • [3. 在 src/main.js 中,导入并使用刚刚创建的路由](#3. 在 src/main.js 中,导入并使用刚刚创建的路由)
    • [4. 在 src/App.vue 中,添加一个 router-view 组件来显示当前路由的内容](#4. 在 src/App.vue 中,添加一个 router-view 组件来显示当前路由的内容)
  • 总结

Vite 是一个基于 Webpack 的开发服务器,用于开发 Vue 3 和 Vite 应用程序

一、创建一个vite项目

Vite 需要 Node.js 版本 18+,20+。然而,有些模板需要依赖更高的 Node 版本才能正常运行,当你的包管理器发出警告时,请注意升级你的 Node 版本。

npm create vite@latest

yarn create vite

yarn create vite my-vue-app --template vue

...

页面结构

↓↓↓

二、集成Vue Router

1.安装 vue-router@next插件

npm install vue-router@next

yarn add vue-router@next

2.在 src 目录下创建一个名为 router 的文件夹,并在其中创建一个名为 index.js 的文件。在这个文件中,我们将设置路由

js 复制代码
// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../view/home/index.vue'
import About from '../view/about/index.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: About
  }
]

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

export default router

3. 在 src/main.js 中,导入并使用刚刚创建的路由

js 复制代码
// src/main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

const app = createApp(App)
app.use(router)
app.mount('#app')

4. 在 src/App.vue 中,添加一个 router-view 组件来显示当前路由的内容

js 复制代码
<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>
//-------------------
//vite.config.js  配置一下config
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  base: './',
  server: {
    host: '0.0.0.0'
  },
  plugins: [vue()],
})

...


总结

相关推荐
我叫蒙奇12 小时前
JS中的隐式类型转换
前端
他们叫我秃子12 小时前
前端开发转 Go 全栈(四):代码写在前面,却要最后执行?我终于搞懂了 defer
前端·后端·go
生戎马 平安京策12 小时前
只学一点点:我的技术学习策略
前端·javascript·react.js
大鹏说大话12 小时前
HTML5 地理定位 Geolocation:获取用户位置的“红线”与最佳实践
前端·html·html5
shawxlee12 小时前
vue3+elementplus+sass/scss安装配置教程:使用svg图标、引入外部字体、定义变量模块、修改组件样式、一键切换主题等
vue·sass·svg·字体·element·scss·主题样式
Csvn13 小时前
content-visibility: auto —— 让浏览器跳过离屏渲染的性能黑科技
前端
小鹰信息技术服务部13 小时前
Edge安装包MicrosoftEdgeSetup.exe无法运行,点击没反应
前端·edge
webkubor14 小时前
一次前端生产白屏复盘:旧 HTML、Hash 资源和 MIME type text/html
前端·前端工程化
咩咩啃树皮14 小时前
第63篇【终极整合巨作】从零手写原生中后台管理系统|整合62篇全部前端知识点|从头到尾完整架构+全功能逻辑
前端·架构
Highcharts15 小时前
用Highcharts如何动态向一个序列添加点-示列讲解
前端