【053】使用vue时Module build failed: TypeError: this._init is not a function错误的解决方法

本文介绍了一个Vue项目中Webpack配置不正确导致的问题及解决方法。主要原因是vue-loader配置错误,未使用完整名称。

错误描述

整个项目目录结构如下:

  • src

    • hello.vue
    • main.js
  • index.html

  • package.json

  • webpack.config.js

package.json

{
  "name": "test-typeof-vue",
  "version": "0.0.1",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "dev": "cross-env NODE_ENV=development webpack-dev-server --port 28080 --open --inline --hot",
    "build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
  },
  "devDependencies": {
    "babel-core": "6.18.2",
    "babel-loader": "6.2.8",
    "babel-preset-es2015": "6.18.0",
    "cross-env": "3.1.3",
    "css-loader": "0.26.0",
    "file-loader": "0.9.0",
    "style-loader": "0.13.1",
    "vue-loader": "9.9.5",
    "webpack": "2.1.0-beta.27",
    "webpack-dev-server": "2.1.0-beta.11"
  },
  "dependencies": {
    "vue": "2.1.3"
  }
}

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8"> <title>bmsclient</title>
  </head>
  <body>
    <div id="app"><hello></hello></div>
    <script src="/dist/build.js"></script>
  </body>
</html>

webpack.config.js

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
          test: /\.css$/,
          use: [
              {
                  loader: 'style-loader'
              }, {
                  loader: 'css-loader'
              }
          ]
      },
      {
        test: /\.vue$/,
        loader: 'vue',
        options: {
          // vue-loader options go here
        }
      },
      {
         test: /\.js$/,
         loader: 'babel-loader',
         exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true
  },
  devtool: '#eval-source-map',
  plugins: [
  ]
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

hello.vue

<template>
  <div>
    <h1>Hello</h1><span>{{zca}}</span>
  </div>
</template>
<script>
export default {
  data:function(){
    return {zca: "zca"}
  },
  mounted: function() { }
}
</script>
<style></style>

main.js

import Vue from "vue/dist/vue.common.js";
import hello from "./hello.vue"
new Vue({
    el: "#app",
    components:{hello:hello}
});

报错如下:

VM219:1Uncaught Error: Module build failed: TypeError: this._init is not a function
    at Object.Vue$2 (F:\workspaceSet\vsc\test-typeof-vue\node_modules\vue\dist\vue.runtime.common.js:3272:8)
    at Object.Vue$2 (F:\workspaceSet\vsc\test-typeof-vue\node_modules\vue\dist\vue.runtime.common.js:3272:8)
    at eval (eval at <anonymous> (http://localhost:28080/dist/build.js:1250:1), <anonymous>:1:7)
    at Object.<anonymous> (http://localhost:28080/dist/build.js:1250:1)
    at __webpack_require__ (http://localhost:28080/dist/build.js:658:30)
    at fn (http://localhost:28080/dist/build.js:84:20)
    at eval (eval at <anonymous> (http://localhost:28080/dist/build.js:949:1), <anonymous>:3:69)
    at Object.<anonymous> (http://localhost:28080/dist/build.js:949:1)
    at __webpack_require__ (http://localhost:28080/dist/build.js:658:30)
    at fn (http://localhost:28080/dist/build.js:84:20)
    at Object.<anonymous> (http://localhost:28080/dist/build.js:1276:18)
    at __webpack_require__ (http://localhost:28080/dist/build.js:658:30)


./src/hello.vue
Module build failed: TypeError: this._init is not a function
    at Object.Vue$2 (F:\workspaceSet\vsc\test-typeof-vue\node_modules\vue\dist\vue.runtime.common.js:3272:8)
 @ ./src/main.js 2:0-32
 @ multi main

解决方法

这个例子中Webpack的版本是2.1.0-beta.27。在这个版本中,loader不能用简写省去 -loader 的形式。因此vue-loader应该使用全拼的形式。
webpack.config.js 文件应该改成这样:

     {
        test: /\.vue$/,
        loader: 'vue-loader', /* 原来的'vue'改成'vue-loader' */
        options: {
          // vue-loader options go here
        }
      },

改完后,系统就正常运行了。

引用了import { UniversalProvider } from '@walletconnect/universal-provider'; import { WalletConnectModal } from '@walletconnect/modal';这两条代码,报错:test1.vue:3 GET http://localhost:5173/unpackage/dist/cache/.vite/deps/@walletconnect_universal-provider.js?t=1757990241347&v=fa29221d net::ERR_ABORTED 504 (Outdated Optimize Dep) main.js:38 [Vue warn]: Unhandled error during execution of async component loader at <AsyncComponentWrapper> at <PageBody> at <Page> at <Anonymous> at <KeepAlive> at <RouterView> at <Layout> at <App> warnHandler @ uni-h5.es.js:16350 callWithErrorHandling @ vue.runtime.esm.js:1381 warn$1 @ vue.runtime.esm.js:1207 logError @ vue.runtime.esm.js:1438 errorHandler @ uni-h5.es.js:16149 callWithErrorHandling @ vue.runtime.esm.js:1381 handleError @ vue.runtime.esm.js:1421 onError @ vue.runtime.esm.js:3724 (anonymous) @ vue.runtime.esm.js:3767 Promise.catch setup @ vue.runtime.esm.js:3766 callWithErrorHandling @ vue.runtime.esm.js:1381 setupStatefulComponent @ vue.runtime.esm.js:8941 setupComponent @ vue.runtime.esm.js:8902 mountComponent @ vue.runtime.esm.js:7218 processComponent @ vue.runtime.esm.js:7184 patch @ vue.runtime.esm.js:6650 mountChildren @ vue.runtime.esm.js:6898 processFragment @ vue.runtime.esm.js:7114 patch @ vue.runtime.esm.js:6624 mountChildren @ vue.runtime.esm.js:6898 processFragment @ vue.runtime.esm.js:7114 patch @ vue.runtime.esm.js:6624 mountChildren @ vue.runtime.esm.js:6898 mountElement @ vue.runtime.esm.js:6805 processElement @ vue.runtime.esm.js:6770 patch @ vue.runtime.esm.js:6638 mountChildren @ vue.runtime.esm.js:6898 mountElement @ vue.runtime.esm.js:6805 processElement @ vue.runtime.esm.js:6770 patch @ vue.runtime.esm.js:6638 mountChildren @ vue.runtime.esm.js:6898 processFragment @ vue.runtime.esm.js:7114 patch @ vue.runtime.esm.js:6624 componentUpdateFn @ vue.runtime.esm.js:7328 run @ vue.runtime.esm.js:153 instance.update @ vue.runtime.esm.js:7453 setupRenderEffect @ vue.runtime.esm.js:7463 mountComponent @ vue.runtime.esm.js:7230 processComponent @ vue.runtime.esm.js:7184 patch @ vue.runtime.esm.js:6650 mountChildren @ vue.runtime.esm.js:6898 mountElement @ vue.runtime.esm.js:6805 processElement @ vue.runtime.esm.js:6770 patch @ vue.runtime.esm.js:6638 componentUpdateFn @ vue.runtime.esm.js:7328 run @ vue.runtime.esm.js:153 instance.update @ vue.runtime.esm.js:7453 setupRenderEffect @ vue.runtime.esm.js:7463 mountComponent @ vue.runtime.esm.js:7230 processComponent @ vue.runtime.esm.js:7184 patch @ vue.runtime.esm.js:6650 componentUpdateFn @ vue.runtime.esm.js:7328 run @ vue.runtime.esm.js:153 instance.update @ vue.runtime.esm.js:7453 setupRenderEffect @ vue.runtime.esm.js:7463 mountComponent @ vue.runtime.esm.js:7230 processComponent @ vue.runtime.esm.js:7184 patch @ vue.runtime.esm.js:6650 componentUpdateFn @ vue.runtime.esm.js:7409 run @ vue.runtime.esm.js:153 instance.update @ vue.runtime.esm.js:7453 updateComponent @ vue.runtime.esm.js:7261 processComponent @ vue.runtime.esm.js:7195 patch @ vue.runtime.esm.js:6650 componentUpdateFn @ vue.runtime.esm.js:7409 run @ vue.runtime.esm.js:153 instance.update @ vue.runtime.esm.js:7453 callWithErrorHandling @ vue.runtime.esm.js:1381 flushJobs @ vue.runtime.esm.js:1585 Promise.then queueFlush @ vue.runtime.esm.js:1494 queueJob @ vue.runtime.esm.js:1488 scheduler @ vue.runtime.esm.js:3179 resetScheduling @ vue.runtime.esm.js:236 triggerEffects @ vue.runtime.esm.js:280 triggerRefValue @ vue.runtime.esm.js:1033 set value @ vue.runtime.esm.js:1078 finalizeNavigation @ vue-router.mjs?v=016578d9:2392 (anonymous) @ vue-router.mjs?v=016578d9:2302 Promise.then pushWithRedirect @ vue-router.mjs?v=016578d9:2270 push @ vue-router.mjs?v=016578d9:2196 install @ vue-router.mjs?v=016578d9:2548 use @ vue.runtime.esm.js:5146 initRouter @ uni-h5.es.js:16261 install @ uni-h5.es.js:16330 use @ vue.runtime.esm.js:5146 (anonymous) @ main.js:38 main.js:38 TypeError: Failed to fetch dynamically imported module: http://localhost:5173/pages/index/test1.vue?t=1757990487294 logError @ vue.runtime.esm.js:1443 errorHandler @ uni-h5.es.js:16149 callWithErrorHandling @ vue.runtime.esm.js:1381 handleError @ vue.runtime.esm.js:1421 onError @ vue.runtime.esm.js:3724 (anonymous) @ vue.runtime.esm.js:3767 Promise.catch setup @ vue.runtime.esm.js:3766 callWithErrorHandling @ vue.runtime.esm.js:1381 setupStatefulComponent @ vue.runtime.esm.js:8941 setupComponent @ vue.runtime.esm.js:8902 mountComponent @ vue.runtime.esm.js:7218 processComponent @ vue.runtime.esm.js:7184 patch @ vue.runtime.esm.js:6650 mountChildren @ vue.runtime.esm.js:6898 processFragment @ vue.runtime.esm.js:7114 patch @ vue.runtime.esm.js:6624 mountChildren @ vue.runtime.esm.js:6898 processFragment @ vue.runtime.esm.js:7114 patch @ vue.runtime.esm.js:6624 mountChildren @ vue.runtime.esm.js:6898 mountElement @ vue.runtime.esm.js:6805 processElement @ vue.runtime.esm.js:6770 patch @ vue.runtime.esm.js:6638 mountChildren @ vue.runtime.esm.js:6898 mountElement @ vue.runtime.esm.js:6805 processElement @ vue.runtime.esm.js:6770 patch @ vue.runtime.esm.js:6638 mountChildren @ vue.runtime.esm.js:6898 processFragment @ vue.runtime.esm.js:7114 patch @ vue.runtime.esm.js:6624 componentUpdateFn @ vue.runtime.esm.js:7328 run @ vue.runtime.esm.js:153 instance.update @ vue.runtime.esm.js:7453 setupRenderEffect @ vue.runtime.esm.js:7463 mountComponent @ vue.runtime.esm.js:7230 processComponent @ vue.runtime.esm.js:7184 patch @ vue.runtime.esm.js:6650 mountChildren @ vue.runtime.esm.js:6898 mountElement @ vue.runtime.esm.js:6805 processElement @ vue.runtime.esm.js:6770 patch @ vue.runtime.esm.js:6638 componentUpdateFn @ vue.runtime.esm.js:7328 run @ vue.runtime.esm.js:153 instance.update @ vue.runtime.esm.js:7453 setupRenderEffect @ vue.runtime.esm.js:7463 mountComponent @ vue.runtime.esm.js:7230 processComponent @ vue.runtime.esm.js:7184 patch @ vue.runtime.esm.js:6650 componentUpdateFn @ vue.runtime.esm.js:7328 run @ vue.runtime.esm.js:153 instance.update @ vue.runtime.esm.js:7453 setupRenderEffect @ vue.runtime.esm.js:7463 mountComponent @ vue.runtime.esm.js:7230 processComponent @ vue.runtime.esm.js:7184 patch @ vue.runtime.esm.js:6650 componentUpdateFn @ vue.runtime.esm.js:7409 run @ vue.runtime.esm.js:153 instance.update @ vue.runtime.esm.js:7453 updateComponent @ vue.runtime.esm.js:7261 processComponent @ vue.runtime.esm.js:7195 patch @ vue.runtime.esm.js:6650 componentUpdateFn @ vue.runtime.esm.js:7409 run @ vue.runtime.esm.js:153 instance.update @ vue.runtime.esm.js:7453 callWithErrorHandling @ vue.runtime.esm.js:1381 flushJobs @ vue.runtime.esm.js:1585 Promise.then queueFlush @ vue.runtime.esm.js:1494 queueJob @ vue.runtime.esm.js:1488 scheduler @ vue.runtime.esm.js:3179 resetScheduling @ vue.runtime.esm.js:236 triggerEffects @ vue.runtime.esm.js:280 triggerRefValue @ vue.runtime.esm.js:1033 set value @ vue.runtime.esm.js:1078 finalizeNavigation @ vue-router.mjs?v=016578d9:2392 (anonymous) @ vue-router.mjs?v=016578d9:2302 Promise.then pushWithRedirect @ vue-router.mjs?v=016578d9:2270 push @ vue-router.mjs?v=016578d9:2196 install @ vue-router.mjs?v=016578d9:2548 use @ vue.runtime.esm.js:5146 initRouter @ uni-h5.es.js:16261 install @ uni-h5.es.js:16330 use @ vue.runtime.esm.js:5146 (anonymous) @ main.js:38 test1.vue:4 GET http://localhost:5173/unpackage/dist/cache/.vite/deps/@walletconnect_modal.js?t=1757990241347&v=4486af5e net::ERR_ABORTED 504 (Outdated Optimize Dep)
最新发布
09-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值