简体   繁体   English

为什么我不能在 Vue Router 4 的路由中使用异步组件?

[英]Why can't I use Async Components in routes with Vue Router 4?

The documentation at https://next.router.vuejs.org/guide/advanced/lazy-loading.html says: "Do not use Async components for routes." https://next.router.vuejs.org/guide/advanced/lazy-loading.html的文档说:“不要将异步组件用于路由。” Why not?为什么不? I want to do that and I have been doing that in the past.我想这样做,而且我过去一直这样做。

It actually worked with Vue 3.0.11 and Vue Router 4.0.6, but it does not work with Vue 3.2.9 and Vue Router 4.0.11.它实际上适用于 Vue 3.0.11 和 Vue Router 4.0.6,但不适用于 Vue 3.2.9 和 Vue Router 4.0.11。 I used to be able to pass props through the router view like this: <router-view:device="device"></router-view> but not anymore.我曾经能够像这样通过路由器视图传递道具: <router-view:device="device"></router-view> 但现在不行了。 What could be a work-around?什么可以解决?

By Do not use Async components Router documentation says do not use Vue 3 async components as described in Vue 3 documentation通过不要使用Async components路由器文档说不要使用 Vue 3 异步组件,如 Vue 3 文档中所述

Do not:不要:

import { defineAsyncComponent  } from "vue"

const UserDetails = defineAsyncComponent (() => import('./views/UserDetails'))

const router = createRouter({
  // ...
  routes: [{ path: '/users/:id', component: UserDetails }],
})

Do: (...well as you did before Vue 3)做:(......就像你在 Vue 3 之前所做的那样)

const UserDetails = () => import('./views/UserDetails')

const router = createRouter({
  // ...
  routes: [{ path: '/users/:id', component: UserDetails }],
})

More info in Migration Guide 迁移指南中的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM