• vite的搭建与使用


    实际开发中编写的代码往往是不能被浏览器直接识别的,比如ES6,TypeScript,Vue文件等。所以此时我们必须通过构建工具来对代码进行转换,编译,类似的工具有webpack,rollup,parcel.但是随着项目越来越大,需要处理的javascript呈指数级增长,模块越来越多。构建工具需要很长时间才能开启服务器,HMR也需要几秒钟才能在浏览器反应过来。所以出现了vite。

    提示:vite仅支持vue3.0+的项目,也即是说我们无法在其中使用vue2.x

    1.安装:

    1. npm init vite-app //项目名字
    2. cd 项目名字 //进入项目
    3. npm i //安装依赖
    4. npm run dev //打开项目

    2.在vite项目中使用TypeScript

    vite完全可以支持Typescript,不需要任何配置,只需要直接引入ts即可。

    1. <script lang = "ts">
    2. const abc: number = 123456789; //定义一个abc类型是数字,为什么这么定义可以去看一下Typescript的数据类型
    3. console.log(abc, "abc");
    4. </script>

    3.vite项目使用less sass scss

    安装   less:npm install less less-loader -D

    安装  sass:npm install sass node-sass sass-loader -D

     安装好之后在<style lang="less" scoped></style>标签上面直接就可以用

     4.vite打包

    npm run build

    4.下面就来创建一个标准的项目

    安装路由:npm install vue-router@4(这里我是指定安装的版本)

    在src文件夹下面建一个router的文件夹 里面放一个index.ts的路由文件,内容如下:

    1. import { createRouter, createWebHashHistory } from 'vue-router'
    2. const routes = [
    3. {
    4. path: '/',
    5. name: 'Home',
    6. //如果没有在.d.ts文件中定义,在这里引入路径时加后缀名.vue是会报错的
    7. component: () => import("../pages/home/index.vue"),
    8. children: [
    9. {
    10. path: '/news',
    11. name: 'Hews',
    12. component: () => import("../pages/news/index.vue")
    13. }
    14. ]
    15. },
    16. ]
    17. const router = createRouter({
    18. history: createWebHashHistory(),
    19. routes,
    20. })
    21. export default router;

    App.vue文件内容如下:

    1. <template>
    2. <router-view />
    3. </template>
    4. <script>
    5. import { defineComponent, onMounted } from "vue";
    6. export default defineComponent({
    7. name: "App",
    8. });
    9. </script>

    在src文件夹下面建一个后缀名为.d.ts的文件夹,内容如下:

    1. declare module "*.vue" {
    2. import { ComponentOptions } from "vue";
    3. const componentOptions: ComponentOptions;
    4. export default componentOptions;
    5. }
    6. declare module "*.svg";
    7. declare module "*.png";
    8. declare module "*.jpg";
    9. declare module "*.jpeg";
    10. declare module "*.gif";
    11. declare module "*.bmp";
    12. declare module "*.tiff";
    13. declare module "lodash";
    14. declare module "@/api/*";

    两个文件夹内容大致一样,在这里就只说一个home,

    home文件夹下面的index.vue里的内容如下:

    1. <template>
    2. <div>
    3. <h1>我是home页面</h1>
    4. </div>
    5. </template>
    6. <script lang="tsx"></script>
    7. <style lang="less" src="./index.less" scoped></style>

     home文件夹下面的index.tsx里的内容如下:

    1. import { defineComponent } from "vue";
    2. export default defineComponent({
    3. name: "Home",
    4. })

  • 相关阅读:
    【Python自动化Excel】pandas操作Excel的“分分合合”
    【Leetcode】1206. Design Skiplist
    MySQl数据库————DQL数据查询语言
    Android12之DRM架构(一)
    【RISC-V】 li指令
    最新 umi4-max 如何使用 webpack5 联邦模块
    如何让chatGPT给出高质量的回答?
    大数据平台架构及规划
    mysql5.6 修改密码
    Linux下挂载大于2T的硬盘
  • 原文地址:https://blog.csdn.net/weixin_58431406/article/details/125426364