我正在尝试使用webpack来转译.vue文件的Vue项目的绝对最小示例。如何使用vue-loader而不使用vue-cli
我的目标是详细了解每个构建步骤。大多数教程建议使用vue-cli
并使用webpack-simple
配置。尽管这个设置起作用,但为了我的简单目的,这看起来过分了。现在我不想让babel,linting或者带有热模块重载的live 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装载机到的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!")
}
})
Th Ë装载机似乎没有拿起.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组件?
你能添加错误? – imcvampire
你究竟在哪里试图导入'.vue'文件?是的,请分享您遇到的错误。 – thanksd
我已经编辑了这个问题,当我尝试从“导入”代码。vue文件未找到此代码。 – Kokodoko