【问题标题】:Defining Polymer computed property based on an array mutation event and another property基于数组突变事件和另一个属性定义 Polymer 计算属性
【发布时间】:2016-12-05 07:15:21
【问题描述】:

我正在尝试定义一个计算聚合物属性,该属性取决于数组突变事件和另一个自定义属性。由于某种原因,聚合物没有调用计算函数,但是当我删除两个依赖项中的一个时,一切都像魅力一样工作。

item: {
    type: Object,
    value: {
        votes: []
    }
},
authId: {
    type: String,
    value: '123'
},
voted: {
    type: Boolean,
    notify: true,
    computed: 'computeVoted(item.votes.*, authId)'
}

编辑:由于一些误解而更新了示例

【问题讨论】:

    标签: javascript polymer


    【解决方案1】:

    对我来说很好用。你发现下面sn-p的不同了吗?

    Polymer({
      is: 'my-elem',
      
      properties: {
        prop: {
          type: String,
          value: 'prop'
        },
        array: {
          type: Object,
          value: {
            items: []
          }
        },
        computed: {
          computed: 'compute(array.items.*, prop)'
        }
      },
      
      mutateArr: function() {
        this.push('array.items', 'arr');
      },
      
      compute: function(arr, prop) {
        return prop + ';' + arr.base.length;
      }
    });
    <!DOCTYPE html>
    <html>
    <head>
      <base href="https://polygit.org/components/">
      <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
      <link href="polymer/polymer.html" rel="import"/>
    </head>
    
    <body>
      <my-elem></my-elem>
      
      <dom-module id="my-elem">
        <template>
          <input type="button" value="Mutate array" on-tap="mutateArr" />
          <input type="text" value="{{prop::input}}" />
          <br/>
          <span>{{computed}}</span>
        </template>
      </dom-module>
    
    </body>
    </html>

    【讨论】:

    • 不完全一样,我在听array.items.*(在你的例子中)
    • 你是对的。我修复了 sn-p 并且仍然可以工作:)
    【解决方案2】:

    这是您要实现的目标的运行示例。始终建议初始化设置了observers 或comptuted 的值。

    <base href="http://polygit.org/components/">
    <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
    <link rel="import" href="polymer/polymer.html">
    
    <dom-module id="button-group">
      <template>
        <style></style>
        <div on-tap='changeAuthId'>Change AuthId</div>
        <div on-tap='changeItem'>Change item</div>
        {{voted}}
      </template>
    </dom-module>
    <script>
      Polymer({
        is: 'button-group',
        properties: {
          item: {
            type: Object,
            value: function() {
              return {
                votes: [{
                  one: 'one'
                }, {
                  two: 'two'
                }]
              }
            }
          },
          authId: {
            type: String,
            value: 'test'
          },
          voted: {
            type: String,
            notify: true,
            computed: 'computeVoted(item.votes.*,authId)'
          }
        },
        computeVoted: function(firstVal, authId) {
          //firstVal will be an Object containing three variables 
          //base: which will have item.voted object
          //path: which will contain path of changed value/s
          //value: which will contain new value/s
          return 'Path for changed value: ' + firstVal.path + ' AuthId: ' + authId
        },
        changeAuthId: function() {
          this.authId = "new id"
        },
        changeItem: function() {
          var three = {
            three: 'three'
          };
          this.set('item.votes.0', three);
        }
      });
    </script>
    
    
    <button-group></button-group>

    【讨论】:

    • votes 内的 item 属性应该是一个数组
    • 即使你有一个字符串数组而不是对象,如上面的 sn-p 所示,它也可以工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 2019-09-02
    • 2018-01-22
    • 1970-01-01
    • 2020-02-05
    • 2014-04-19
    • 2016-07-21
    相关资源
    最近更新 更多