聚合物3.x中聚合物1.x的“行为”对应项是什么

时间:2019-05-07 07:48:08

标签: polymer polymer-2.x polymer-3.x

我正在将行为成分从聚合物1.x转换为聚合物3.x。我关心的是如何转换我的行为类以用于Polymer 3应用程序。测试代码如下:

PermissionsBehaviour =
{
    ready: function ()
    {
        var _th = this;
        this._getRoles().then(function (d)
        {
            _th.set('perms_roles', d);          
        });
    },

    properties:
    {
         perms_roles: { type: Array, value: [] },          
    },

    _getRoles: function ()
    {
        return $.get(Global.Settings.RootWebUrl +                                                     
                       "Permission/GetUserRoles", function (result) { });
    }
}

1 个答案:

答案 0 :(得分:1)

您可以在聚合物3中将其定义为mixin:https://polymer-library.polymer-project.org/3.0/docs/devguide/custom-elements#defining-mixins

import {dedupingMixin} from '@polymer/polymer/lib/utils/mixin.js';

let internalMyMixin = function(superClass) {
  return class extends superClass {
    constructor() {
      super();
      this.addEventListener('keypress', (e) => this._handlePress(e));
    }

    static get properties() {
      return {
        bar: {
          type: Object
        }
      };
    }

    static get observers() {
      return [ '_barChanged(bar.*)' ];
    }

    _barChanged(bar) { ... }

    _handlePress(e) { console.log('key pressed: ' + e.charCode); }
  }
}

const MyMixin = dedupingMixin(internalMyMixin);
export {MyMixin as default};

然后按如下所示使用mixin:

import {MyMixin} from './my-mixin.js';

class Foo extends MyMixin(PolymerElement) { ... }