(12/30) Learn Blazor together: Cascading values and parameters

(12/30) Learn Blazor together: Cascading values and parameters

Yesterday I accidentally changed the type of Reset button to button, and changed it back to reset today.

最后更新 12/15/2021 11:38 PM
StrayaWorker
预计阅读 3 分钟
分类
Blazor
专题
Learn the Blazor series together
标签
.NET C# ASP.NET Core Blazor

(Note: I accidentally changed the type of the Reset button to button yesterday, and changed it back to reset today.)

我们目前建立了 3 个组件,BlogPostMyButton,如果想让 3 个组件的字体颜色或是尺寸都一样,似乎得先在MyButton定义一个带有[Parameter] attribute 的变量,Post调用时再填入值,Post同样定义变量,Blog调用时再填入,每个环节都要这样做。

前面讲过父组件想将数据传递到子组件内的就是靠Parameter,有没有类似Parameter不过可以一次传递到底下所有组件的方法呢?

这时候就要用到CascadingValue这个组件了,cascading的意思是级联、喷泻,可以将其理解为由上而下注入,前端用的 CSS 全名就是Cascading Style Sheet(层级式样式表)。

我们先在BlogBase.razor.cs加入一个 Property ColorStyle(注:笔者之前文件取名有误,这篇改为 BlogBase.razor.cs),其值为"color: goldenrod"

接着在Blog.razor<CascadingValue Value="ColorStyle"><Post>包住。

再于 PostBase.razor.cs 定义同名变量,不过多了[CascadingParameter]attribute。

Then just fill in the variable where you want to apply it.

MyButton也是一样的用法。

虽然每个组件都还是要定义一个变量去承接最上层来的变量,不过可以看到[CascadingParameter]省去了在PostBase.razor调用<MyButton>必须填值的步骤。

那如果有两个值要这样层层传递呢?没错,就是包两层<CascadingValue>即可,但真的写了会发现,怎么会套用两个color style呢?

[Webmaster's note: There are differences between top and bottom. The above is the webmaster's debugging results, and the following is a screenshot of the original text]

因为<CascadingValue>认识的是变量类型,如果类型不同可以方便套用,但如果有两个string,则子元素只会套用最靠近的<CascadingValue>,以图片为例就是ColorStyle

为了解决这问题,可以使用里面的Name变量,如此一来组件之间就会认识这个<CascadingValue>的名字,如果子组件找不到该Name就会回传空白。

从上面的范例可以知道每次<CascadingValue>Blazor 都会层层通知下层组件,但如果不希望每次都通知占用资源呢?可以用IsFixed,将其设为 true,Blazor 就不会去通知子组件了。

** Quotes: **

  1. Blazor cascading values and parameters
  2. Blazor multiple cascading parameters
  3. Blazor cascading values performance

** Note: The code in this article is refactored through. NET 6 + Visual Studio 2022. You can click on the original link to compare and learn the refactored code. Thank you for reading and support the original author **

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 12/25/2021

(29/30) Everyone learn Blazor together: Blazor unit testing

Probably the most boring process of developing a system is to solve bugs, especially the error of trying to value null objects (`Object reference not set to an instance of an object.`). This should be the most common problem that most people encounter when they first step into the programming field. In order to relieve themselves from the boring process of solving bugs, this article introduces 'unit testing'.

继续阅读
同分类 / 同标签 12/25/2021

(28/30) Everyone learns Blazor together: Policy-based authorization

It was mentioned before that 'ASP.NET Core Identity' uses 'Claim' based authentication. In fact,'ASP.NET Core Identity' has different types of authorization methods, the simplest are 'login authorization','role authorization', and 'Claim authorization', but all of the above are implemented in one way: 'Policy-based authorization'.

继续阅读