在作为标签帮助程序调用的视图组件中,我们如何访问内部HTML? [英] In a view component invoked as a tag helper, how can we access the inner HTML?

查看:110
本文介绍了在作为标签帮助程序调用的视图组件中,我们如何访问内部HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标签助手中,我们可以访问内部HTML的标签.

In tag helpers, we can access the tags inner HTML.

<!-- Index.cshtml -->  
<my-first>
    This is the original Inner HTML from the *.cshtml file.
</my-first>

// MyFirstTagHelper.cs > ProcessAsync
var childContent = await output.GetChildContentAsync();
var innerHtml = childContent.GetContent();

如果我们

If we invoke a view component as a tag helper, how can we access the inner HTML?

<vc:my-first>
    This is the original Inner HTML from the *.cshtml file.
</vc:my-first>

// MyFirstViewComponent.cs > InvokeAsync()
var innerHtml = DoMagic();

进一步思考:

我很欣赏我们可以通过HTML属性将任意内容传递给视图组件.但是,对于大量文本而言,这可能变得不切实际,在这种情况下,内部HTML更具可读性并具有更好的工具支持.

I appreciate that we can pass arbitrary content to a view component via HTML attributes. That can become impractical, though, in the case of very large amounts of text, in which case inner HTML would be more readable and have better tooling support.

有人可能会说:那为什么不只使用标签助手呢?好吧,我们希望将视图与标签帮助程序相关联,并且标签帮助程序不支持视图.相反,标签助手需要我们以编程方式构建所有HTML.

Some might also say, "Why not just use a tag helper, then?" Well, we want to associate a view with the tag helper, and tag helpers do not support views; instead, tag helpers require us to build all the HTML programmatically.

因此,我们陷入了束缚.一方面,我们需要标签帮助器,但它们不支持视图.另一方面,我们可以使用view组件作为标签助手,但是view组件可能无法让我们访问内部HTML.

So we're stuck in a bit of bind. On the one hand, we want a tag helper, but they don't support views; on the other hand, we can use a view component as a tag helper, but view components might not let us access the inner HTML.

推荐答案

对不起,答案是只使用标记助手"

Sorry, but the answer is "Just use a tag helper"

请参阅以下问题: https://github.com/aspnet/Mvc/issues/5465

能够从标签助手中调用ViewComponent是调用它的另一种方式,而不是使用@Component.Invoke().

Being able to invoke a ViewComponent from a tag helper is just a different way to call it instead of using @Component.Invoke().

我可以看到这有误导之处,并且我确实记得在ASP.NET Core 2.1教程中看到了这样一个声明:查看组件就像Tag Helpers,但功能更强大."

I can see where this is misleading, and I do recall seeing in the ASP.NET Core 2.1 tutorials a statement to the effect of "View Components are like Tag Helpers, but more powerful."

这篇关于在作为标签帮助程序调用的视图组件中,我们如何访问内部HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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