Vue 3 + TypeScript + Vite 新建项目,开发及打包踩过的坑

1,292 阅读1分钟

1.创建项目

执行 npm init @vitejs.app
配置项目名称,直接回车这是默认项目名称

image.png

默认选择的是vanlilla,通过上下建控制选择vue==》回车

image.png

 选择vue-ts

image.png

这样项目就创建完成了

image.png

2.使用vscode 开发注意

如果配置了vetur  会提示 vscode报错 xxxx is declared but its value is never read Vetur

image.png

需要关闭 关闭这个提示,在工作区(当然你也可以选择用户)关闭Validate js/ts in <scrript> ps:将这个勾取消即可

image.png

3.运行项目注意

2.运行项目后发现,如果提示 “Network: use `--host` to expose“,并且通过网络IP无法访问服务 需要在
vite.config.ts 中的defineConfig 添加配置

    server: {			
    host: '0.0.0.0'	
      }	 
2.打包后直接打开dist中的index.html 会显示空白文件 vite.config.ts 中的defineConfig 添加配置
同时需要仪服务的方式访问index.html ps:可以简单使用http-server

base: './',

vite.config.ts 文件

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

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

最后就大功告成