【问题标题】:angular 2 styleurls not workingangular 2 styleurls 不工作
【发布时间】:2018-12-02 06:45:50
【问题描述】:

我不确定这里缺少什么。我已按照https://angular.io/guide/component-styles 的指南使用组件样式 URL。但是当我运行组件时,没有应用样式。但是,如果我使用组件内联样式,它就可以工作。

training.component.ts

@Component({
templateUrl: './training.component.html',
styleUrls: ['./training.component.css']
})

training.component.html

<p class="notification-message" style="margin-bottom:10px;">IMPORTANT NOTE: You must click the “I agree” box and press the Finish button on the last page of this document in order to receive credit for completing this training module</p>    

training.component.css

.notification-message {
border: 2px solid black;
background-color: #ffff00;
color: #ff0000;
width: 100%;
text-align: left;
padding-left: 6px;
padding-right: 6px;
padding-bottom: 2px;
font-size: 11px;
font-family: Verdana, Helvetica, Sans-Serif;
font-weight: bold;
}

webpack.common.js

module: {
    rules: [
      {
          test: /\.ts$/,
          loaders: [
            {
                loader: 'awesome-typescript-loader',
                options: { configFileName: helpers.root('', 'tsconfig.json') }
            }, 'angular2-template-loader'
          ]
      },

      {test: /\.html$/,loader: 'html-loader'},
      { test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)/, loader: 'url-loader' },
      { test: /\.css$/, loader: 'raw-loader!css-loader' }

    ]
},

plugins: [

 new CleanWebpackPlugin(['built'], { root: '' }),

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

  new webpack.optimize.CommonsChunkPlugin({
      name: "commoncode",        
      filename: "commoncode.js",
      chunks: ["app", "vendor", 'kendoVendor'],
      minChunks: Infinity,
  }),


   new webpack.ProvidePlugin({
       $: "jquery",
       jQuery: "jquery"
   })

   /*in order to automatically load bundled js files on layout page, use this plugin and do appropriate changes.
    new HtmlWebpackPlugin({
        inject: false,
        template: './Views/Shared/_LayoutTemplate.cshtml',
        filename: './Views/Shared/_Layout.cshtml'
    })*/
]

有效的组件内联样式

@Component({
templateUrl: './training.component.html',
styles: [`
.notification-message {
border: 2px solid black;
background-color: #ffff00;
color: #ff0000;
width: 100%;
text-align: left;
padding-left: 6px;
padding-right: 6px;
padding-bottom: 2px;
font-size: 11px;
font-family: Verdana, Helvetica, Sans-Serif;
font-weight: bold;
}`]})

【问题讨论】:

  • 尝试删除斜线 styleUrls: ['training.component.css']
  • ^^ 试试不带 ./
  • 我试过不带 ./ 但还是不行。到目前为止只有内联样式。
  • 确保 traing.component.css 与 training.component.ts 位于同一目录中(styleUrls: ['./training.component.css'] 可以)并且没有'与您的 .notification-message 类重叠的 css

标签: angular webpack


【解决方案1】:

我能够确定问题所在。我不得不在 webpack 配置中使用 to-string-loader。

这在 webpack.common.js

中对我有用
{
     test: /\.(css)$/,
     loaders: ['to-string-loader', 'css-loader']
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 2016-10-15
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多