javascript - Vue 中的 "Failed to mount component: template or render function not defined",尽管使用完整的编译器包

标签 javascript webpack vue.js vuejs2 vue-loader

我正在尝试开发一个可重用的 Vue 组件。它设置为使用 webpack 进行构建,然后演示页面将完整的 Vue 编译器和已编译的包加载到页面上。然而,在加载页面时,尽管包含了 Vue 编译器,我还是收到了以下错误。

[Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <Cwl>
       <Root>

我本以为 vue-loader 会设置 cwl 组件的 template 字段,并且我本以为我添加到我的 Vue 版本页面将包括编译器。我在这里缺少什么?

<小时/>

有问题的代码可以在这里看到GitHub URL 。只需克隆存储库,运行 webpack,使用 http-server 启动服务器,然后浏览到 http://localhost:8080/demo/ .

作为引用,我的演示 HTML 页面如下所示:

<!DOCTYPE html>
<html lang="en">
<body>
<div id="vue" class="container container-fluid">
    <cwl
            cwl-url="https://rawgit.com/rabix/cwl-svg/master/cwl-samples/fastqc.json"
    ></cwl>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.13/dist/vue.js"></script>
<script src="../dist/index.js"></script>
<script>
    Vue.config.devtools = true;
    Vue.config.debug = true;
    new Vue({
        el: '#vue',
        components: {
            cwl: vue_cwl
        }
    })
</script>
</body>
</html>

我的 webpack 配置如下所示:

const path = require("path");

module.exports = {
    devtool: "source-map",

    // Read files from js/src
    entry: './cwl.vue',

    // Output everything into the static folder
    output: {
        libraryTarget: "umd",
        path: path.resolve("dist/"),
        filename: "index.js",
        library: 'vue_cwl'
    },

    externals: {
        vue: 'vue'
    },

    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader',
            },
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [require('babel-preset-env')]
                    }
                },

            },
            {
                enforce: "pre",
                test: /\.ts?$/,
                exclude: ["node_modules"],
                use: {
                    loader: "awesome-typescript-loader",
                    options: {
                        useBabel: true
                    }
                }
            },
            {test: /\.css$/, loaders: ["style-loader", "css-loader"]},
            {
                test: /\.scss$/,
                use: [{
                    loader: "style-loader" // creates style nodes from JS strings
                }, {
                    loader: "css-loader" // translates CSS into CommonJS
                }, {
                    loader: "sass-loader" // compiles Sass to CSS
                }]
            }
        ]
    },
    resolve: {
        extensions: [".ts", ".js", ".vue"],
        alias: {
            'vue$': 'vue/dist/vue.esm.js'
        },
    }
};

最佳答案

如果您console.log(vue_cwl)你可以看到有一个default包含实际 Vue 组件的属性。更改您的代码以使用 default属性作为组件:

components: {
    cwl: vue_cwl.default
}

来自my answer to a similar question :

A Vue component is usually exported with export default { /* ... */} so it facilitates the default import like import Component from './component.vue'(ES6 syntax)

When using require() (CommonJS) you have to specify you are requiring the default export

在您的情况下,您通过 <script> 直接包含该组件但这个概念仍然存在。

关于javascript - Vue 中的 "Failed to mount component: template or render function not defined",尽管使用完整的编译器包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48849554/

相关文章:

javascript - 在 Javascript 中切换一个简单的导航

Javascript 函数仅在页面刷新后有效

vue.js - Vue + webpack,字体文件转base64

javascript - 参数未通过 Vuejs 上的路由器链接传递

javascript - 对此行为 : ES6 Promise that is never resolved 的解释是什么

javascript - 如何在mean.io应用程序中进行调试?

cdn - 如何让 webpack 为 publicPath 配置使用多个 url

javascript - 别名导入不适用于 .jsx 扩展

javascript - Vue.js.数据字段不具有约束力

vue.js - 访问 WSL2 Vue.js 服务器 (localhost :8080) exposing NATted IP number externally