聚合物 3.x 中聚合物 1.x 的 'behavior' 对应物是什么

What is the 'behavior' counterpart of Polymer 1.x in polymer 3.x

我正在将行为组件从 Polymer 1.x 翻译成 Polymer 3.x。我关心的是如何翻译我的行为 class 以用于 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) { });
    }
}

你可以在Polymer 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) { ... }