首页 \ 问答 \ Angular 2 Webpack 2 CSS模块解析错误(Angular 2 Webpack 2 CSS module parse error)

Angular 2 Webpack 2 CSS模块解析错误(Angular 2 Webpack 2 CSS module parse error)

我开始使用Angular 2和webpack 2.在运行webpack时,我遇到了以下错误

WARNING in ./~/@angular/core/@angular/core.es5.js
5870:15-36 Critical dependency: the request of a dependency is an expression

WARNING in ./~/@angular/core/@angular/core.es5.js
5886:15-102 Critical dependency: the request of a dependency is an expression

ERROR in ./app/dashboard/hero-detail.component.css
Module parse failed: D:\WorkSpace\Angular2\Login\app\dashboard\hero-detail.compo
nent.css Unexpected token (1:6)
You may need an appropriate loader to handle this file type.
| label {
|   display: inline-block;
|   width: 3em;
 @ ./app/dashboard/hero-detail.component.ts 45:17-55
 @ ./app/dashboard/index.ts
 @ ./app/app.module.ts
 @ ./app/main.ts

ERROR in ./app/dashboard/dashboard.component.css
Module parse failed: D:\WorkSpace\Angular2\Login\app\dashboard\dashboard.compone
nt.css Unexpected token (1:6)
You may need an appropriate loader to handle this file type.
| [class*='col-'] {
|   float: left;
|   padding-right: 20px;
 @ ./app/dashboard/dashboard.component.ts 32:17-53
 @ ./app/dashboard/index.ts
 @ ./app/app.module.ts
 @ ./app/main.ts

ERROR in ./app/dashboard/heroes.component.css
Module parse failed: D:\WorkSpace\Angular2\Login\app\dashboard\heroes.component.
css Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| .selected {
|   background-color: #CFD8DC !important;
|   color: white;
 @ ./app/dashboard/heroes.component.ts 65:17-50
 @ ./app/dashboard/index.ts
 @ ./app/app.module.ts
 @ ./app/main.ts

ERROR in ./app/dashboard/hero-search.component.css
Module parse failed: D:\WorkSpace\Angular2\Login\app\dashboard\hero-search.compo
nent.css Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| .search-result{
|   border-bottom: 1px solid gray;
|   border-left: 1px solid gray;
 @ ./app/dashboard/hero-search.component.ts 59:17-55
 @ ./app/dashboard/index.ts
 @ ./app/app.module.ts
 @ ./app/main.ts
Child html-webpack-plugin for "index.html":
       [0] ./~/html-webpack-plugin/lib/loader.js!./index.html 341 bytes {0} [built]

下面是我的package.json

  "devDependencies": {
    "@types/core-js": "^0.9.41",
    "@types/jasmine": "2.5.36",
    "@types/node": "^6.0.45",
    "angular2-template-loader": "^0.6.0",
    "awesome-typescript-loader": "^3.0.4",
    "css-loader": "^0.28.0",
    "extract-text-webpack-plugin": "^2.1.0",
    "file-loader": "^0.9.0",
    "html-loader": "^0.4.5",
    "html-webpack-plugin": "^2.16.1",
    "jasmine-core": "^2.4.1",
    "karma": "^1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-jasmine": "^1.0.2",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "^2.0.1",
    "null-loader": "^0.1.1",
    "postcss-cssnext": "^2.10.0",
    "postcss-loader": "^1.3.3",
    "postcss-responsive-type": "^0.5.1",
    "postcss-smart-import": "^0.6.12",
    "raw-loader": "^0.5.1",
    "resolve-url-loader": "^2.0.2",
    "rimraf": "^2.5.2",
    "sass-loader": "^6.0.3",
    "style-loader": "^0.13.1",
    "stylus-loader": "^3.0.1",
    "to-string-loader": "^1.1.5",
    "typescript": "^2.0.9",
    "webpack": "2.2.1",
    "webpack-dev-server": "2.4.1",
    "webpack-merge": "^3.0.0"
  }

这是我的webpack.config.js

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');

module.exports = {
    entry: {
        'app': './app/main.ts',
        'vendor': './vendor.ts'
    },
    output: {
        filename: "[name].js",
        path: path.resolve(__dirname, 'dist')
    },
    resolve: {
        extensions: ['.ts', '.js']
    },

    module: {
        rules: [
            {
                test: /\.ts$/,
                loaders: ['awesome-typescript-loader', 'angular2-template-loader']
            },
            {
                test: /\.html$/,
                use: [{
                    loader: 'html-loader',
                    options: {
                        minimize: true
                    }
                }],
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                loader: 'file-loader?name=assets/[name].[hash].[ext]'
            },
            {
                test: /\.css$/,
                include: /app(\/|\\)css/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            }
        ]
    },

    plugins: [
        // Workaround for angular/angular#11580
        new webpack.ContextReplacementPlugin(
            // The (\\|\/) piece accounts for path separators in *nix and Windows
            /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
            'app', // location of your src
            {} // a map of your routes
        ),

        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor'
        }),

        new HtmlWebpackPlugin({
            template: './index.html'
        }),
        new ExtractTextPlugin("bundle.css")
    ]
};

如果我删除包含在css中的内置加载器是成功的但是我在遇到URL时遇到了不同的错误

Uncaught Error: Expected 'styles' to be an array of strings.
    at assertArrayOfStrings (http://127.0.0.1:8080/vendor.js:30661:19) [<root>]
    at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (http://127.0.0.1:8080/vendor.js:41563:13) [<root>]
    at CompileMetadataResolver._getEntryComponentMetadata (http://127.0.0.1:8080/vendor.js:42265:45) [<root>]
    at http://127.0.0.1:8080/vendor.js:42251:48 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver._getEntryComponentsFromProvider (http://127.0.0.1:8080/vendor.js:42250:30) [<root>]
    at http://127.0.0.1:8080/vendor.js:42214:83 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver._getProvidersMetadata (http://127.0.0.1:8080/vendor.js:42178:19) [<root>]
    at http://127.0.0.1:8080/vendor.js:42180:23 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver._getProvidersMetadata (http://127.0.0.1:8080/vendor.js:42178:19) [<root>]
    at http://127.0.0.1:8080/vendor.js:41753:63 [<root>]
    at Array.forEach (native) [<root>]

似乎很多人都面临这个问题,但他们的解决方案对我有用。任何想法我哪里出错了?


I am starting with Angular 2 with webpack 2. while running webpack I am facing below errors

WARNING in ./~/@angular/core/@angular/core.es5.js
5870:15-36 Critical dependency: the request of a dependency is an expression

WARNING in ./~/@angular/core/@angular/core.es5.js
5886:15-102 Critical dependency: the request of a dependency is an expression

ERROR in ./app/dashboard/hero-detail.component.css
Module parse failed: D:\WorkSpace\Angular2\Login\app\dashboard\hero-detail.compo
nent.css Unexpected token (1:6)
You may need an appropriate loader to handle this file type.
| label {
|   display: inline-block;
|   width: 3em;
 @ ./app/dashboard/hero-detail.component.ts 45:17-55
 @ ./app/dashboard/index.ts
 @ ./app/app.module.ts
 @ ./app/main.ts

ERROR in ./app/dashboard/dashboard.component.css
Module parse failed: D:\WorkSpace\Angular2\Login\app\dashboard\dashboard.compone
nt.css Unexpected token (1:6)
You may need an appropriate loader to handle this file type.
| [class*='col-'] {
|   float: left;
|   padding-right: 20px;
 @ ./app/dashboard/dashboard.component.ts 32:17-53
 @ ./app/dashboard/index.ts
 @ ./app/app.module.ts
 @ ./app/main.ts

ERROR in ./app/dashboard/heroes.component.css
Module parse failed: D:\WorkSpace\Angular2\Login\app\dashboard\heroes.component.
css Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| .selected {
|   background-color: #CFD8DC !important;
|   color: white;
 @ ./app/dashboard/heroes.component.ts 65:17-50
 @ ./app/dashboard/index.ts
 @ ./app/app.module.ts
 @ ./app/main.ts

ERROR in ./app/dashboard/hero-search.component.css
Module parse failed: D:\WorkSpace\Angular2\Login\app\dashboard\hero-search.compo
nent.css Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| .search-result{
|   border-bottom: 1px solid gray;
|   border-left: 1px solid gray;
 @ ./app/dashboard/hero-search.component.ts 59:17-55
 @ ./app/dashboard/index.ts
 @ ./app/app.module.ts
 @ ./app/main.ts
Child html-webpack-plugin for "index.html":
       [0] ./~/html-webpack-plugin/lib/loader.js!./index.html 341 bytes {0} [built]

Below is my package.json

  "devDependencies": {
    "@types/core-js": "^0.9.41",
    "@types/jasmine": "2.5.36",
    "@types/node": "^6.0.45",
    "angular2-template-loader": "^0.6.0",
    "awesome-typescript-loader": "^3.0.4",
    "css-loader": "^0.28.0",
    "extract-text-webpack-plugin": "^2.1.0",
    "file-loader": "^0.9.0",
    "html-loader": "^0.4.5",
    "html-webpack-plugin": "^2.16.1",
    "jasmine-core": "^2.4.1",
    "karma": "^1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-jasmine": "^1.0.2",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "^2.0.1",
    "null-loader": "^0.1.1",
    "postcss-cssnext": "^2.10.0",
    "postcss-loader": "^1.3.3",
    "postcss-responsive-type": "^0.5.1",
    "postcss-smart-import": "^0.6.12",
    "raw-loader": "^0.5.1",
    "resolve-url-loader": "^2.0.2",
    "rimraf": "^2.5.2",
    "sass-loader": "^6.0.3",
    "style-loader": "^0.13.1",
    "stylus-loader": "^3.0.1",
    "to-string-loader": "^1.1.5",
    "typescript": "^2.0.9",
    "webpack": "2.2.1",
    "webpack-dev-server": "2.4.1",
    "webpack-merge": "^3.0.0"
  }

This is my webpack.config.js

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');

module.exports = {
    entry: {
        'app': './app/main.ts',
        'vendor': './vendor.ts'
    },
    output: {
        filename: "[name].js",
        path: path.resolve(__dirname, 'dist')
    },
    resolve: {
        extensions: ['.ts', '.js']
    },

    module: {
        rules: [
            {
                test: /\.ts$/,
                loaders: ['awesome-typescript-loader', 'angular2-template-loader']
            },
            {
                test: /\.html$/,
                use: [{
                    loader: 'html-loader',
                    options: {
                        minimize: true
                    }
                }],
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                loader: 'file-loader?name=assets/[name].[hash].[ext]'
            },
            {
                test: /\.css$/,
                include: /app(\/|\\)css/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            }
        ]
    },

    plugins: [
        // Workaround for angular/angular#11580
        new webpack.ContextReplacementPlugin(
            // The (\\|\/) piece accounts for path separators in *nix and Windows
            /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
            'app', // location of your src
            {} // a map of your routes
        ),

        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor'
        }),

        new HtmlWebpackPlugin({
            template: './index.html'
        }),
        new ExtractTextPlugin("bundle.css")
    ]
};

If I remove include in css loader built is successfull but I am facing different error while hitting the URL

Uncaught Error: Expected 'styles' to be an array of strings.
    at assertArrayOfStrings (http://127.0.0.1:8080/vendor.js:30661:19) [<root>]
    at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (http://127.0.0.1:8080/vendor.js:41563:13) [<root>]
    at CompileMetadataResolver._getEntryComponentMetadata (http://127.0.0.1:8080/vendor.js:42265:45) [<root>]
    at http://127.0.0.1:8080/vendor.js:42251:48 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver._getEntryComponentsFromProvider (http://127.0.0.1:8080/vendor.js:42250:30) [<root>]
    at http://127.0.0.1:8080/vendor.js:42214:83 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver._getProvidersMetadata (http://127.0.0.1:8080/vendor.js:42178:19) [<root>]
    at http://127.0.0.1:8080/vendor.js:42180:23 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver._getProvidersMetadata (http://127.0.0.1:8080/vendor.js:42178:19) [<root>]
    at http://127.0.0.1:8080/vendor.js:41753:63 [<root>]
    at Array.forEach (native) [<root>]

It seems many have faced this issue but their solution dint worked for me.Any idea where I am going wrong?


原文:https://stackoverflow.com/questions/43732532
更新时间:2019-12-06 06:00

最满意答案

您可以使用DataGridTemplateColumn创建自定义模板 ,并在其DataTemplate中放置具有所需边距的文本块。

  <DataGrid Name="dgUsers" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
        <DataGridTemplateColumn Header="Birthday">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Textblock Text="{Binding Name}" BorderThickness="0" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

资源


You can create a custom template, with a DataGridTemplateColumn, and in its DataTemplate you can put a textblock with the margin that you want.

  <DataGrid Name="dgUsers" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
        <DataGridTemplateColumn Header="Birthday">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Textblock Text="{Binding Name}" BorderThickness="0" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Source

相关问答

更多
  • 尝试更换 你的问题应该解决了。 WPF中的宽度是一个指示,而ActualWidth是元素渲染后的实际宽度。 编辑:要避免在combobox上进行裁剪,您需要添加转换器(请参阅此答案 )以删除一些像素的宽度。 如果 ...
  • 您可以使用DataGridTemplateColumn创建自定义模板 ,并在其DataTemplate中放置具有所需边距的文本块。
    @Ruben解决方案有效。 对于任何关心此处的人都是工作代码:
  • 列中的绑定可能会覆盖Setter 。 但是,您不需要数据触发器来执行此操作。 由于绑定中有一个属性,您可以为这些场景设置。 TargetNullValue允许您在绑定路径为空的情况下设置值。 Binding="{Binding Path=finish, TargetNullValue=Whatever you want}" 摘自: 使用WPF数据绑定将NULL值显示为“NULL”的最简单方法是什么? The Binding in the column might be overriding the Sett ...
  • 意识到我试图做的是一件愚蠢的事。 解决方案是将行为提取到可测试的类中。 Realized that what I was trying to do is a stupid thing. The solution is to extract the behavior into a testable class.
  • 你想让ComboBox + TextBlock正确对齐吗? 如果是这样,我相信你可以沿着这些方向做点什么:
  • 必须有一个可以设置的简单样式元素,对吗? 我找不到它。 是的,您可以将列的ElementStyle属性设置为以下样式以包装文本: 您可以使用XamlReader.Parse方法以编程方式创建Style: string xaml = "