2017-07-25 41 views
0

我有代码分割成不同的文件作为例如(简化版本):如何检查父元素是否包含某个标签? - 聚合物

管理员-page.html中

<paper-tabs selected={{selected}} attr-for-selecton="name"> 
    <paper-tab name="User">User</paper-tab> 
    ... 
    ... 
</paper-tabs> 
<iron-pages selected={{selected}} attr-for-selection="name"> 
    <admin-user-page name="User"></admin-user-page> 
    ... 
    ... 
</iron-pages> 

管理员用户-page.html中

<!-- general page content... --> 

// Javascript 
//-------------- 
// here I want to have an if statement for whether the parent (user-page.html) 
// contains the <paper-tabs> tag. 

请记住,我是在对此进行PolymerJS解决方案之后,是否有任何方法检测是否存在于admin-page.html from admin-user-page.html file?

+2

你为什么想这样做?难道你不能通过一个标志或东西到你的管理用户页面组件?我觉得有点优雅 – dloeda

+0

我几乎不知道你的意思是什么XD – physicsboy

回答

2

那么,在您的子组件(admin-user-page)中,您可以使用this.parentNode来访问您的父组件(admin-page),然后检查是否存在paper-tabs标签。所以它看起来像这样:

Polymer('admin-user-page', { 
    ready: function(){ 
    // you have to use parentNode twice 
    // this.parentNode is the iron-pages 
    // this.parentNode.parentNode is the admin-page 
    if(this.parentNode.parentNode.querySelector('paper-tabs')){ 
     // paper-tabs tag exists in parent 
    } 
    } 
}); 

虽然它不是一个优雅的解决方案。但我希望能回答你的问题。

+0

我会给它一个去,并报告西蒙!感谢您的输入:-) – physicsboy

相关问题