[英]"TypeError: chart_js__WEBPACK_IMPORTED_MODULE_9__.default is not a constructor"
[英]*.default is not a constructor, with a imported js plugin
我嘗試創建一個簡單的表單驗證,並通過示例項目中的yarn鏈接注冊這個以測試設置。 但它絕對不起作用,我不知道如何繼續。
export default class Proofr {
constructor() {
console.log('test');
}
}
然后由webpack
生成這個“腳本”,使用eslint-loader
和babel-loader
( eslint-loader
env&stage-0)。
所以它不應該只是為了記錄消息的樂趣。 但是當我嘗試調用new Proofr()
它會告訴我以下內容:
form.js:25 Uncaught TypeError: _proofr2.default is not a constructor
但如果它不是構造函數那么它是什么? 這是一個空對象,至少這是crome開發工具所說的。 我導入了通過紗線鏈接與js文件中的項目鏈接的打樣機。
import Proofr from 'proofr';
import PinguComponent from '../../assets/js/helpers/PinguComponent';
class Form extends PinguComponent {
constructor(el) {
const defaultOpts = {
classes: {
dom: {},
state: {},
},
customEvents: {},
};
const defaultData = {};
super(el, defaultOpts, defaultData);
new Proofr();
this.log('Form loaded');
}
}
export default Form;
所以,在我的知識結束時,也許有人看到最可能非常小的拼寫錯誤。 兩個存儲庫都使用相同的babelrc,因此它們使用相同的預設。
這里由webpack和babel生成的文件用於打樣
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Proofr a lightweight js tool
*/
var Proofr = function Proofr() {
_classCallCheck(this, Proofr);
console.log('test');
};
exports.default = Proofr;
/***/ })
/******/ ]);
如果你想親眼看到它你可以通過npm安裝打樣機(或紗線打樣機)添加它
通過將output.libraryTarget
設置為umd
來更改proofr
庫的webpack構建配置:
output: {
filename: '[name]',
path: path.resolve(__dirname, '../dist'),
libraryTarget: "umd"
},
有關更多選項,請參閱https://webpack.js.org/configuration/output/#output-librarytarget 。
我通過使用npm run start
重建校對器並在node.js腳本中導入它來測試它:
var proofr = require('proofr');
console.log(proofr);
沒有output.libraryTarget: "umd"
:
$ node index.js
{}
用它:
$ node index.js
{ default: [Function: Proofr] }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.