【发布时间】:2018-08-16 05:54:55
【问题描述】:
我有一个 VueJS 2 应用程序,我试图根据配置 JSON 文件中的不同标志加载特定组件。
我的文件结构如下:
root
config.json
> src
>> client
>>> components
>>>> dashboard
>>>>> MenuBarBrand.vue
> product
>> product01
>>> client
>>>> components
>>>>> branding
>>>>>> MenuBrand.vue
>> product02
>>> client
>>>> components
>>>>> branding
>>>>>> MenuBrand.vue
目前我尝试了两种不同的方法:
在我的 MenuBarBrand 组件中使用 webpack 导入语法:
<template>
<b-navbar-brand href="/dashboard">
<MenuBrand />
</b-navbar-brand>
</template>
<script>
import config from '../../../../config.json';
const MenuBrand = import(`../../../../product/${config.product}/components/branding/MenuBrand`);
export default {
name: 'MenuBarBrand',
components: {
'MenuBrand',
},
}
</script>
或 Vue Async Components 本地注册语法:
<template>
<b-navbar-brand href="/dashboard">
<MenuBrand />
</b-navbar-brand>
</template>
<script>
import config from '../../../../config.json';
export default {
name: 'MenuBarBrand',
components: {
'MenuBrand': () => import(`../../../../product/${config.product}/components/branding/MenuBrand`)
},
}
</script>
我是否有任何明显的错误,或者是否需要任何特定的 babel/webpack 插件来启用此功能?
编辑:根据我收到的评论添加我的加载器配置:
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
scss: [
'vue-style-loader',
'css-loader',
'sass-loader',
],
sass: [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax',
],
},
},
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]',
},
},
{
test: /\.css$/,
loader: 'style-loader!css-loader',
},
{
test: /\.(ttf|otf|eot|woff(2)?)(\?[a-z0-9]+)?$/,
loader: 'file-loader?name=fonts/[name].[ext]',
},
],
},
【问题讨论】:
-
"但目前这两个选项似乎都不起作用。"这没有帮助。请描述预期的行为。
-
预期的行为是,无论哪种情况,导入都会正常工作,并允许我将 Vue 组件加载到应用程序中。相反,我遇到了错误(我将附加到原始问题中)。
标签: javascript webpack vuejs2 vue-component es6-promise