2017-07-25 95 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

相關問題