Skip to content

Import CSS Styles in Rails, Angular, Webpacker Project.  #963

@mayordwells

Description

@mayordwells

I am trying to import Individual CSS modules into my component in Angular.
For example

  • component1.ts

  • component1.css / component1.sass

  • component2.ts

  • component2.css / component2.sass

I have included this in the html.d.ts which allowed me to import html files as a string before, this also allowed me to import css file as a string, I'm not sure if this is needed.

declare module "*.css" {
  const content: string
  export default content
}

I added the css to the list of imports but angular did not allow me to use homecss as a variable in the component decorator.
So used the style's url as is, however i am getting an error.
I have installed css-loader. But I have no idea what other configuration i need to do to make this work. At least for the html part, it was clearly explained in the documentation here - https://github.com/rails/webpacker/blob/master/docs/typescript.md
So why is there no clear documentation on how to do the same thing with css?
Here's my component file:

import { Component } from '@angular/core';
import home from './home.html';
import homecss from './home.css';

@Component({
  selector: 'eg-home',
  template: home,
  styleUrls: ['./home.css']
})
export class HomeComponent { }

And here's what my error in the console looks like:

zone.js?6524:2933 GET http://localhost:5000/dashboard/home.css 404 (Not Found)
scheduleTask @ zone.js?6524:2933
ZoneDelegate.scheduleTask @ zone.js?6524:411
Zone.scheduleTask @ zone.js?6524:236
Zone.scheduleMacroTask @ zone.js?6524:259
(anonymous) @ zone.js?6524:2966
proto.(anonymous function) @ zone.js?6524:1366
ResourceLoaderImpl.get @ platform-browser-dynamic.es5.js?3bad:54
DirectiveNormalizer._fetch @ compiler.es5.js?524d:14079
(anonymous) @ compiler.es5.js?524d:14201
DirectiveNormalizer._loadMissingExternalStylesheets @ compiler.es5.js?524d:14201
DirectiveNormalizer.normalizeExternalStylesheets @ compiler.es5.js?524d:14178
(anonymous) @ compiler.es5.js?524d:14110
then @ compiler.es5.js?524d:1683
DirectiveNormalizer.normalizeTemplate @ compiler.es5.js?524d:14110
CompileMetadataResolver.loadDirectiveMetadata @ compiler.es5.js?524d:15139
(anonymous) @ compiler.es5.js?524d:26833
(anonymous) @ compiler.es5.js?524d:26832
JitCompiler._loadModules @ compiler.es5.js?524d:26829
JitCompiler._compileModuleAndComponents @ compiler.es5.js?524d:26799
JitCompiler.compileModuleAsync @ compiler.es5.js?524d:26728
PlatformRef_._bootstrapModuleWithZone @ core.es5.js?de3d:4536
PlatformRef_.bootstrapModule @ core.es5.js?de3d:4522
(anonymous) @ index.ts?c69e:5
ZoneDelegate.invokeTask @ zone.js?6524:425
Zone.runTask @ zone.js?6524:192
ZoneTask.invokeTask @ zone.js?6524:499
invokeTask @ zone.js?6524:1540
globalZoneAwareCallback @ zone.js?6524:1566
zone.js?6524:690 Unhandled Promise rejection: Failed to load home.css ; Zone: <root> ; Task: Promise.then ; Value: Failed to load home.css undefined

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions