简体   繁体   English

AdonisJs:使用Hashids

[英]AdonisJs: Using Hashids

Had already someone used Hashids in AdonisJs? 已经有人在AdonisJs中使用Hashids了吗?

Being more specific, in a Model, to return a property hashid in an object 更具体的是,在Model中,返回对象中的属性hashid

I'm working on a migration from Laravel to Adonis. 我正在从Laravel迁移到Adonis。 In Laravel, it's possible just with a couple of code lines in each Model, like this: 在Laravel中,每个模型中只需要几行代码就可以了,如下所示:

use Hashids;

class Menu extends Model
{
    use \OwenIt\Auditing\Auditable;

    protected $appends = ['hashid'];

    public function getHashidAttribute()
    {
        return Hashids::encode($this->attributes['id']);
    }
}

I installed this NPM package: https://www.npmjs.com/package/adonis-hashids , and I tried to figure out how to use like the Laravel way 我安装了这个NPM软件包: https ://www.npmjs.com/package/adonis-hashids,我试着弄清楚如何使用Laravel方式

I'd used Computed Properties ( https://adonisjs.com/docs/4.1/database-getters-setters#_computed_properties ) 我使用过计算属性( https://adonisjs.com/docs/4.1/database-getters-setters#_computed_properties

class Menu extends Model {
  static get computed () {
    return ['hashids']
  }
  getHashids({ id }) {
    return Hashids.encode(id)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM