简体   繁体   English

Vue.component() 带加载器的动态导入

[英]Vue.component() dynamic import with loader

Is there a way to import dynamically a component with a Loader default if it's not loaded?如果未加载,有没有办法动态导入具有 Loader 默认值的组件?

We use something like this right now,我们现在使用这样的东西,

import Dashboard from '../components/dashboard';


Vue.component('dashboard', Dashboard);

I tried something like this as the documentation say link to the docs我尝试过这样的事情,因为文档说链接到文档

import Loader from '../components/loader';
const Dashboard = () => ({
  component: import('../components/dashboard'),
  loading: Loader,
  delay: 500, //to test
})

Vue.component('dashboard', Dashboard);

Either way it doesn't seems to work无论哪种方式,它似乎都不起作用

Your code should already work.您的代码应该已经可以工作了。 It might seem to not work because the loading happens instantaneously.它似乎不起作用,因为加载是瞬间发生的。

If you artificially delay the return of import('../components/dashboard') , you'll notice the loader displayed:如果你人为地延迟import('../components/dashboard')的返回,你会注意到加载器显示:

const delayed = async (promise) => {
  const result = await promise
  await new Promise(r => setTimeout(r, 2000))
  return result
}

Vue.component('Dashboard', () => ({
  component: delayed(import('./components/Dashboard.vue')),
  loading: Loader,
  delay: 500
}))

demo 演示

Realistically, the dynamic component is intended for components that might take a significant amount of time to load (eg, from a remote URL).实际上,动态组件旨在用于可能需要大量时间来加载(例如,从远程 URL)的组件。

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

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