在Ionic 2中,我想访问文件"[my project]\src\theme\variables.scss"中的$colors个变量.

此文件包含:

$colors: (
  primary:    #387ef5,
  secondary:  #32db64,
  danger:     #f53d3d,
  light:      #f4f4f4,
  dark:       #222,
  favorite:   #69BB7B
);

在一个组件中,我画了一幅画布.看起来是这样的:

import {Component, Input, ViewChild, ElementRef} from '@angular/core';

@Component({
    selector: 'my-graph',
})
@View({
    template: `<canvas #myGraph class='myGraph'
     [attr.width]='_size'
     [attr.height]='_size'></canvas>`,
})

export class MyGraphDiagram {
    private _size: number;

    // get the element with the #myGraph on it
    @ViewChild("myGraph") myGraph: ElementRef; 

    constructor(){
        this._size = 150;
    }

    ngAfterViewInit() { // wait for the view to init before using the element

      let context: CanvasRenderingContext2D = this.myGraph.nativeElement.getContext("2d");
      // HERE THE COLOR IS DEFINED AND I D LIKE TO ACCESS variable.scss TO DO THAT
      context.fillStyle = 'blue';
      context.fillRect(10, 10, 150, 150);
    }

}

正如你所看到的,在这个代码的某个点上,形状的 colored颜色 被定义为:context.fillStyle = 'blue',我想用context.fillStyle = '[variables.scss OBJECT].$colors.primary '来代替.

有人有主意吗?

推荐答案

不幸的是,无法直接从typescript/javascript代码访问SASS变量.然而,我们可以找到一个解决方法来访问这些变量.

让我简要描述一下从typescript源代码中访问SASS变量的步骤:

1.创建SASS帮助器组件

创建../providers/sass-helper/sass-helper.component.scss个:

$prefix: "--"; //Prefix string for custom CSS properties

//Merges a variable name with $prefix
@function custom-property-name($name) {
    @return $prefix + $name;
}

// Defines a custom property
@mixin define-custom-property($name, $value) {
    #{custom-property-name($name)}: $value;
}

body {
    // Append pre-defined colors in $colors:
    @each $name, $value in $colors {
        @include define-custom-property($name, $value);
    }

    // Append SASS variables which are desired to be accesible:
    @include define-custom-property('background-color', $background-color);
}

在这个SCSS文件中,我们只需在DOM的body部分中创建自定义属性.您应该使用名为define-custom-propertymixin将希望访问的每个SASS变量添加到此SCSS文件中,它需要两个参数:变量名和变量值.

例如,我为$colors中定义的所有colors添加了条目,并为theme/variables.scss文件中定义的SASS变量$background-color添加了条目.您可以添加任意数量的变量.

创建../providers/sass-helper/sass-helper.component.ts个:

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

export const PREFIX = '--';

@Component({
    selector: 'sass-helper',
    template: '<div></div>'
})
export class SassHelperComponent {

    constructor() {

    }

    // Read the custom property of body section with given name:
    readProperty(name: string): string {
        let bodyStyles = window.getComputedStyle(document.body);
        return bodyStyles.getPropertyValue(PREFIX + name);
    }
}

2.集成SASS辅助组件

从现在起,我们可以遵循标准Ionic2框架原则进行组件集成和使用.

  • 将组件类名(SassHelperComponent)添加到app.module.tsNgModule的声明部分
  • 将以下HTML代码插入页面的HTML模板,从中访问这些神奇变量:

    <sass-helper></sass-helper>


3.使用辅助组件

在页面的TS文件中,应在页面类中插入以下行:

@ViewChild(SassHelperComponent)
private sassHelper: SassHelperComponent;

最后,只需调用子类方法即可读取任何SASS变量的值,如下所示:

// Read $background-color:
this.sassHelper.readProperty('background-color');

// Read primary:
this.sassHelper.readProperty('primary');

Typescript相关问答推荐

脉轮ui打字脚本强制执行图标按钮的咏叹调标签-如何关闭

ReturnType此索引方法与点表示法之间的差异

有没有一种方法可以在保持类型的可选状态的同时更改类型?

TypeScrip 5.3和5.4-对`Readonly<;[...number[],字符串]>;`的测试版处理:有什么变化?

返回具有递归属性的泛型类型的泛型函数

如何将所有props传递给React中的组件?

类型脚本满足将布尔类型转换为文字.这正常吗?

在数据加载后,如何使用NgFor在使用异步管道的可观测对象上生成子组件?

如何在typescript中为this关键字设置上下文

编译TypeScrip项目的一部分导致错误

TypeScrip:从嵌套的对象值创建新类型

有没有什么方法可以使用ProvideState()提供多个削减器作为根减减器

如何处理可能为空的变量...我知道这不是Null?

为什么`map()`返回类型不能维护与输入相同数量的值?

在tabel管理区域react js上设置特定顺序

导航链接不触发自定义挂钩

为什么类方法参数可以比接口参数窄

如何使用 AWS CDK 扩展默认 ALB 控制器策略?

基于可选参数的动态类型泛型类型

Mongodb TypeError:类继承 events_1.EventEmitter 不是对象或 null