Javascript JS和Vue.JS中的动态导入
我正在使用Vue.js框架编写一个web应用程序。需要使用动态导入。您需要使用名称为的参数进行导入,然后定义生成的组件 js中以下动态导入的语法:Javascript JS和Vue.JS中的动态导入,javascript,vue.js,import,async-await,Javascript,Vue.js,Import,Async Await,我正在使用Vue.js框架编写一个web应用程序。需要使用动态导入。您需要使用名称为的参数进行导入,然后定义生成的组件 js中以下动态导入的语法: async () => { const module_path = 'module_path'; const module = await import(module_path) module.default(); } 因此,只有在异步函数中才能使用动态导入的库。我需要在其他地方使用导入的组件: <templ
async () => {
const module_path = 'module_path';
const module = await import(module_path)
module.default();
}
因此,只有在异步函数中才能使用动态导入的库。我需要在其他地方使用导入的组件:
<template>
<div>
<!-- imported component definition -->
<component v-bind:is="my_component"></component>
</div>
</template>
<script lang="coffee">
# Here you need to dynamically import the component, so that you
# can define it later on line 3.
# How to make a static import is clear (shown below). But I need a
# dynamic one.
import my_component from "./component_title.Vue"
export default {
props: () -> {
# The name of the component to be imported.
# Transferred from another component.
component_title: {type: String, default: null}
}
components: {
# The component is declared
my_component: my_component
}
}
</script>
#在这里,您需要动态导入组件,以便
#可以稍后在第3行定义它。
#如何进行静态导入很清楚(如下所示)。但我需要一个
#动态的。
从“/component\u title.Vue”导入我的组件
导出默认值{
道具:()->{
#要导入的组件的名称。
#从另一个组件转移。
组件标题:{type:String,默认值:null}
}
组成部分:{
#组件已声明
my_组件:my_组件
}
}
是否可以在此任务中实现动态导入?是。只需在组件上使用import()
:
components: {
my_component: () => import(path)
}