javascript - Vue.js/NuxtJS - 如何创建具有可通过 JSON 配置文件自定义的默认设计的组件
问题描述
我正在开发一个 NuxtJS 网站,默认情况下页面/组件可以具有通用设计,或者可以由客户端自定义,并且将在 url 中指定。
仅此而已:
http://localhost:3000/registration
- 对于通用页面
http://localhost:3000/client-name/registration
- 客户特定页面
为了实现这个目标,我为每个客户端(比如说client-name.json
)有一个 JSON 配置文件,它具有这种结构。
{
"some_configuration_property": {},
"another_configuration_property": {},
"design": {
"logoUrl": "/assets/client-name/logo.png",
"backgroundColor": "#000000",
"primaryColor": "#ffffff",
"secondaryColor": "#ffff00"
},
}
首先,我实现了路由系统,并且可以<script>
在 setup 方法中(我使用@nuxt/composition-api)根据当前路由(在该路由的 Vue 文件的标签内)成功读取每个客户端的配置。
我现在面临的问题是弄清楚如何将这些“设计变量”传递到<style>
我使用 SCSS 的 Vue 文件的标签中。我想要实现的行为是为特定组件/页面提供默认设计,但这可能会被每个客户端特定的这些“设计变量”覆盖。
- 我想到的第一件事是使用 CSS 变量,这将允许我创建具有默认值的变量,但我可以在样式中覆盖这些变量。我创建了一个用于测试的示例组件,它与 CSS 属性和 v-deep 伪元素一起使用。但是,这意味着我必须在自定义组件中为每个客户端创建一个类,而这正是我想要避免的。我首先想到了这种方法,因为它会给我很大的灵活性来选择如何在样式中使用这些设计颜色。
例子:
// From the customizable component
.my-button {
color: (--button-color, teal);
}
// Styling from a parent component/view
// Had to create a selector with a style like <div> for superior specificity though, not so clean
v::deep {
div {
&.my-button {
--button-color: purple;
}
}
}
我见过
/deep/
选择器或::v-deep
伪选择器,但我认为这不是一个非常干净的解决方案,因为它会在代码库中大量使用。来自父母的样式组件会使代码难以维护。另一种方法可能是传递一个变量,
classArray
例如,在 setup 方法中动态绑定 DOM 元素上的 CSS 类。虽然,为每个客户端创建一个带有关联样式的 CSS 类太麻烦了。
像这样:
<template>
<my-button :class="classArray"></my-button>
</template>
<script lang="ts">
import { defineComponent } from '@nuxtjs/composition-api'
export default defineComponent({
name: 'MyPage',
setup() {
const clientName = 'someClientName';
const classArray = [clientName]
return { classArray };
},
})
</script>
<style lang="scss" scoped>
.someClientName {
// some custom styles
}
</style>
在这种情况下你会采取什么方法?
感谢帮助!
解决方案
如果需要在运行时加载自定义主题配置,则需要使用 CSS 变量(属性)。它们可以包装在 SCSS 函数中并具有默认主题回退:
// theme.scss
$primaryColor: #abc;
// $buttonColor: $primaryColor
@function primaryColor() {
@return #{var(--primary-color, $primaryColor)}
}
@function buttonColor() {
@return #{var(--button-color, primaryColor())}
}
然后primaryColor()
, etc 被使用而不是直接使用$primaryColor
, etc 就像在常规 SCSS 主题中所做的那样:
// From the customizable component
.my-button {
color: buttonColor();
}
自定义主题可以在加载到整个文档或它的一部分(组件的层次结构)时应用,这些文档应该受自定义主题的影响:
const config = await loadClientConfig();
document.documentElement.style.setProperty(--primary-color, config.design.primaryColor)
推荐阅读
- godot - 如何修复“无效的获取索引'位置'(基于:Nil)”。错误
- r - 如何防止林地被砍伐?
- java - Switch/ToggleSwitch 状态以命令 IO 状态 w/ React
- python - 即使输出正确,Hackerrank 测试用例也会失败
- javascript - 如何从 safari 中删除基本 gatsby 样式(紫色 bg)
- appkit - WidgetKit:从小部件到 macOS 应用程序的深层链接
- javascript - 如何正确使用 put 方法 ANGULAR
- javascript - 为什么这个值在循环结束时返回 true,然后在循环开始时返回 false?
- clojure - 在列表的第 N 个位置插入一个新元素
- performance - WebGL 中的 AR 应用程序性能很差