access SASS values ($colors from variables.scss) in Typescript (Angular2 ionic2)

后端 未结 6 1212
醉梦人生
醉梦人生 2020-11-27 05:51

In Ionic 2, I would like to access the $colors variables from the file \"[my project]\\src\\theme\\variables.scss\".

This file contains:



        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 06:13

    I would like to add up something to @mete-cantimur answer.

    import {Component, OnInit, ViewEncapsulation} from '@angular/core';
    
    const PREFIX = '--';
    
    @Component({
      selector: 'app-styles-helper',
      templateUrl: './styles-helper.component.html',
      styleUrls: ['./styles-helper.component.scss'],
      encapsulation: ViewEncapsulation.None
    })
    export class StylesHelperComponent implements OnInit {
    
      ngOnInit(): void {
    
      }
    
      readProperty(name: string): string {
        const bodyStyles = window.getComputedStyle(document.body);
        return bodyStyles.getPropertyValue(PREFIX + name);
      }
    }
    

    My helper component wasn't being able to modify body styles. Even I set up everything correctly, custom properties were not being saved.

    I had to add encapsulation: ViewEncapsulation.None to the component in order to let it modify body styles.

    Hope this helps.

提交回复
热议问题