asp.net – .Net Core 2 Spa模板与角度材料

前端之家收集整理的这篇文章主要介绍了asp.net – .Net Core 2 Spa模板与角度材料前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
试图让Angular Material或任何第三方控制集真正地使用这个新模板而苦苦挣扎.在这个新模板中,webpack分为TreeShakable和NonTreeShakable.此外,app.module现在是app.module.shared,app.module.browser,app.module.server.

由于我试图使其工作,应用程序将仅使用app.module.browser中配置的模块运行,但材料标签未得到处理.尝试简单的东西并尝试按钮.我没有收到任何错误,但没有错误.我在Plunker中找到了他们的例子,复制了它生成内容,它显示正确告诉我,我至少得到了正确的CSS.

包括webpack供应商配置作为起点,因为这似乎是关键因为它如何捆绑css和js.

TIA

Webpack.vendor
const path = require('path');
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','@angular/material','@angular/cdk','zone.js'

];
const nonTreeShakableModules = [
    'bootstrap','jqwidgets-framework',"@angular/material/prebuilt-themes/indigo-pink.css",'bootstrap/dist/css/bootstrap.css','font-awesome/css/font-awesome.css','es6-promise','es6-shim','event-source-polyfill','jquery',];
const allModules = treeShakableModules.concat(nonTreeShakableModules);

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|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/,// 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,'dist','[name]-manifest.json'),name: '[name]_[hash]'
            })
        ].concat(isDevBuild ? [] : [
            new webpack.optimize.UglifyJsPlugin()
        ])
    });

    const serverBundleConfig = merge(sharedConfig,{
        target: 'node',resolve: { mainFields: ['main'] },entry: { vendor: allModules.concat(['aspnet-prerendering']) },output: {
            path: path.join(__dirname,'ClientApp','dist'),libraryTarget: 'commonjs2',},module: {
            rules: [ { test: /\.css(\?|$)/,use: ['to-string-loader',isDevBuild ? 'css-loader' : 'css-loader?minimize' ] } ]
        },plugins: [
            new webpack.DllPlugin({
                path: path.join(__dirname,name: '[name]_[hash]'
            })
        ]
    });

    return [clientBundleConfig,serverBundleConfig];
}

解决方法

您需要在nonTreeShakableModules中包含角度材质和cdk,如:
const treeShakableModules = [
    '@angular/animations','zone.js',];
const nonTreeShakableModules = [
    'bootstrap','@angular/material/prebuilt-themes/indigo-pink.css',];

确保已使用以下2个命令从npm安装了角度材质和cdk模块(动画模块是可选的):

npm install --save @angular/material @angular/cdk
npm install --save @angular/animations

这应该在package.json中添加以下行:

"@angular/animations": "https://registry.npmjs.org/@angular/animations/-/animations-4.2.5.tgz","@angular/cdk": "^2.0.0-beta.8","@angular/material": "^2.0.0-beta.8",

您现在应该尝试使用cmd或PowerShell中的以下命令执行webpack build:

webpack --config webpack.config.vendor.js

如果没有错误,您可以在app.module.shared.ts中包含要使用的组件:

// angular material modules
import {
    MdAutocompleteModule,MdButtonModule,MdButtonToggleModule,MdCardModule,MdCheckBoxModule,MdChipsModule,MdCoreModule,MdDatepickerModule,MdDialogModule,MdExpansionModule,MdGridListModule,MdIconModule,MdInputModule,MdListModule,MdMenuModule,MdNativeDateModule,MdPaginatorModule,MdProgressBarModule,MdProgressSpinnerModule,MdRadioModule,MdRippleModule,MdSelectModule,MdSidenavModule,MdSliderModule,MdSlideToggleModule,MdSnackBarModule,MdSortModule,MdTableModule,MdTabsModule,MdToolbarModule,MdTooltipModule,} from '@angular/material';
import { CdkTableModule } from '@angular/cdk';

并将它们添加到@NgModule中的导入

在下一次修复之前,仍有一些组件被窃听.就像复选框组件在刷新页面时破坏服务器端渲染一样.但它将在下一个版本中修复(它已经在主分支上).

原文链接:https://www.f2er.com/netcore/245097.html

猜你在找的.NET Core相关文章