Vue 3 + Vite + TS笔记整理

文章详细介绍了Vue从2到3的主要升级点,包括性能提升、响应式系统的改变、组合式API的引入,以及Vite的使用方法,如创建项目、配置别名和路由。同时,还讨论了TypeScript的基础概念及其在项目中的应用,以及如何进行编译和监控。文章还展示了如何封装路由初始化以提高代码组织性。
摘要由CSDN通过智能技术生成

Vue3

Vue2 到 Vue3的变化

  • 性能优化
  • 打包体积减少
  • 渲染速度提高
  • 更好的拥抱了typescript
  • 源码升级使用proxy代理实现响应式
  • 添加组合式API
  • 添加新的组件 Suspense Fragment Teleport等等
  • v-if 和 v-for优先级更改,源码中循序不同
  • 重构了虚拟dom

Vite 创建

npm init vite

npm run preview 启动提供预览的服务器(预览的是打包的效果)

配置项目路径别名

tsconfig.json

"baseUrl": "./",
"paths": {
    "@/*": ["src/*"],
    "#/*": ["types/*"]
}

vite.config.ts

要安装 @type/node

npm install -D @type/node

配置

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import * as path from "path";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      "@": path.join(__dirname, "src"),
      "#": path.join(__dirname, "types"),
    },
  },
});

配置路由

router.ts

import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

const routes:RouteRecordRaw[] = [
    {
        path:'/home',
        name:'home',
        component:() => import('../pages/home/home.vue')
    }
]

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

export default router

main.ts

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './routers'

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

封装路由初始化

router.ts

import { App } from "vue";
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

const routes: RouteRecordRaw[] = [
  {
    path: "/home",
    name: "home",
    component: () => import("../pages/home/home.vue"),
  },
];

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

// 封装路由初始化
export const initRouter = (app: App<Element>) => {
  app.use(router);
};

// export default router

main.ts

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import {initRouter} from './routers'

const app = createApp(App)
initRouter(app)
app.mount('#app')

TypeScript

简介

Typescript 是 JS 的超集,主要学习 ts 里面的原始类型、字面量类型、数组类型、函数类型、类类型、接口类型、类型别名,联合与交叉类型、枚举类型、泛型等类型元素,以及类型推断、类型断言、类型缩小、类型放大等特性。

相比JS编程,编写TS更加严谨

tsc初始化

tsc init

编译当前项目文件夹的ts文件到指定的目录下并开启监视,

tsc -p tsconfig.json --watch
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Morgan_Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值