gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:25:27 30 4
gpt4 key购买 nike

我正在尝试开发一个可重用的 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com