Filter writing method: judging multiple conditions based on the content returned by the backend

Note: When it is necessary to render the corresponding color or content according to the data status returned by the backend, a filter can be used. If there are only two states, you can use the ternary operator directly

三元写法
<el-tag :type="row.status === 1 ? red : bule">{
   
   { row.status }}</el-tag>

When there are many things to judge

<el-tag :type="row.status | statusFilter">{
   
   { row.status }}</el-tag>

filters: {
    //根据返回的状态来渲染对于的颜色或者内容,这里的根据状态返回的数据去取对应的下标,所以,需要根据你的状态数字来放对应的内容,比如你后端给的1代表红色,那么你就需要在第二个那里放红色,根据下标1来取,就是第二个
    statusFilter(status) {
        const statusMap = {
            published: 'success',
            draft: 'info',
            deleted: 'danger'
        }
        return statusMap[status]
    }
},

Guess you like

Origin blog.csdn.net/xiaokangna/article/details/123660543