Razor 组件与视图组件

Razor components vs View Components

我最近从 Asp .NET Core 2.2 升级到 3.1,可以在 Razor 页面中使用 Razor 组件。它们看起来基本上类似于 View Components,只是它使用 blazor 和同一文件中的所有内容。我尝试搜索,但我没有阅读任何关于使用 Razor Components 而不是 View Components 的文档。

所以问题来了...

在 Razor 页面中使用 Razor 组件而不是视图组件有什么意义?这个会比较好吗?我知道视图组件设置起来有点痛苦,因为您必须设置一个默认视图和一个带有逻辑的代码隐藏文件,并且您不能在其上使用 javascript,只能在父视图上使用。我的大多数应用程序都充满了视图组件,我想知道切换到剃须刀组件有什么优势?

虽然 View Components are more like partial views but asynchronous, DI friendly, and kind of independent. They are mostly for view-only purposes (yeah it's possible to add some update logic via AJAX 但它更像是 hack 而不是 feature) 并且主要用于重复使用常见的 UI 单元,例如导航、登录块、边栏等

Razor Components are sort of "full featured" UI components, which can be easily nested, pass parameters through the components hierarchy, raise and handle UI events, and so on. So you can easily use them to manage your app UI, or even create custom UI component libraries