> vue3的异步组件用法较vue2有所区别:
```
//异步组件
import { defineAsyncComponent } from 'vue'
// 不带选项的异步组件
let HelloWorld = defineAsyncComponent(() => import('./components/HelloWorld.vue'))
// 带选项的异步组件
const HelloWorldOption = defineAsyncComponent({
loader: () => import('./components/HelloWorld.vue'),
delay: 200,
timeout: 3000,
errorComponent: ErrorComponent, 异步加载组件错误时加载该组件
loadingComponent: LoadingComponent 异步加载组件加载中时加载该组件
})
export default {
components: { HelloWorld },
setup() {
let itemRefs = []
const list = ref(['1', '2', '3']);
//方法
const setItemRef = el => {
if (el) {
itemRefs.push(el)
}
}
onMounted(()=>{
console.log(itemRefs[0])
})
//在模版更新之前
onBeforeUpdate(() => {
itemRefs = []
})
//模版更新
onUpdated(() => {
console.log(itemRefs)
})
//输出方法和变量给模版使用
return {
setItemRef,
list
}
}
}
</script>
```