简体   繁体   English

将配色方案JSON列表添加到Angular 8环境中的Scss文件

[英]Add Color Scheme JSON list to Scss file in Angular 8 Environment

I'm building a Web based project with Angular 8. At the starting of the Application, I need to retrieve a list of predefined colour scheme defined in a JSON file and I have to add them dynamically to the Application. 我正在使用Angular 8构建一个基于Web的项目。在应用程序启动时,我需要检索一个在JSON文件中定义的预定义配色方案的列表,并且必须将它们动态添加到应用程序中。 The colour scheme JSON looks like as below: 配色方案JSON如下所示:

    {
      "bar": "#FFFFFF",
      "text_small": "#FFFFFF",
      "text_nav_header": "#FFFFFF"
       ............ ...........
       ........... ...........
    }

I have gone through few links as below but somehow I'm missing the point to import this JSON to the SCSS processing. 我浏览了以下几个链接,但是不知何故我错过了将此JSON导入SCSS处理的要点。

https://scotch.io/tutorials/angular-shortcut-to-importing-styles-files-in-components https://scotch.io/tutorials/angular-shortcut-to-importing-styles-files-in-components

https://medium.com/@dmitriy.borodiy/easy-color-theming-with-scss-bc38fd5734d1 https://medium.com/@dmitriy.borodiy/easy-color-theming-with-scss-bc38fd5734d1

https://css-tricks.com/making-sass-talk-to-javascript-with-json/ https://css-tricks.com/making-sass-talk-to-javascript-with-json/

https://www.viget.com/articles/sharing-data-between-sass-and-javascript-with-json/ https://www.viget.com/articles/sharing-data-between-sass-and-javascript-with-json/

Please refer to the below link that I want to achieve with SCSS 请参考以下我要使用SCSS实现的链接

Sample code with gist on SassMeister. SassMeister上带有gist的示例代码。

Can somebody through some light that what I'm missing here? 有人可以说出我在这里想念的东西吗? Also, I'm not an expect in CSS but can understand the basic use of the CSS and media queries. 另外,我不是CSS的期望者,但可以理解CSS和媒体查询的基本用法。

Thanks in Advance 提前致谢

Are you sure that these color variables have to be inside a json file? 您确定这些颜色变量必须在json文件中吗? If the values are generated: then change the output format to a scss file. 如果生成了值:然后将输出格式更改为scss文件。 If the values are defined once (or not very often) : copy/paste them to a scss file. 如果值定义一次(或不是很经常定义):将它们复制/粘贴到scss文件中。

Here are the steps that I work out with the Angular and backend script to achieve it. 这是我使用Angular和后端脚本来实现它的步骤。

After Angular App is started. Angular App启动后。 I downloaded the JSON in the background and written a file with few changes as mentioned below. 我在后台下载了JSON,并编写了一个文件,进行了一些更改,如下所述。

  • Convert the JSON to JSONString. 将JSON转换为JSONString。
  • Add a string ':root' before the JSONString. 在JSONString之前添加字符串':root'

    In Python: 在Python中:

     data = ":root %s" % jsonString 

    In PHP: 在PHP中:

     $data = ":root ". $jsonString; 
  • Replace few character in the string as below: 替换字符串中的几个字符,如下所示:

    In Python 在Python中

     data = data.replace('\\"', '') data = data.replace(',', ';') 

    In PHP: 在PHP中:

     $data = str_replace('\\"', '', $data); $data = str_replace(',', ';', $data); 
  • Write a file to the src folder of the Angular project. 将文件写入Angular项目的src文件夹。 In Python: 在Python中:

     f = open("src/themes.scss", "w+") f.write(data) f.close() 

    In PHP: 在PHP中:

     $fp = fopen('src/themes.scss', 'w'); fwrite($fp, $data); fclose($fp); 
  • Import the filename.scss in your styles.css file of your Angular project. filename.scss导入到Angular项目的styles.css文件中。

     @import "themes.scss"; 

Thus how I have generated the dynamic scss file with a JSON of color codes. 因此,我如何用颜色代码的JSON生成dynamic scss file It look weird but yes, it works like a charm. 看起来很奇怪,但是可以,它就像一种魅力。

Note : It works in production environment of Angular as well 注意 :它也可以在Angular的生产环境中使用

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

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