javaScrip

Model(数据转换模型)

  1. yarn add modeljs-core
import Model from 'modeljs-core'

const User = Model.define('User', {
  id: {
    type: Number,
    field: 'id',
    default: 0
  },
  isImg: {
    type: String, // 可以强制更改后端的数据类型,比如Number转String
    field: 'isImg',
    default: ''
  },
  img: {
    // key和后端接口返回的key一致,就不需要写对应的field字段
    type: String,
    default: ''
  },
  isPopup: {
    type: Number,
    field: 'isPopup',
    default: ''
  },
  isShow: {
    type: Number,
    field: 'isShow',
    default: ''
  },
  title: {
    //更改key
    type: String,
    field: 'tittle',
    default: ''
  },
  ary: {
    type: Array,
    field: 'class',
    default: []
  },
  obj: {
    type: Object,
    field: 'class',
    default: {}
  }
})

getLoginConfigApi().then(res => {
  res.data[0].ary = [1, 2]
  let mapping = User.fromData(res.data) //数据一一映射,
  // res.data 如果是[], 返回的是数组;是{},返回的就是{}
  // let mapping = User.toData(res.data[0])  //原始数据对象
  console.log('--ddd-:', mapping)
})

https://github.com/charleslo1/modeljs

https://www.npmjs.com/package/modeljs-core

上次更新: