7 javascript dom polymer polymer-1.0
我尝试通过模板重复使用滑动页面.
   <swipe-pages>
        <template is="dom-repeat" items="{{values}}">
          <swipe-page>
               <div>
                   Text to swipe
               </div>
          </swipe-page>
         </template>
   </swipe-pages>
在我写的聚合物中
created: function()  {
        console.log(this);
        this.values = [1,2,3];
       }
它给了我错误
Uncaught TypeError: Cannot set property 'values' of undefined
  Polymer.created   
  Polymer.Base._addFeature._invokeBehavior  
  Polymer.Base._addFeature._doBehavior  
  Polymer.Base.createdCallback  
  window.Polymer    
  (anonymous function)  
  Polymer.DomApi.DomApi._addNode
我不能让它工作.
也用
ready:function(){this.values=[1,2,3];};
不起作用.在这种情况下,它会抛出异常,即它们是0页.
我认为在模板重复运行后,滑动页面不会收到输入.
如果我不是通过模板写它,它可以正常工作..
更新:
这是所有的聚合物元素.
<dom-module id="element-element">
          <template>
              <swipe-pages>
                <template is="dom-repeat" items="{{values}}">
                   <swipe-page>
                     <div>
                        text
                     </div>
                   </swipe-page>
               </template>
              </swipe-pages>
           </template>
           <script>
               Polymer({
                    is:'element-element',
                    created: function()  {
                        this.values = [1,2,3];
                    },
                    ready: function(){
                        this.values=[1,2,3];
                    }
                });
            </script>
</dom-module>
如果它们是聚合物的另一个滑动页面元素,可以动态改变,我将很高兴知道.
如果他们是一个黑客解决方案(如动态加载所有元素),它也可以
谢谢.
一个快速修复方法是修改 的swipe-pages源代码。
首先,找到该selectedChanged函数并在最顶部添加此检查 -
selectedChanged: function (newValue, oldValue) {
    if (oldValue === undefined) {
        return;
    }
    if (newValue >= this.pageCount || newValue < 0) {
        ....
然后,因为resetPositions和resetWidths被调用得太早,你想替换以下内容
ready: function () {
    this.resetPositions();
    this.resetWidths();
}
和
attached: function () {
    this.async(function () {
        this.resetPositions();
        this.resetWidths();
    }, 5);
确保页面已经通过 呈现dom-repeat。
最后,确保在 或 内初始化ready数组attached。
ready: function()  {
    console.log(this);
    this.values = [1,2,3];
}
应该这样做!
| 归档时间: | 
 | 
| 查看次数: | 357 次 | 
| 最近记录: |