vite+vue3.0+ts+element-plus快速搭建项目的实现

 更新时间:2021年06月24日 09:38:15   作者:火滴雪叶  
本文将结合实例代码,介绍vite+vue3.0+ts+element-plus快速搭建项目的实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

vite 出了 2.x 版本,抱着学一学的心态决定出个简单的项目,结合 element-plus,以及将会成为每位前端必会的 typescript,实现了如下内容。

vite是一个由原生 ESM 驱动的 Web 开发构建工具。在开发环境下基于浏览器原生 ES imports 开发,在生产环境下基于 Rollup 打包。

vite 作用

  • 快速的冷启动:不需要等待打包操作;
  • 即时的热模块更新:替换性能和模块数量的解耦让更新飞起;
  • 真正的按需编译:不再等待整个应用编译完成,这是一个巨大的改变。

使用的环境

  • node v12.19.0
  • @vue/cli 4.5.8

搭建项目

npm init vite-app <project-name>
cd <project-name>
npm install
npm run dev


yarn create vite-app <project-name>
cd <project-name>
yarn
yarn dev

配置

vite.config.ts

vite.config.ts 相当于 @vue-cli 项目中的 vue.config.js

import path from "path";

const pathResolve = (pathStr: string) => {
  return path.resolve(__dirname, pathStr);
}

const config = {
  base: './',//在生产中服务时的基本公共路径。@default '/'
  alias: {
    '/@/': pathResolve('./src'),
  },
  outDir: 'vite-init',//构建输出将放在其中。会在构建之前删除旧目录。@default 'dist'
  minify: 'esbuild',//构建时的压缩方式
  hostname: 'localhost',//本地启动的服务地址
  port: '8800',//服务端口号
  open: false,//启动服务时是否在浏览器打开
  https: false,//是否开启https
  ssr: false,//是否服务端渲染
  optimizeDeps: {// 引入第三方的配置
    include: ['axios']
  },
  // proxy: {//代理配置
  //   '/api': {
  //     target: 'http://xx.xx.xx.xx:xxxx',
  //     changeOrigin: true,
  //     ws: true,
  //     rewrite: (path: string) => { path.replace(/^\/api/, '') }
  //   }
  // }
}
module.exports = config;

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5",//指定ECMAScript的目标版本:'ES3'(默认),'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020',或'ESNEXT'。
    "module": "commonjs",//指定模块代码生成:'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020',或'ESNext'。
    "strict": true,//是否启用所有严格的类型检查选项。
    "baseUrl":"./",//用于解析非绝对模块名称的基本目录。
    "paths": {
      "/@/*": ["./src/*"]
    },  
    "noImplicitAny": true, //对于隐含有'any'类型的表达式和声明引发错误。
    "esModuleInterop": true, //通过为所有导入创建名称空间对象,实现CommonJS和ES模块之间的互操作性。意味着“allowSyntheticDefaultImports”。
    "experimentalDecorators": true, //支持对ES7装饰器的实验性支持。
    "skipLibCheck": true, //跳过声明文件的类型检查。
    "forceConsistentCasingInFileNames": true //禁止对同一文件使用大小写不一致的引用。
  }
}

App.vue

修改app.vue

<template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <router-view />
</template>

<script>
export default {
  name: 'App',
  setup() {

  }
}
</script>

Views

在 src 下新建 views文件夹,并在文件夹内创建 index.vue

<template>
  <HelloWorld :msg="msg"></HelloWorld>
</template>

<script lang="ts">
import HelloWorld from "/@/views/HelloWorld.vue";
import { defineComponent } from "vue";
export default defineComponent({
  name: "Home",
  components: { HelloWorld },
  setup() {
    return {
      msg: "hello World",
    };
  },
});
</script>

PS:在引入.vue文件时一定要带上后缀名,否则会报找不到文件

在views文件夹内创建 HelloWorld.vue

<template>
  <h1>{{ msg }}</h1>
  <button @click="realTime.count++">count is: {{ realTime.count }}</button>
  <button @click="handleclick">点击跳转其它路由</button>
  <p>Edit <code>components/HelloWorld.vue</code> to test hot module replacement.</p>
</template>

<script>
import { defineComponent, reactive } from "vue";
import { useRouter } from 'vue-router'
export default defineComponent({
  name: 'Index',
  props: { msg: { type: String, default: '欢迎来到vue3' } },
  setup(props) {
    const router = useRouter();
    let realTime = reactive({ count: 0 });

    function handleclick() {
      router.push({ path: '/other' })
    }
    return {
      msg: props.msg,
      realTime,
      handleclick
    }
  }
})
</script>

router

在 src 下新建 router 文件夹,并在文件夹内创建 index.ts

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

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    component: () => import("/@/views/index.vue")
  },
]

export default createRouter({
  history: createWebHistory(),
  routes
})

main.ts

把原本的main.js改为main.ts,修改后别忘记把index.html里面也改为main.ts

import { createApp } from 'vue'
import router from './router/index'
import App from './App.vue'
import ElementPlus from 'element-plus'

import 'element-plus/lib/theme-chalk/index.css'
import './reset.css'

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

细心的同学这时可能已经发现:在 ts 文件中引入 .vue 文件时出现以下报错,但是不影响代码正常运行

报错原因:typescript 只能理解 .ts 文件,无法理解 .vue文件

解决方法:在项目根目录或 src 文件夹下创建一个后缀为 .d.ts 的文件,并写入以下内容:

declare module '*.vue' { }
declare module '*.js'
declare module '*.json';
declare module '*.svg'
declare module '*.png'
declare module '*.jpg'
declare module '*.jpeg'
declare module '*.gif'
declare module '*.bmp'
declare module '*.tiff'

至此项目搭建完成,可以愉快的写代码了。

到此这篇关于vite+vue3.0+ts+element-plus快速搭建项目的实现的文章就介绍到这了,更多相关vite+vue3.0+ts+element-plus搭建内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 解决Vue axios post请求,后台获取不到数据的问题方法

    解决Vue axios post请求,后台获取不到数据的问题方法

    今天小编就为大家分享一篇解决Vue axios post请求,后台获取不到数据的问题方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Vue的状态管理vuex使用方法详解

    Vue的状态管理vuex使用方法详解

    由于Vue多个状态分散的跨越在许多组件和交互间各个角落,大型应用复杂度也经常逐渐增长。为了解决这个问题,Vue提供了vuex。本文将详细介绍Vue状态管理vuex,需要的朋友可以参考下
    2020-02-02
  • Element table 上下移需求的实现

    Element table 上下移需求的实现

    本文主要介绍了Element table 上下移需求的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • vue2封装input组件方式(输入的双向绑定)

    vue2封装input组件方式(输入的双向绑定)

    这篇文章主要介绍了vue2封装input组件方式(输入的双向绑定),具有很好的参考价值,希望对大家有所帮助。
    2023-04-04
  • 移动端Vue2.x Picker的全局调用实现

    移动端Vue2.x Picker的全局调用实现

    这篇文章主要介绍了移动端Vue2.x Picker的全局调用实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Vue配合iView实现省市二级联动的示例代码

    Vue配合iView实现省市二级联动的示例代码

    本篇文章主要介绍了Vue配合iView实现省市二级联动的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • 使用vue实现grid-layout功能实例代码

    使用vue实现grid-layout功能实例代码

    这篇文章主要介绍了使用vue实现grid-layout功能的代码讲解,需要的朋友可以参考下
    2018-01-01
  • Vue不能下载xls以及文件乱码问题解决

    Vue不能下载xls以及文件乱码问题解决

    最近工作中遇到了一些问题,通过查找相关资料终于找到了相关的解决方法,这篇文章主要给大家介绍了关于Vue不能下载xls以及文件乱码问题解决的相关资料,需要的朋友可以参考下
    2022-04-04
  • Vue自定义指令学习及应用详解

    Vue自定义指令学习及应用详解

    这篇文章主要为大家详细介绍了Vue中自定义指令的学习以及如何利用Vue制作一个简单的学生信息管理系统,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-05-05
  • 一文详解Vue3中简单diff算法的实现

    一文详解Vue3中简单diff算法的实现

    这篇文章主要为大家详细介绍Vue3中简单diff算法的实现与使用,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的可以了解一下
    2022-09-09

最新评论