typescript - vue不是构造函数错误,使用Typescript和webpack

标签 typescript webpack vue.js

我正在使用 Typescript 和 Webpack。我的代码如下。我在 chrome 中运行它,它给出了错误:

Uncaught TypeError: vue_1.default is not a constructor
    at Object.defineProperty.value (Index.ts:3)
    at __webpack_require__ (bootstrap f29fbbb047d131556dcf:19)
    at Object.defineProperty.value (bootstrap f29fbbb047d131556dcf:62)
    at bootstrap f29fbbb047d131556dcf:62

我添加了导入,还添加了 resolve -> alias -> vue 部分。并尝试了一大堆其他东西,但没有用。我也尝试过 tsconfig 文件,但没有成功。

我该如何解决?

webpack.config.js

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');

module.exports = {
    context: __dirname,//another dir +"/app"
    // devtool: debug ? "inline-sourcemap" : null,

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    entry: "./code/client/scripts/Index.ts",
    output: {
        path: __dirname + "/code/client/views",
        filename: "scripts.min.js"
    },
    plugins: debug ? [] : [
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin({mangle: false, sourcemap: false}),
        new webpack.IgnorePlugin(/fs/),
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"'
            }
        })
    ],
    node: {
        fs: 'empty',
        child_process: 'empty',
    },

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        alias: {
            vue: 'vue/dist/vue.js'
        },
        extensions: [".ts", ".tsx", ".js", ".json"]
    },

    module: {
        rules: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
            { test: /\.tsx?$/, loader: "awesome-typescript-loader" },

            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            { enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
        ]
    },
};

tsconfig.js

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2015",
    "noImplicitThis": true,
    "sourceMap": true,
    "strictNullChecks": true,
    "removeComments": false,
    "moduleResolution": "node",
    "types": [
      "node",
      "mocha",
      "chai"
    ],
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules"
  ]
}

索引.ts

import Vue from "vue"

var app = new Vue({
    el: '#app',
    data: {
        message: 'Hello Vue!',
    }
})
console.log(app)

html

<div id="app">
    <p>{{ message }}</p>
    <input v-model="message">
</div>

最佳答案

vue/dist/vue.js文件没有默认导出,但是Vue也提供了ES模块版本:vue/dist/vue.esm.js。你想使用这个,因为你将它用作带有 import 语句的 ES 模块。

您的别名应该是:

alias: {
    'vue': 'vue/dist/vue.esm.js'
},

关于typescript - vue不是构造函数错误,使用Typescript和webpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47357763/

相关文章:

typescript - NgModel subscribe 仅在 angular2 中更改实际文本区域时触发

typescript - 寻找有关如何从 Javascript 库生成 .d.ts 文件的文档

javascript - 使用 Browserify 时如何捕获 javascript 文件的导出?

vue.js - Vue点击按钮时改变属性值的方法

typescript - Typescript 和 VSCode 的简单问题

Javascript 数组到对象的转换

javascript - Karma 代码覆盖率在测试中始终显示 100% (0/0)

javascript - react Bootstrap : TypeError: Unable to get property 'bool' of undefined or null reference

vue.js - 使用对象通过 v-for 生成选择选项

javascript - 在 Javascript 中将对象转换为包含多个对象的数组的理想方法是什么? (允许 ES6)