【问题标题】:Webpack cannot resolve module errorWebpack 无法解析模块错误
【发布时间】:2016-09-26 09:44:19
【问题描述】:

所以我正在尝试编译一个vue 文件。 但我收到以下错误

我不知道为什么。我的main.js 文件包含以下内容

// Vue things
import Vue from 'vue'
import VueResource from 'vue-resource'
Vue.use(VueResource)


// Vue Components
import dashboard from 'components/dashboard-component'

new Vue({
    el: "body",
    components: {
        dashboard
    },
    methods: {
    }
})

我的组件文件夹与main.js 在同一个文件夹中。 这是我的 Vue 组件:

<template>
Hello
</template>

<script>
// Exports
export default {
    components: {
        dashboard
    },
    props: {
    },
    data: function (argument) {
    },
    ready() {
    },
    methods: {
    }
}
</script>

在 components/dashboard-component 下为index.vue。

我做错了什么?

谢谢。

【问题讨论】:

    标签: webpack vue.js


    【解决方案1】:

    为方便起见,我通常会在已解析的扩展数组中添加.vue,并为经常使用的目录设置别名:

    resolve: {
      extensions: ['', '.js', '.vue'],
      fallback: [path.join(__dirname, '../node_modules')],
      alias: {
        'data': path.resolve(__dirname, '../data'),
        'src': path.resolve(__dirname, '../src'),
        'assets': path.resolve(__dirname, '../src/assets'),
        'views': path.resolve(__dirname, '../src/views'),
        'components': path.resolve(__dirname, '../src/components')
      }
    },
    

    所以我可以在我的项目中使用import dashboard from 'components/dashboard-component';。

    【讨论】:

      【解决方案2】:

      您需要导入 Vue 文件。所以你需要将你的组件保存到components/Dashboard/Dashboard.vue 并像这样导入它:

      import Dashboard from './components/Dashboard/Dashboard.vue'
      
      new Vue({
          el: "body",
          components: {
              Dashboard
          },
          methods: {
          }
      })
      

      不要忘记components 文件夹前面的./ 才能正常工作

      【讨论】:

      • 现在它显示了以下内容:ERROR in ./view/vue/components/dashboard/dashboard.vue. Module build faild: TypeError: this._init is not a function 而我在dashboard.vue 中没有名为_init 的函数
      • 尝试安装以下依赖项:npm install --save vue vue-loader vue-html-loader css-loader style-loader babel-loader babel-core babel-plugin-transform-runtime babel-preset-es2015 babel-runtime vue-hot-reload-api这是github问题中报告的错误:github.com/vuejs/vue-loader/issues/23
      猜你喜欢
      • 1970-01-01
      • 2017-03-22
      • 2017-07-10
      • 2017-02-10
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 2021-07-21
      • 2020-03-18
      相关资源
      最近更新 更多