如何在属性中插入功能?
我正在尝试循环循环 COST_CLASSIFIACY_OPTIONS
数组,并用空格代替下划线,同时将每个字符串的第一个字母大写。我已经完成了一个方法 converttitlecase(str)
,但似乎不起作用。如何在中正确插入转换totitlecase(str)方法:options
()?
<style src="vue-multiselect/dist/vue-multiselect.min.css"></style>
<script>
import Form from 'vform'
import Multiselect from 'vue-multiselect'
export default {
components: {
Multiselect
},
data: () => ({
errors: {},
form: new Form({
designation_id: [],
position_id: [],
cost_classification: []
}),
designation_options: [],
position_options: [],
cost_classification_options: ['direct_labor', 'indirect_labor', 'general_and_admin']
}),
methods: {
async convertToTitleCase(str) {
const arr = str.split('_');
const result = [];
for (const word of arr) {
result.push(word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());
}
return result.join(' ');
},
},
created: function() {
this.getDesignationNames();
this.getPositionNames();
},
}
</script>
<multiselect
v-model="form.cost_classification"
:options="cost_classification_options"
:multiple="false"
:taggable="false"
:tabindex="6"
></multiselect>
任何帮助都非常感谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从方法的第一个中删除
async
关键字:创建一个
compulated
属性并使用该方法转换所有选项:将模板更改为:
Remove the
async
keyword from the first of the method:Create a
computed
property and convert all of options with the method:Change the template to this:
而不是让逻辑变得复杂。您可以通过迭代
mounted()
挂钩中的选项数组来简单地实现它。工作演示:
Instead of making the logic complex. You can simply achieve it by iterating the options array in the
mounted()
hook.Working Demo :