ReferenceError:未定义matchMedia

时间:2018-03-12 23:44:52

标签: angular webpack sass asp.net-core-2.0 angular-flex-layout

我正在尝试将sass连接到我的asp.net core2 / angular 5 Web应用程序。我试图合并scss(sass)文件而不是css。我认为设置似乎是正确的,但是当我运行应用程序时,我得到matchMedia错误。这是我的设置: webpack.config.js:

const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: {
            modules: false
        },
        context: __dirname,
        resolve: {
            extensions: ['.js', '.ts']
        },
        output: {
            filename: '[name].js',
            publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            rules: [{
                    test: /\.ts$/,
                    use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader', 'angular2-router-loader'] : '@ngtools/webpack'
                },
                {
                    test: /\.html$/,
                    use: 'html-loader?minimize=false'
                },
                {
                    test: /\.(css|scss)$/,
                    loaders: ['to-string-loader', 'css-loader', 'sass-loader']
                },
                {
                    test: /\.(png|jpg|jpeg|gif|svg)$/,
                    use: 'url-loader?limit=25000'
                }
            ]
        },
        plugins: [new ExtractTextPlugin('css/[name].css'), new CheckerPlugin()]
    };

    // Configuration for client-side bundle suitable for running in browsers
    const clientBundleOutputDir = './wwwroot/dist';
    const clientBundleConfig = merge(sharedConfig, {
        entry: {
            'main-client': './ClientApp/boot.browser.ts'
        },
        output: {
            path: path.join(__dirname, clientBundleOutputDir)
        },
        plugins: [
            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(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
            // Plugins that apply in production builds only
            new webpack.optimize.UglifyJsPlugin(),
            new AotPlugin({
                tsConfigPath: './tsconfig.json',
                entryModule: path.join(__dirname, 'ClientApp/app/app.browser.module#AppModule'),
                exclude: ['./**/*.server.ts']
            })
        ])
    });

    // Configuration for server-side (prerendering) bundle suitable for running in Node
    const serverBundleConfig = merge(sharedConfig, {
        resolve: {
            mainFields: ['main']
        },
        entry: {
            'main-server': './ClientApp/boot.server.ts'
        },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./ClientApp/dist/vendor-manifest.json'),
                sourceType: 'commonjs2',
                name: './vendor'
            })
        ].concat(isDevBuild ? [] : [
            // Plugins that apply in production builds only
            new AotPlugin({
                tsConfigPath: './tsconfig.json',
                entryModule: path.join(__dirname, 'ClientApp/app/app.server.module#AppModule'),
                exclude: ['./**/*.browser.ts']
            })
        ]),
        output: {
            libraryTarget: 'commonjs',
            path: path.join(__dirname, './ClientApp/dist')
        },
        target: 'node',
        devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
};

这里是我使用matchMedia查询的地方: sidenav.component.ts:

import { Component, OnInit, NgZone } from '@angular/core';

const SMALL_WIDTH_BREAKPOINT = 720;

@Component({
  selector: 'app-sidenav',
  templateUrl: './sidenav.component.html',
  styleUrls: ['./sidenav.component.scss']
})
export class SidenavComponent implements OnInit {

  private mediaMatcher: MediaQueryList =
    matchMedia(`(max-width: ${SMALL_WIDTH_BREAKPOINT}px)`);

  constructor(zone: NgZone) {
    this.mediaMatcher.addListener(mql => 
      zone.run(() => this.mediaMatcher = mql));
  }

  ngOnInit() {
  }

  isScreenSmall(): boolean {
    return this.mediaMatcher.matches;
  }

}

的package.json:

{
  "name": "myapp.Forms.API",
  "private": true,
  "version": "0.0.0",
  "scripts": {
    "test": "karma start ClientApp/test/karma.conf.js"
  },
  "devDependencies": {
    "@angular/cdk": "^5.2.4",
    "@angular/cli": "^1.7.3",
    "@angular/compiler-cli": "^5.2.8",
    "@angular/material": "^5.2.4",
    "@ngtools/webpack": "^1.10.2",
    "@types/chai": "^3.4.34",
    "@types/jasmine": "^2.8.6",
    "@types/node": "^7.0.56",
    "angular-matchmedia": "^1.0.0",
    "chai": "^3.5.0",
    "codelyzer": "^3.0.0",
    "css-loader": "^0.28.10",
    "extract-text-webpack-plugin": "^3.0.2",
    "istanbul-instrumenter-loader": "^3.0.0",
    "jasmine-core": "^2.99.1",
    "karma": "^1.7.1",
    "karma-chai": "^0.1.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-coverage": "^1.1.1",
    "karma-jasmine": "^1.1.1",
    "karma-mocha-reporter": "^2.2.4",
    "karma-phantomjs-launcher": "^1.0.4",
    "karma-remap-coverage": "^0.1.4",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "^2.0.13",
    "node-sass": "^4.8.1",
    "raw-loader": "^0.5.1",
    "sass-loader": "^6.0.7",
    "style-loader": "^0.18.2",
    "tslint": "^5.7.0",
    "webpack": "^3.11.0",
    "webpack-bundle-analyzer": "^2.11.1"
  },
  "dependencies": {
    "@angular/animations": "^5.2.8",
    "@angular/common": "^5.2.8",
    "@angular/compiler": "^5.2.8",
    "@angular/core": "^5.2.8",
    "@angular/flex-layout": "^5.0.0-beta.13",
    "@angular/forms": "^5.2.8",
    "@angular/http": "^5.2.8",
    "@angular/platform-browser": "^5.2.8",
    "@angular/platform-browser-dynamic": "^5.2.8",
    "@angular/platform-server": "^5.2.8",
    "@angular/router": "^5.2.8",
    "@nguniversal/aspnetcore-engine": "^5.0.0-beta.5",
    "@nguniversal/common": "^5.0.0-beta.5",
    "@ngx-translate/core": "^8.0.0",
    "@ngx-translate/http-loader": "^2.0.0",
    "angular2-router-loader": "^0.3.5",
    "angular2-template-loader": "^0.6.2",
    "aspnet-prerendering": "^3.0.1",
    "aspnet-webpack": "^2.0.1",
    "awesome-typescript-loader": "^3.5.0",
    "bootstrap": "^3.3.7",
    "bootstrap-sass": "^3.3.7",
    "core-js": "^2.5.1",
    "css": "^2.2.1",
    "es6-shim": "^0.35.3",
    "event-source-polyfill": "^0.0.9",
    "expose-loader": "^0.7.5",
    "file-loader": "^0.11.2",
    "hammerjs": "^2.0.8",
    "html-loader": "^0.5.1",
    "isomorphic-fetch": "^2.2.1",
    "jquery": "^2.2.1",
    "json-loader": "^0.5.7",
    "moment": "2.18.1",
    "ngx-bootstrap": "2.0.0-beta.6",
    "npm": "^5.7.1",
    "preboot": "^5.0.0",
    "rimraf": "^2.6.2",
    "rxjs": "^5.5.6",
    "to-string-loader": "^1.1.5",
    "typescript": "~2.5.0",
    "url-loader": "^0.5.7",
    "webpack-hot-middleware": "^2.21.2",
    "webpack-merge": "^4.1.2",
    "zone.js": "^0.8.17"
  }
}

以下是我运行应用时遇到的错误:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[0]
      An unhandled exception has occurred while executing the request
Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: matchMedia is not defined
ReferenceError: matchMedia is not defined
    at new SidenavComponent (C:\Users\user\Documents\Visual Studio 2017\Projects\myapp.Forms\myapp.forms.api\ClientApp\dist\main-server.js:42199:9)
    at createClass (C:\Users\user\Documents\Visual Studio 2017\Projects\myapp.Forms\myapp.forms.api\ClientApp\dist\vendor.js:12858:20)
    at createDirectiveInstance (C:\Users\user\Documents\Visual Studio 2017\Projects\myapp.Forms\myapp.forms.api\ClientApp\dist\vendor.js:12703:37)
    at createViewNodes (C:\Users\user\Documents\Visual Studio 2017\Projects\myapp.Forms\myapp.forms.api\ClientApp\dist\vendor.js:14161:53)
    at callViewAction (C:\Users\user\Documents\Visual Studio 2017\Projects\myapp.Forms\myapp.forms.api\ClientApp\dist\vendor.js:14595:13)
    at execComponentViewsAction (C:\Users\user\Documents\Visual Studio 2017\Projects\myapp.Forms\myapp.forms.api\ClientApp\dist\vendor.js:14504:13)
    at createViewNodes (C:\Users\user\Documents\Visual Studio 2017\Projects\myapp.Forms\myapp.forms.api\ClientApp\dist\vendor.js:14189:5)
    at createRootView (C:\Users\user\Documents\Visual Studio 2017\Projects\myapp.Forms\myapp.forms.api\ClientApp\dist\vendor.js:14050:5)
    at Object.createProdRootView [as createRootView] (C:\Users\user\Documents\Visual Studio 2017\Projects\myapp.Forms\myapp.forms.api\ClientApp\dist\vendor.js:14743:12)
    at ComponentFactory_.module.exports.ComponentFactory_.create (C:\Users\user\Documents\Visual Studio 2017\Projects\myapp.Forms\myapp.forms.api\ClientApp\dist\vendor.js:11655:46)
   at Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance.<InvokeExportAsync>d__7`1.MoveNext()

有关如何纠正的任何想法?

0 个答案:

没有答案