【问题标题】:View component taghelper with areas查看带有区域的组件 taghelper
【发布时间】:2018-10-19 12:24:24
【问题描述】:

我想拥有两个同名但位于不同区域的组件。

我的组件叫做 C1

两个区域分别是A1和A2

一个视图组件类存在于每个对应的命名空间中

  • ./Areas/a1/c1ViewComponent.cs 文件中的 MyProject.Areas.A1.ViewComponents
  • ./Areas/a2/c1ViewComponent.cs 文件中的 MyProject.Areas.A2.ViewComponents

代码编译;但是,当我尝试将组件添加到带有标签的页面时:

<div class="component-container c1" id="system-a1-c1">
  <vc:c1></vc:c1>
</div>

无法区分这两个组件,因为它们具有相同的名称并且无法解析路径

所以我看过/阅读了很多关于 taghelpers 的内容,但似乎没有提到如何在区域中使用它们

  1. 能达到我想要的效果吗?
  2. 为了实现这一目标需要进行哪些更改?

【问题讨论】:

    标签: c# asp.net-core asp.net-core-viewcomponent


    【解决方案1】:

    有两种方法可以引用ViewComponent

    • 通过@Component.InvokeAsync 调用组件
    • 调用视图组件作为标签助手

    对于&lt;vc:c1&gt;&lt;/vc:c1&gt;,您无法指定哪个视图组件,这意味着它将无法知道要使用哪个视图组件。

    您可以尝试@Component.InvokeAsync,它可以通过type 引用 ViewComponent。

    @await Component.InvokeAsync(typeof(MVCPro.Areas.Admin.ShareViewComponent))
    @await Component.InvokeAsync(typeof(MVCPro.Areas.User.ShareViewComponent))
    

    【讨论】:

      猜你喜欢
      • 2017-01-31
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多