【发布时间】:2018-05-08 21:35:51
【问题描述】:
我尝试导入文件夹的所有组件,并根据传递的道具显示其中一个。
我使用 webpack 和 vue-loader 来导入我的所有组件。每个组件都是一个 *.vue 文件。
问题是通过导入存储在子文件夹中的一些组件,我在运行时收到此错误:
[Vue warn]: Failed to mount component: template or render function not defined.
found in
---> <Test2>
<VoneDocs> at src\components\VoneDocs.vue
<App> at src\App.vue
<Root>
经过研究和@craig_h 的帮助,我发现问题出在我导入文件的方式上:
<template>
<transition name="fade">
<div class="vone-docs" v-if="docName !== undefined">
<component :is="docName"/>
</div>
</transition>
</template>
<script>
import Test from '../assets/docs/Test';
// import all docs (*.vue files) in '../assets/docs'
let docsContext = require.context('../assets/docs', false, /\.vue$/);
let docsData = {}; // docsData is {...<filenames>: <components data>}
let docsNames = {};
let docsComponents = {};
docsContext.keys().forEach(function (key) {
docsData[key] = docsContext(key); // contains [{<filename>: <component data>}]
docsNames[key] = key.replace(/^\.\/(.+)\.vue$/, '$1'); // contains [{<filename>: <component name>}]
docsComponents[docsNames[key]] = docsData[key]; // contains [{<component name>: <component data>}]
});
export default {
name: 'vone-docs',
props: ['page'],
components: {
...docsComponents,
Test
},
computed: {
docName () {
return this.page;
},
docFileName () {
return './' + this.docName + '.vue';
},
docData () {
return docsData[this.docFileName];
}
},
beforeRouteUpdate (to, from, next) {
if (to.path === from.path) {
location.hash = to.hash;
} else next();
},
mounted () {
console.log(docsComponents);
}
};
</script>
虽然我的Test 组件在docName 为'test' 时成功显示(因为它是直接导入的),但每隔一个使用require.context() 导入的Vue 单文件组件就会导致错误:Failed to mount component: template or render function not defined.
我的require.context() 有什么错误吗?
这是我的 webpack 配置(除了使用 raw-loader 和 html-loader,和 Vue webpack-template 的一样)。
// webpack.base.conf.js
'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')
function resolve (dir) {
return path.join(__dirname, '..', dir)
}
module.exports = {
context: path.resolve(__dirname, '../'),
entry: {
app: './src/main.js'
},
output: {
path: config.build.assetsRoot,
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'production'
? config.build.assetsPublicPath
: config.dev.assetsPublicPath
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
}
},
module: {
rules: [
...(config.dev.useEslint? [{
test: /\.(js|vue)$/,
loader: 'eslint-loader',
enforce: 'pre',
include: [resolve('src'), resolve('test')],
options: {
formatter: require('eslint-friendly-formatter'),
emitWarning: !config.dev.showEslintErrorsInOverlay
}
}] : []),
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test')]
},
{
test: /\.(png|jpe?g|gif)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
// Art SVG are loaded as strings. Must be placed in the html with `v-html` directive.
{
test: /\.raw\.svg$/,
loader: 'raw-loader'
},
// Icon SVG are loaded as files like regular images.
{
test: /\.icon\.svg$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
},
{
test: /\.(html)$/,
use: {
loader: 'html-loader',
options: {
attrs: [':data-src', 'img:src']
}
}
}
]
}
}
感谢您的帮助!
【问题讨论】:
-
你的主 vue 实例是什么样的?你定义了一个渲染函数吗?
-
我编辑了帖子! ;) 我只使用不需要渲染函数的 *.vue 文件(据我所知),以及我的主 Vue 实例中的字符串模板。
标签: javascript webpack import vue.js vuejs2