Angular数据表使用webpack呈现错误

时间:2017-10-19 06:57:37

标签: angular webpack angular-datatables

你好我使用的角度数据表有问题 https://l-lin.github.io/angular-datatables/#/getting-started

我已按照说明操作,但数据表未被渲染,不同之处在于我使用webpack编译脚本和css文件,并将主题作为叠加层。正在编译文件,但是当我尝试显示数据表时会出现问题,这会导致错误

Global error handler: TypeError: $(...).DataTable is not a function
    at angular-datatables.directive.js:38
    at ZoneDelegate.invokeTask (zone.js:425)
    at Object.onInvokeTask (core.es5.js:3881)
    at ZoneDelegate.invokeTask (zone.js:424)
    at Zone.runTask (zone.js:192)
    at ZoneTask.invokeTask (zone.js:499)
    at ZoneTask.invoke (zone.js:488)
    at timer (zone.js:2040)

主题路由模块中包含的角度数据表:

import { NgModule } from '@angular/core';
import { AuthGuard } from "../auth";
import { ThemeComponent } from './';
import { 
    Routes, 
    RouterModule 
} from '@angular/router';
import {
    IndexComponent,
    View1Component,
    View2Component
} from './pages';
import { DataTablesModule } from 'angular-datatables';

const routes: Routes = [
    {
        path: 'index',
        component: IndexComponent
    },
    {
        path: 'view1',
        component: View1Component
    },
    {
        path: 'view2',
        component: View2Component
    }
];

@NgModule({
    declarations: [
        IndexComponent,
        View1Component,
        View2Component
    ],
    imports: [RouterModule.forChild(routes), DataTablesModule],
    exports: [RouterModule]
})

export class ThemeRoutingModule { }

和webpack.config.vendor.js

const path = require('path');
const glob = require('glob');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const merge = require('webpack-merge');
const treeShakableModules = [
    '@angular/animations',
    '@angular/common',
    '@angular/compiler',
    '@angular/core',
    '@angular/forms',
    '@angular/http',
    '@angular/platform-browser',
    '@angular/platform-browser-dynamic',
    '@angular/router',
    'zone.js',
];
const nonTreeShakableModules = [
    'bootstrap',
    'bootstrap/dist/css/bootstrap.css',
    'es6-promise',
    'es6-shim',
    'event-source-polyfill',
    'jquery',
    'datatables.net/js/jquery.dataTables.js',
    'datatables.net-dt/css/jquery.dataTables.css'
];

const allModules = treeShakableModules.concat(nonTreeShakableModules).concat(glob.sync('./ClientApp/assets/**/*.css')).concat(glob.sync('./ClientApp/assets/**/*.js'));

module.exports = (env) => {
    const extractCSS = new ExtractTextPlugin('vendor.css');
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: false },
        resolve: { extensions: [ '.js' ] },
        module: {
            rules: [
                { test: /\.(png|woff|gif|woff2|eot|ttf|svg)(\?|$)/, use: 'url-loader?limit=100000' }
            ]
        },
        output: {
            publicPath: 'dist/',
            filename: '[name].js',
            library: '[name]_[hash]'
        },
        plugins: [
            new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
            new webpack.ContextReplacementPlugin(/\@angular\b.*\b(bundles|linker)/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/11580
            new webpack.ContextReplacementPlugin(/angular(\\|\/)core(\\|\/)@angular/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/14898
            new webpack.IgnorePlugin(/^vertx$/) // Workaround for https://github.com/stefanpenner/es6-promise/issues/100
        ]
    };

    const clientBundleConfig = merge(sharedConfig, {
        entry: {
            // To keep development builds fast, include all vendor dependencies in the vendor bundle.
            // But for production builds, leave the tree-shakable ones out so the AOT compiler can produce a smaller bundle.
            vendor: isDevBuild ? allModules : nonTreeShakableModules
        },
        output: { path: path.join(__dirname, 'wwwroot', 'dist') },
        module: {
            rules: [
                {
                    test: /\.css(\?|$)/, use: extractCSS.extract({ use: isDevBuild ? 'css-loader' : 'css-loader?minimize' })
                }
            ]
        },
        plugins: [
            extractCSS,
            new webpack.DllPlugin({
                path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
                name: '[name]_[hash]'
            })
        ].concat(isDevBuild ? [] : [
            new webpack.optimize.UglifyJsPlugin()
        ])
    });

    return [clientBundleConfig];
}

0 个答案:

没有答案