Vue3中formatter的使用

用法:在后端传给我们数据的时候,有时候数据并不是我们想要的,我们需要将它转换为我们所使用的格式

例子如下:

方法1:

<el-table :data="tabledata">

  <el-table-column label="性别" prop="sex">

    <template slot-scope="scope">

      <span v-if="scope.row.sex==="1">男</span>

      <span v-else-if="scope.row.sex==="2">女</span>

      <span v-else="scope.row.sex==="3">其它</span>

    </template>

  <el-table-column>

<el-table>

方法2:

<el-table :data="tabledata>

  <el-table-column :formatter="sexFormatter" prop="sex">

  <el-table-column>

<el-table>

const sexFormatter = row = >{

  return getSexStatus(row.sex)

}

const getSexStatus = sex = >{

  switch(sex){

    case '1':

       return '男';

    case '2':

       return '女';

    default:

      return "其它";

  }

}