使用vite创建vite3 项目以及遇到的一些问题

Vite是一种新型前端构建工具,能够显著提升前端开发体验。

------------------------开发------------------------------

1、快速创建vue3+ts的项目

npm init vite@latest vue3-project-demo --template vue-ts

2、引入sass

npm add -D sass

3、设置环境变量

1、创建.env环境变量文件,见vite文档
2、在环境变量文件中定义的变量,可以在 env.d.ts 增加智能提示

/// <reference types="vite/client" />
interface ImportMetaEnv {
  readonly VITE_APP_TITLE: string
  // 更多环境变量...
}
interface ImportMeta {
  readonly env: ImportMetaEnv
}

3、设置多环境运行:

  "scripts": {
    "dev": "vite serve --mode development",
    "test": "vite serve --mode uat",
    "build:dev": "vite build --mode development",
    "build:test": "vite build --mode uat",
    "build:prod": "vite build --mode production",
    ...
}

------------------------问题------------------------------

1、项目创建成功后,使用命令npm run dev看到终端显示,Network没有显示局域网ip

  vite v2.9.13 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 291ms.

根据网上查询的资料,有以下几种解决方法:

  • 通过修改vite.config.ts 方法解决
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  server: {
    host: '0.0.0.0'        // 添加server,设置host
  }
})
  • 通过Vite CLI配置
    执行 npx vite --host 0.0.0.0命令后,就可以通过局域网ip访问了
  • 修改npm脚本
    在根目录下package.json 文件中修改scriptes节点下的脚本:
  "scripts": {
    "dev": "vite --host 0.0.0.0",        // 在此添加host
    "build": "vue-tsc --noEmit && vite build",
    "preview": "vite preview"
  },
  ....
  ....
}

修改上面任何一种配置,可解决network 为空的问题。

2、项目中引入组件HelloWorld有一个红色波浪号提示

截屏2022-07-05 下午5.33.01.png
造成的原因:

因为vue3不支持vutur

解决方案:

方案1:在vscode 中搜索vetur插件,禁用之后,重启就可以。


截屏2022-07-05 下午5.44.09.png

方案2:在根目录 env.d.ts中,添加如下代码也可以解决

declare module "*.vue" {
  import { App, defineComponent } from "vue";
  const component: ReturnType<typeof defineComponent> & {
    install(app: App): void;
  };
  export default component;
}

3、element-plus 按需导入后,使用elmessage、elloading提示红色下划线。

1673574701383.png
解决方案:

在根目录新建一个 element-plus.d.ts 文件,保证后缀名是.d.ts就可以了

export {}
declare global {
  const ElMessage:typeof import('element-plus')['ElMessage'] 
  const ElLoading:typeof import('element-plus')['ElLoading'] 
}

在tsconfig.js 中引入element-plus.d.ts

"include": ["element-plus.d.ts"],

4、vue3 运行报错:Uncaught ReferenceError: globalThis is not defined

原因:

globalThis旨在通过定义一个标准的全局属性来整合日益分散的访问全局对象的方法。该提案目前处于第四阶段,这意味着它已经准备好被纳入ES2020标准。所有流行的浏览器,包括Chrome 71+、Firefox 65+和Safari 12.1+,都已经支持这项功能。你也可以在Node.js 12+中使用它。

解决方案:

方案1:
可以通过升级node版本和浏览器版本解决,node> 12,chrome版本>71。

方案2:
在index.html中

<script>
  this.globalThis || (this.globalThis = this);
</script>

5、引入router 报错:变量“routes”在某些无法确定其类型的位置处隐式具有类型“any[]”。

解决方案:

引入RouteRecordRaw,充当路由数据类型,ts中数据类型是Array<T>,T就是数据类型。

import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'

const routes:Array<RouteRecordRaw> = [
   {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/layout/baselayout.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/:path(.*)',
    name: '404',
    component: () => import('@/views/notFound/index.vue')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

export default router

6、引入element-plus

执行命令:

npm add element-plus

如果添加命令报错:
ERROR TypeError: Cannot read properties of undefined (reading 'replace')
表明element-plus没有删除干净
执行命令:

npm uninstall element-plus

在main.ts中删除应用element-plus的代码

7、按需引入element-plus 组件

按需引入element-plus
参考官方文档

npm install -D unplugin-vue-components unplugin-auto-import

Vite

// vite.config.ts
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig({
  // ...
  plugins: [
    // ...
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
})

引入图标库

npm install @element-plus/icons-vue

main.ts

// 如果您正在使用CDN引入,请删除下面一行。
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 226,488评论 6 524
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 97,466评论 3 411
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 174,084评论 0 371
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,024评论 1 305
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 70,882评论 6 405
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,395评论 1 318
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,539评论 3 433
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,670评论 0 282
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,194评论 1 329
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,173评论 3 352
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,302评论 1 362
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,872评论 5 354
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,581评论 3 342
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,984评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,179评论 1 278
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 50,888评论 3 385
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,306评论 2 369

推荐阅读更多精彩内容