【发布时间】:2018-03-11 15:24:45
【问题描述】:
我正在尝试将使用 webpack 转换 .vue 文件的 Vue 项目的绝对最小示例放在一起。
我的目标是详细了解每个构建步骤。大多数教程建议使用vue-cli 并使用webpack-simple 配置。尽管该设置有效,但对于我的简单目的来说似乎有点过分了。现在我不想要 babel、linting 或带有热模块重新加载的实时 Web 服务器。
仅使用import Vue from 'vue' 的最小示例有效! Webpack 将 vue 库和我自己的代码编译成一个包。
但是现在,我想将 vue-loader 添加到 webpack 配置中,以便 .vue 文件将被转译。我已经安装了 vue 加载器:
npm install vue-loader
npm install css-loader
npm install vue-template-compiler
我已经将 vue-loader 添加到 webpack 配置中:
var path = require('path')
module.exports = {
entry: './dev/app.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
}
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
}
};
我已经创建了 hello.vue
<template>
<p>{{greeting}} World</p>
</template>
<script>
export default {
data:function(){
return {
greeting:"Hi there"
}
}
}
</script>
在我的应用程序中,我导入“你好”
import Vue from 'vue'
import hello from "./hello.vue";
new Vue({
el: '#app',
template:`<div><hello></hello></div>`,
created: function () {
console.log("Hey, a vue app!")
}
})
加载器似乎没有拾取.vue 文件,我收到错误:
Module not found: Error: Can't resolve './hello.js'
编辑
尝试import hello from 'hello.vue' 时出现错误:
Unknown custom element: <hello> - did you register the component correctly?
我错过了一步吗?我是否以正确的方式导入 .vue 组件?如何使用 app.js 中的 hello.vue 组件?
【问题讨论】:
-
可以添加错误吗?
-
您实际上是在哪里尝试导入
.vue文件?是的,请分享您遇到的错误。 -
我已经编辑了问题,当我尝试从 .vue 文件中
import代码时,找不到此代码。 -
是的,坚持使用 vue-cli(在他们的文档中)很烦人。例如,我使用 laravel-mix 编译 .vue 和一个快速简单的 webpack API。