.Net Core 2.0 React VS 模板导入组件无法渲染



>我在Visual Studio 2017中使用默认.Net Core 2.0 React Web模板创建了一个Web项目。然后使用 npm install -save react-table @types/react-table 安装了 ReactTable。一切正常。

然后我想使用该组件。所以在文件ClientApp/components/FetchData.tsx里面,在顶部

import { ReactTable } from 'react-table';

然后我用 renderForecastsTable 替换

const columns = [
    {
        Header: 'Date formatted',
        accessor: 'dateFormatted'
    }, {
        Header: 'Summary',
        accessor: 'summary'
    }
];
return <ReactTable data={forecasts} columns={columns} />;

当我在 Chrome 中运行时,会发生以下错误:vendor.js?v=OjVxDpV6p_Jfz2P38F_R2lc3pjVsUisUejeIABZq7AE:118 Uncaught (in promise) Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of FetchData.

我想这是由于 web 包中的某些设置或项目丢失,因此它在运行时找不到该组件。知道吗?

编辑Webpack.config.js

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
const bundleOutputDir = './wwwroot/dist';
module.exports = (env) => {
    const isDevBuild = !(env && env.prod);
    return [{
        stats: { modules: false },
        entry: { 'main': './ClientApp/boot.tsx' },
        resolve: { extensions: ['.js', '.jsx', '.ts', '.tsx'] },
        output: {
            path: path.join(__dirname, bundleOutputDir),
            filename: '[name].js',
            publicPath: 'dist/'
        },
        module: {
            rules: [
                { test: /.tsx?$/, include: /ClientApp/, use: 'awesome-typescript-loader?silent=true' },
                { test: /.css$/, use: isDevBuild ? ['style-loader', 'css-loader'] : ExtractTextPlugin.extract({ use: 'css-loader?minimize' }) },
                { test: /.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
            ]
        },
        plugins: [
            new CheckerPlugin(),
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./wwwroot/dist/vendor-manifest.json')
            })
        ].concat(isDevBuild ? [
            // Plugins that apply in development builds only
            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(bundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
            // Plugins that apply in production builds only
            new webpack.optimize.UglifyJsPlugin(),
            new ExtractTextPlugin('site.css')
        ])
    }];
};

尝试使用从"反应表"导入ReactTable。

最新更新