【问题标题】:Meteor destroy subTemplate流星破坏子模板
【发布时间】:2021-01-24 19:18:54
【问题描述】:

我有一个调用子模板的父模板。我想通过按一下按钮来杀死子模板。

父模板


    <template name="tracker">
         <span id="destroyChild" class="btn" title="destroyTable"></span>
    
         {{<child}}
    </template>

然后,我有一个事件来尝试并销毁子模板

'click span[id=destroyChild]'(e, template) {
            //This works to remove the Parent template
            //Blaze.remove(template.view);
 
           Blaze.remove('what do I put here?');
        },

我找不到任何可用作删除子模板的参数。我不断收到Uncaught Error: Expected template rendered with Blaze.render

我给子模板一个 ID 并尝试使用 selector 调用它,但没有运气。有任何想法吗?谢谢!

【问题讨论】:

  • 不渲染到DOM的时候,child不是自动销毁了吗?
  • 我想是这样,但你的目的是什么?
  • 我假设在你的代码中{{&lt;child}} 实际上是{{&gt; child}},对吧?
  • 在使用 Meteor 的五年里,我从来没有打电话给 Blaze.remove,就像 Jan 建议的那样,你可能也不需要。您可能应该找到一种被动的方式来执行此操作,即,使用您在单击按钮时更改的条件来保护子项的包含。

标签: javascript node.js templates meteor meteor-blaze


【解决方案1】:

您不应该强制修改您的布局。 Blaze 是一个反应式引擎,所以一切都应该以声明方式决定,例如:

<template name="tracker">
   <span id="destroyChild" class="btn" title="destroyTable"></span>

   {{#if show}}
   {{> child}}
   {{/if}}
</template>
Session.setDefault('show', true);
Template.tracker.helpers({
  show() {
    return Session.get('show');
  }
});

...

  'click span[id=destroyChild]'(e, template) {
     Session.set('show', false);
  },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-13
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多