如何使用带有 JS switch 语句的动态顺风类并在 Vue 中正确传递它们? [英] How to use dynamic tailwind classes with JS switch statement and pass them correctly in Vue?

查看:15
本文介绍了如何使用带有 JS switch 语句的动态顺风类并在 Vue 中正确传递它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Vue JS 的初学者,我正在尝试创建一个函数来为订单状态分配相应的颜色.我想使用 switch-statement 来实现这一点,它会获取订单状态的值并将其传递给 getStatusColour 函数(),如下所示:

const getStatusColour = (orderStatus) =>{让 statusColour = "";开关(订单状态){案例新":statusColour = "bg-green-100 text-green-900";休息;案件准备":statusColour = "bg-yellow-400 text-yellow-900";休息;案例准备好":statusColour = "bg-blue-200 text-blue-800";休息;案例已交付":statusColour = "bg-green-300 text-green-800";休息;案例失败":statusColour = "bg-red-400 text-red-900";休息;默认:statusColour = "bg-gray-100 text-gray-800";}返回状态颜色;}

然后在 Index.vue 文件中我有 export default { getStatusColour },我想这应该是一个错误.

然后在模板中我这样称呼它:

{{ order.status }}

但我不断收到 Uncaught (in promise) TypeError: _ctx.getStatusColour is not a function 错误.我将不胜感激.

解决方案

如 MDN 文档所示:https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export#using_named_exports


这里是整个 github 代码库.

utils/test.js

const getStatusColour = (orderStatus) =>{让状态颜色 = ''开关(订单状态){案例新":statusColour = 'bg-green-100 text-green-900'休息案例准备":statusColour = 'bg-yellow-400 text-yellow-900'休息案例准备好":statusColour = 'bg-blue-200 text-blue-800'休息案例已交付":statusColour = 'bg-green-300 text-green-800'休息案例失败":statusColour = 'bg-red-400 text-red-900'休息默认:statusColour = 'bg-gray-100 text-gray-800'}返回状态颜色}导出 { getStatusColour }

App.vue