是一个内置组件,用来在组件树中协调对异步依赖的处理。它让我们可以在组件树上层等待下层的多个嵌套异步依赖项解析完成,并可以在等待时渲染一个加载状态。

我们可以看到官网并不推荐我们使用它,目前仍处于测试中。
react懒加载组件(如下):
- import { Component, lazy, Suspense } from "react";
-
- const Childs = lazy(() => import("./Childs"));
-
- export default class Parent extends Component {
- render() {
- return (
- <>
- <h2>我是父组件h2>
- <Suspense fallback={<div>正在加载中....div>}>
- <Childs />
- Suspense>
- >
- );
- }
- }
vue3
SuspenseView路由组件
- <h2>Suspenseh2>
- <Suspense>
- <template v-slot:default>
-
- <AsyncComponent />
- template>
- <template v-slot:fallback>
-
- <div>正在加载中...div>
- template>
- Suspense>
-
- <script setup>
- import { defineAsyncComponent } from "vue"
- // 引入异步组件
- const AsyncComponent = defineAsyncComponent(() => import("../components/AsyncComponent.vue"))
- script>
-
- <style lang="scss" scoped>style>
AsyncComponent组件
- <div style="color: red;">
- {{ param }}
- div>
-
- <script setup>
- const asyncFunc = () => {
- return new Promise((resolve, reject) => {
- setTimeout(() => {
- resolve("各位好啊 异步组件闪亮登场(●ˇ∀ˇ●)")
- }, 1000)
- })
- }
-
- const param = await asyncFunc()
- script>
-
- <style lang="scss" scoped>
-
- style>
suspense测试
