2016-11-17 110 views
0

我試圖將應用路由的參數傳遞給未定義的自定義元素。我可以在文件中顯示該參數,但在自定義元素中,它沒有定義。以下是我的代碼,請幫助我檢查如何修復它。謝謝。聚合物通過自定義元素的路由數據未定義

在自定義元素文件中,getid沒有定義。

我的文件

<app-route 
    route="[[route]]" 
    pattern="/:slug/:id" 
    data="{{routeData}}"></app-route> 

    <!-- display correctly --> 
    [[routeData.id]] 

<myelement-http-get 
    id="getCategoryData" 
    getid="[[routeData.id]]" 
    geturl="http://localhost:9000/polymer/category/" 
    jsonobject="{{jsonobject}}" 
    failure="{{failure}}"></myelement-http-get> 

自定義元素

<script> 
    Polymer({ 
     is: 'myelement-http-get', 

     properties: { 
     geturl: String, 

     getid: String, 

     jsonobject: { 
      type: Object, 
      notify: true 
     }, 

     failure: { 
      type: Boolean, 
      notify: true, 
      readOnly: true 
     } 

     }, 

     ready: function() { 
     /* undefined found this.getid */ 
     this.$.ajaxData.url = this.geturl + this.getid; 
     this.$.ajaxData.generateRequest(); 
     }, 

     handleResponse: function (data) { 
     this.jsonobject = data.detail.response; 
     } 
    }); 
</script> 

回答

1

的數據綁定getidgeturl效應發生ready後的回調,所以這不是你會想操縱這些屬性。

相反,您應該使用complex observer來觀察getidgeturl。只有在定義和更改了這兩個屬性時,纔會調用此觀察者(此行爲在Polymer 2.0中稍有變化)。

你應該從ready刪除更改,並添加複雜的觀察者像如下所示:

​​
+0

感謝,這是部分正確。但是'觀察者:['_generateRequest(geturl,getid)']'當我得到第三頁時不能更新。 'geturl'總是顯示第二頁網址。 – ppshein

相關問題