简体   繁体   English

VueJS - 将数据中的全局变量共享到内部 scss 文件(动态 CSS)

[英]VueJS - Share global variable from data to internal scss file (DYNAMIC CSS)

I have a master component named App.vue and in that file I am calling an ajax request to the server which returns JSON of colors.我有一个名为App.vue的主组件,在该文件中,我向服务器调用了一个 ajax 请求,该请求返回颜色的 JSON。

My ajax call is located inside of the vue life cycle hook named created .我的 ajax 调用位于名为created的 vue 生命周期钩子内。 When the ajax is done, it will populate the local data base on the result. ajax 完成后,它将根据结果填充本地数据库。

In the same file, I have style written in SCSS.在同一个文件中,我用 SCSS 编写了样式。

<style lang="scss">
$primary: #ff2000;
$secondary: #000111;
</style>

Is it possible to share to this SCSS file block those JSON of colors from the state of the components?是否可以将这些颜色的 JSON 与组件状态共享到此 SCSS 文件中? Please imagine the below image attached.请想象下面的图片。

在此处输入图片说明

** CORRECTIONS ** **更正**

  1. this.colors = colors.data this.colors = 颜色.数据

  2. .color-secondary { background: $secondary; .color-secondary { 背景:$secondary; } }

The colors are dynamic that is why it is coming from a database.颜色是动态的,这就是它来自数据库的原因。

Thanks in advance.提前致谢。

I think I nailed it!!我想我钉了它!! But I'm not quite sure if it is recommended or not.但我不太确定是否推荐。 I don't even know yet the up and down side of doing this tweak.我什至不知道做这个调整的好处和坏处。 But it did work!但它确实奏效了! :) :)

created() {
  // assume that this colors are coming from an async ajax call
  let colors = {
    primary: 'red',
    secondary: 'blue'
  }

  this.setThemeColors(colors);
},
methods: {
  setThemeColors(colors) {
    let style = document.documentElement.style;

    for (let key of Object.keys(colors)) {
      style.setProperty("--theme-color-" + key, colors[key]);
    }
  }
}

It's all set up from the root of your stylesheet.这一切都是从样式表的根设置的。 Check below to use it,检查以下以使用它,

<style lang="scss">
$color-primary: var(--theme-color-primary);
$color-secondary: var(--theme-color-secondary);

.color-primary {
  color: var(--theme-color-primary)
}
.color-secondary {
  color: var(--theme-color-secondary)
}
</style>

But another problem encountered, css variables is not working in IE11 and below!但是又遇到了一个问题,css变量在IE11及以下版本不起作用!

It is not possible to update the values of SCSS variables dynamically like you want to.不可能像您想要的那样动态更新 SCSS 变量的值。 Web browsers do not understand SCSS, only CSS. Web 浏览器不了解 SCSS,只了解 CSS。 SCSS is compiled into CSS ahead of time; SCSS提前编译成CSS; the variables get compiled away.变量被编译掉。

Somewhat recently, CSS variables have become a thing, so maybe that could be something that you can consider looking into.最近, CSS 变量已经成为一种东西,所以也许你可以考虑研究一下。

Other than that, you don't generally make dynamic updates to stylesheets.除此之外,您通常不会对样式表进行动态更新。 Perhaps some kind of CSS-in-JS solution might be what you're looking for.也许某种CSS-in-JS解决方案可能是您正在寻找的。

The key feature of SCSS is that it compiles readable / less bloated CSS into normal CSS. SCSS 的关键特性是它将可读/不那么臃肿的 CSS 编译成普通的 CSS。 Adjusting the styles on-the-fly inside a single file component is not possible.在单个文件组件中即时调整样式是不可能的。

See this question for possible workarounds: https://stackoverflow.com/a/48201646/6803592有关可能的解决方法,请参阅此问题: https : //stackoverflow.com/a/48201646/6803592

If you're looking for a solution, you could possibly get the colors before your vue app loads.如果您正在寻找解决方案,您可能会vue 应用程序加载之前获得颜色。

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

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