简体   繁体   English

如何在Angular 2类中导入外部SCSS

[英]How to import an external SCSS in Angular 2 class

I have to use the scss defined elsewhere, to be used inside my @Injectable class 我必须使用在其他地方定义的scss,才能在我的@Injectable类中使用

import { Injectable } from '@angular/core';

const customThemeRed = require('../../common/styles/custom/themes/custom-theme-red.scss');

@Injectable()
export class MyThemesService {
    constructor() {
        console.log('customThemeRed is' + customThemeRed);
    }
...

For some reason, am not able to use this scss even though I imported via NodeJS 'require' var. 由于某些原因,即使我通过NodeJS'require'var导入,也无法使用此scss。 May I know how to import an external JS/SCSS file 我可以知道如何导入外部JS / SCSS文件吗

You'll need to incorporate some kind of SASS preprocessor in your tool chain. 您需要在工具链中包含某种SASS预处理器。 I'd recommend Webpack which can use the sass-launcher, css-launcher, and style-launcher together to read your SCSS file import, convert the SASS to CSS, and inline it into a <style> element in the head of your page, or into your JS bundle. 我建议Webpack可以同时使用sass-launcher,css-launcher和style-launcher来读取您的SCSS文件导入,将SASS转换为CSS,并将其内联到页面顶部的<style>元素中,或放入您的JS捆绑包中。

You can definitely use the ES2015 import statement to import SCSS files using Webpack 2 - I've just done it for a small project :) 您绝对可以使用Webpack 2使用ES2015 import语句导入SCSS文件-我刚刚为一个小型项目完成了它:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM