如何在不使用vue-cli的情况下使用vue-loader

vue.js Vue.js

Harry伽罗

2020-03-12

我试图将使用webpack转换.vue文件的Vue项目的绝对最小示例放在一起。

我的目标是详细了解每个构建步骤。大多数教程建议使用vue-cli和使用webpack-simple配置。尽管该设置有效,但出于我的简单目的,这似乎有些过分。现在,我不希望babel,linting或具有热模块重新加载功能的实时Web服务器。

一个最小的例子就是import Vue from 'vue'行得通!Webpack将vue库和我自己的代码编译为一个捆绑包。

但是现在,我想将vue-loader添加到webpack配置中,以便.vue文件可以被编译。我已经安装了vue loader:

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>

然后在我的应用程序中导入“ hello”

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组件?

第1366篇《如何在不使用vue-cli的情况下使用vue-loader》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

0个回答

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android