简体   繁体   English

是否可以为多个组件创建一个 Blazor 范围的 CSS 文件而不复制它?

[英]Is it possible to create a Blazor scoped CSS file for multiple components without duplicating it?

Is it possible to create some sort of component scoped CSS file for multiple components without duplicating it?是否可以为多个组件创建某种组件范围的 CSS 文件而不复制它? Is there any naming convention?有什么命名约定吗?

Lets assume there are three components which all use the same tags but a style should only be applied for components of naming scheme ComponentA .让我们假设有三个组件都使用相同的标签,但样式应该只应用于命名方案ComponentA Other components should not be affected.其他组件不应受到影响。

ComponentA_A.razor
ComponentA_B.razor
ComponentOther.razor

having

ComponentA.razor.css

instead of代替

ComponentA_A.razor.css
ComponentA_B.razor.css

If duplication is the only way, is it possible to use some kind of referencing in css file itself or referencing a specific css file in blazor component?如果重复是唯一的方法,是否可以在 css 文件本身中使用某种引用或在 blazor 组件中引用特定的 css 文件?

I've found a workaround for this, using custom scope identifier format .我找到了一种解决方法,使用自定义范围标识符格式
It's not very elegant but you might consider it:它不是很优雅,但您可以考虑:

  • All .razor components need to have their respective .razor.css files:所有.razor组件都需要有各自的.razor.css文件:

     ComponentA.razor ↳ ComponentA.razor.css ComponentB.razor ↳ ComponentB.razor.css
  • One of the .razor.css files contains the css styles, the others can stay empty.其中.razor.css文件包含CSS样式,其他人可以留空。

  • In your .csproj file add the following item group:在您的.csproj文件中添加以下项目组:

     <ItemGroup> <None Update="Components\\ComponentA.razor.css" CssScope="my-custom-scope" /> <None Update="Components\\ComponentB.razor.css" CssScope="my-custom-scope" /> </ItemGroup>

This will create the same custom scope my-custom-scope for all components it's applied to, and thus will apply the same styles to them.这将为它应用到的所有组件创建相同的自定义范围my-custom-scope ,因此将对它们应用相同的样式。

Another alternative (might not be applicable to the OP but it's a way) is to use a preprocessor like SASS then you can create mixin s and variable s that are shared across components but you can also use pure CSS solution nowadays using custom properties (variables).另一种选择(可能不适用于 OP,但它是一种方法)是使用像 SASS 这样的预处理器,然后您可以创建跨组件共享的mixinvariable但现在您也可以使用自定义属性(变量)使用纯 CSS 解决方案)。

So just to give an example using pure CSS you can do something like the following:因此,仅举一个使用纯 CSS 的示例,您可以执行以下操作:

  1. In your _Host.cshtml file add a link to a global CSS file (in our example we gonna call it site.css ) like so:在您的_Host.cshtml文件中添加一个link全局 CSS 文件的link (在我们的示例中,我们将其称为site.css ),如下所示:
<head>
    ...
    <link href="css/site.css" rel="stylesheet" />
    <link href="BlazorWebApp.styles.css" rel="stylesheet" />
</head>

Note, that we added the link to site.css above BlazorWebApp.styles.css which is a generated file for the CSS, depends on the setup though.请注意,我们添加链接site.css以上BlazorWebApp.styles.css这对CSS生成文件,取决于安装虽然。

  1. Then you can define a CSS variable preferably in the :root but it can be anywhere, you can even create a dummy class called .variables and declare it there (cascading rules applies) but for the example we gonna stick with :root like so:然后你可以最好在:root定义一个 CSS 变量,但它可以在任何地方,你甚至可以创建一个名为.variables的虚拟类并在那里声明它(级联规则适用)但对于这个例子,我们将坚持使用:root像这样:
:root {
    --primary-color: #ff0000
}
  1. And then we can use --primary-color across components and we can declare yet another variable say --component-a-color and use that for components related to ComponentA like so:然后我们可以跨组件使用--primary-color ,我们可以声明另一个变量说--component-a-color并将其用于与ComponentA相关的ComponentA如下所示:

ComponentA_A.razor.css ComponentA_A.razor.css

div {
    --component-a-color: #00ff00
    color: var(--component-a-color);
    background-color: var(--primary-color);
}

ComponentA_B.razor.css ComponentA_B.razor.css

div {
    color: var(--component-a-color);
    background-color: var(--primary-color);
}

ComponentOther.razor.css ComponentOther.razor.css

div {
    color: #0000ff;
    background-color: var(--primary-color);
}

ps I'm using separate files for the CSS here but this shouldn't matter. ps 我在这里为 CSS 使用单独的文件,但这无关紧要。

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

相关问题 从一个数据源创建多个网格,而无需复制该数据源 - Create multiple grids from one data source without duplicating that source 在组件初始化之前使用异步方法初始化 Blazor 范围服务 - Initialize Blazor scoped service using async method before components are initialized Blazor - 如何动态创建组件 - Blazor - How to create Components dynamically 在 Blazor 服务器应用程序中,是否可以将 GraphServiceClient 注入范围服务? - In a Blazor Server app, is it possible to inject a GraphServiceClient into a scoped service? 是否可以在不更改 App.razor 的情况下从 RCL 加载 blazor 页面(不是组件)? - Is it possible to load blazor pages (not components) from RCL without changing App.razor? Blazor WebAssembly CSS 隔离范围身份不匹配 - Blazor WebAssembly CSS isolation scoped identities doesn't match 共享 CSS 文件 blazor - Shared CSS file blazor 用于 CSS 隔离的 blazor 组件中间的链接标签 - link tag in middle of blazor components for CSS isolation Blazor(服务器)范围内的 object 依赖注入创建多个实例 - Blazor (Server) scoped object in dependency injection creating multiple instances 如何在父组件中渲染多个子组件而不在 Blazor 中调用 @ChildContent - How to render multiple child components in parent component without calling @ChildContent in Blazor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM