剃刀组件与视图组件 [英] Razor components vs View Components

查看:116
本文介绍了剃刀组件与视图组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I recently upgraded from Asp .NET Core 2.2 to 3.1 and can use Razor Components in razor pages. They look basically like View Components except it uses blazor and everything on the same file. I tried to search but i haven't read anything on documentation that says to use Razor Components instead View Components.

这是问题所在...

在Razor页面中使用Razor组件而不是View组件有什么意义?这个会比较好吗?我知道视图组件的设置有点麻烦,因为您必须设置默认视图和带有逻辑的文件后面的代码,并且不能仅在父视图上使用javascript.我的大多数应用程序都装有View组件,我想知道切换到剃须刀组件有什么优势?

What is the point of using Razor Components instead View Components in Razor Pages? Is it better? I know View Components are a bit painful to set because you have to set a default view and a code behind file with the logic and you cannot use javascript on it, only on the parent view. Most of my apps are full of View Components and I was wondering what are the advantages of switching to razor components?

推荐答案

视图组件更像是局部视图,但是异步 DI 友好且独立.它们主要用于仅查看的目的(是的,可以通过

While 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 but it's more like a hack rather than a feature) and are used mostly to re-use common UI units like navigation, login block, sidebar, etc.

剃刀组件属于功能齐全"的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.

这篇关于剃刀组件与视图组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆