【问题标题】:Is it possible to init all Taghelpers before Processing it?是否可以在处理之前初始化所有 Taghelper?
【发布时间】:2018-03-07 04:13:13
【问题描述】:

我有一个 TagHelper,它在 Init 方法中创建上下文:

public class TabContext
{
    public bool HasExplicitActiveItem { get; set; }
    public bool HasActiveItem { get; set; }
}

在子 Taghelper 中,如果此 taghelper 的 Html 属性 (bool IsActive) 设置为 true,我将尝试设置 HasExplicitActiveItem

public override void Init(TagHelperContext context)
{
    //...
    if (IsActive)
    {
        _tabContext.HasExplicitActiveItem = true;
    }
}

现在,当 taghelper 初始化时,HasExplicitActiveItem 不正确,我想从第一个子 taghelper 设置 IsActive 状态:

public override void Process(TagHelperContext context, TagHelperOutput output)
{
    //...
    if (_tabContext.HasActiveItem)
    {
        IsActive = false;
    }
    else if (!_tabContext.HasExplicitActiveItem)
    {
        IsActive = true;
    }
    if (IsActive)
    {
        _tabContext.HasActiveItem = true;
    }
    //...
}

但是,这不起作用,因为每个子标签助手都会在下一个子标签助手初始化之前进行初始化和处理。

要调用它们,我使用父标签助手中的await output.GetChildContentAsync()

那么是否可以先初始化所有(直接)孩子,然后再处理呢?如果没有,有没有办法预扫描孩子的属性?


有疑问,我有一个 XY 问题,所需的标记是这样的:

<tab>
    <tab-pane />
    <tab-pane />
    <tab-pane is-active="true" />
    <tab-pane />
</tab>

如果没有设置is-active,我想在第一个tab-pane上设置is-active

【问题讨论】:

    标签: c# asp.net-core asp.net-core-tag-helpers


    【解决方案1】:

    我通过不输出孩子们的任何东西来解决了这个问题。

    我已将所有子数据映射到项目上下文中:

    public class TabPaneContext
    {
        public IHtmlContent Content { get; set; }
        public string Id { get; set; }
        public bool IsActive { get; set; }
        public string Title { get; set; }
    }
    

    main Context 只获取一个 Item Context 的 List:

    public class TabContext
    {
        public IList<TabPaneContext> Items { get; set; }
    }
    

    子 Taghelper 将实际内容保存到 Item Context 中:

    public class TabPaneTagHelper : TagHelper
    {
        private TabContext _tabContext;
        private TabPaneContext _tabPaneContext = new TabPaneContext();
    
        public string Id
        {
            get => _tabPaneContext.Id;
            set => _tabPaneContext.Id = value;
        }
        public bool IsActive
        {
            get => _tabPaneContext.IsActive;
            set => _tabPaneContext.IsActive = value;
        }
        public string Title
        {
            get => _tabPaneContext.Title;
            set => _tabPaneContext.Title = value;
        }
    
        public override void Init(TagHelperContext context)
        {
            _tabContext = context.Items[typeof(TabContext)];
            _tabContext.Items.Add(_tabPaneContext);
        }
    
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            _tabPaneContext.Content = await output.GetChildContentAsync();
            output.SuppressOutput();
        }
    

    输出的实际生成是由主 taghelper 通过迭代 Items 来完成的。

    不确定它的效率如何,但它正在工作。感觉还是有点脏。

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-06
      • 2011-12-12
      • 2017-03-13
      相关资源
      最近更新 更多