Vue3+TypeScript学习笔记(十二)

简介: 本节记录异步组件&代码分包相关的知识
  1. 当页面需要请求大量资源时,同步组件会一次性加载所有资源后才能展示,从而造成网页首屏加载时间特别长。但是我们可以利用异步组件让浏览器仅在需要时才加载对应资源,从而达到优化性能的目的。
  2. 要让一个组件成为异步组件可以使用ES7的顶层await技术——即不通过async函数,直接在script标签中写上await,后面跟一个返回promise对象的函数调用语句,此时整个组件就会成为异步组件。

Card.vue(异步组件顶层await)

<template>
  <div>
    <div class="icon">
      <img src="../assets/102439408_p1_master1200.jpg" alt="我是一张图片" />
      <span>{{ result.name }}</span>
    </div>
    <hr />
    <div class="text">{{ result.text }}</div>
  </div>
</template>

<script setup lang="ts">
  import { ref, reactive } from 'vue'
  import { axios } from '../utils/axios'

  interface Data {
    name: string
    text: string
    url: string
  }
  // 从ES7开始可以在顶层使用await,这样整个组件会变成异步组件
  const result = await axios.get<Data>('/api/list')
  console.log(result.url)
</script>

<style scoped>
  .icon {
    display: flex;
    align-items: center;
  }
  .icon img {
    height: 100px;
    border-radius: 50%;
  }
  .icon span {
    font-size: 18px;
    letter-spacing: 2px;
    margin-left: 10px;
  }
  .text {
    font-size: 20px;
  }
</style>
  1. 异步组件不能用传统的import……from……导入,必须通过调用defineAsyncComponent函数来定义。该函数接收一个回调函数,其返回值是import()动态导入语句(本质上也是一个promise对象),此外该组件必须写在Suspense标签内部。Suspense标签拥有两个具名插槽default和fallback。default用于加载异步组件,fallback用于异步组件加载完成之前临时展示(骨架屏)

App.vue(骨架屏)

<template>
    <Suspense>
        <template #default>
      // 异步组件,展示需要加载大量资源的内容
            <Card></Card>
        </template>
        <template #fallback>
          // 同步组件,展示骨架屏
            <Sync></Sync>
        </template>
    </Suspense>
</template>

<script setup lang="ts">
import { ref, reactive, defineAsyncComponent } from 'vue'
import Sync from './components/Sync.vue';

const Card = defineAsyncComponent(() => import('./components/Card.vue'))
</script>

<style scoped></style>
  1. defineAsyncComponent有两种写法,第一种是直接写回调函数,第二种是传入一个对象,对象中有loadingComponent、errorComponent和timeout等属性,可以基于不同状态展示不同组件。
  2. 代码分包:使用异步组件后在pnpm run build时就不会将所有资源打包到一个JS文件中,当用户访问网页时仅在需要时才加载对应的资源,起到性能优化的作用

完整代码:
App.vue

<template>
    <Suspense>
        <template #default>
            <Card></Card>
        </template>
        <template #fallback>
            <Sync></Sync>
        </template>
    </Suspense>
</template>

<script setup lang="ts">
import { ref, reactive, defineAsyncComponent } from 'vue'
import Sync from './components/Sync.vue';

const Card = defineAsyncComponent(() => import('./components/Card.vue'))
</script>

<style scoped></style>

Card.vue

<template>
    <div>
        <div class="icon">
            <img src="../assets/102439408_p1_master1200.jpg" alt="我是一张图片" />
            <span>{{ result.name }}</span>
        </div>
        <hr />
        <div class="text">{{ result.text }}</div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { axios } from '../utils/axios'

interface Data {
    name: string
    text: string
    url: string
}
// 从ES7开始可以在顶层使用await,这样整个组件会变成异步组件
const result = await axios.get<Data>('/api/list')
console.log(result.url)
</script>

<style scoped>
.icon {
    display: flex;
    align-items: center;
}
.icon img {
    height: 100px;
    border-radius: 50%;
}
.icon span {
    font-size: 18px;
    letter-spacing: 2px;
    margin-left: 10px;
}
.text {
    font-size: 20px;
}
</style>

Sync.vue

<template>
    <div>
        <div class="icon">
            <img src="" alt="我是一张图片" />
            <div></div>
        </div>
        <hr />
        <div class="text">
            <div></div>
            <div></div>
        </div>
    </div>
</template>

<script setup lang='ts'>
import { ref,reactive } from 'vue'

</script>

<style scoped>
.icon {
    display: flex;
    align-items: center;
}
.icon img {
    height: 100px;
    border-radius: 50%;
}
.icon div {
    width: 400px;
    height: 20px;
    margin-left: 10px;
    background-color: rgb(61, 58, 58);
}
.text {
    height: 30px;
    font-size: 20px;
}
.text div{
    height: 20px;
    margin-top: 20px;
    background-color: rgb(61, 58, 58);
}
</style>

axios.ts(基于底层Ajax)

export const axios = {
    get<T>(url: string): Promise<T> {
        return new Promise((resolve, reject) => {
            const xhr = new XMLHttpRequest()
            xhr.open('GET', url)
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    setTimeout(() => {
                        resolve(JSON.parse(xhr.responseText))
                    },2000)
                }
            }
            xhr.send(null)
        })
    },
}

express框架(简单的后端)

import express, { Express, Router } from 'express'
import path from 'path'

const app: Express = express()

const router: Router = express.Router()

app.use('/api', router)

router.get('/list', (req, res) => {
    res.json({
        name: '诺航同学',
        text: '负责本群服务器运维',
        url: './assets/102439408_p1_master1200.jpg',
    })
})

app.listen('5555', () => {
    console.log('server success http://localhost:5555')
})
相关文章
|
1天前
|
JavaScript 安全 前端开发
Vue 3 中的 TypeScript
【6月更文挑战第15天】
8 6
|
24天前
|
JavaScript 前端开发
TypeScript 学习笔记(六):TypeScript 与前端框架的结合应用
笔记,进一步提升 TypeScript 编程技能。
23 1
|
1月前
|
JavaScript 前端开发 编译器
TypeScript 学习笔记
TypeScript 学习笔记
|
1月前
|
JavaScript 前端开发 开发者
类型检查:结合TypeScript和Vue进行开发
【4月更文挑战第24天】TypeScript是JavaScript超集,提供类型注解等特性,提升代码质量和可维护性。Vue.js是一款高效前端框架,两者结合优化开发体验。本文指导如何配置和使用TypeScript与Vue:安装TypeScript和Vue CLI,创建Vue项目时选择TypeScript支持,配置`tsconfig.json`,编写`.tsx`组件,最后运行和构建项目。这种结合有助于错误检查和提升开发效率。
|
1月前
|
JavaScript 前端开发 开发者
Vue工具和生态系统: Vue.js和TypeScript可以一起使用吗?
【4月更文挑战第18天】Vue.js与TypeScript兼容,官方文档支持在Vue项目中集成TypeScript。TypeScript作为JavaScript超集,提供静态类型检查和面向对象编程,增强代码准确性和健壮性。使用TypeScript能提前发现潜在错误,提升代码可读性,支持接口和泛型,使数据结构和函数更灵活。然而,不是所有Vue插件都兼容TypeScript,可能需额外配置。推荐尝试在Vue项目中使用TypeScript以提升项目质量。
24 0
|
1月前
|
JavaScript 前端开发 数据可视化
前端开发使用 Vue 3 + TypeScript + Pinia + Element Plus + ECharts
前端开发使用 Vue 3 + TypeScript + Pinia + Element Plus + ECharts
60 0
|
1月前
|
JavaScript 前端开发
在Vue中使用TypeScript的常见问题有哪些?
在Vue中使用TypeScript的常见问题有哪些?
43 2
|
1月前
|
JavaScript 前端开发
在Vue中使用TypeScript的优缺点是什么?
在Vue中使用TypeScript的优缺点是什么?
32 0
|
1月前
|
JavaScript
在 Vue 中如何使用 TypeScript?
在 Vue 中如何使用 TypeScript?
18 0
|
1月前
|
JavaScript 安全 容器
Vue3 + setup + TypeScript: 构建现代、类型安全的Vue应用的关键技巧总结
当使用 setup 的时候,组件直接引入就可以了,不需要再自己手动注册