I have the following vue instance which has a method to conditionally return some html markup. As you can see the markup is identical for each condition, only different styles are being applied.
Is there a more elegant way to achieve the same? Also should I be using a method or a computed property or a component to achieve this - If I should use a computed property how to I pass the value to evaluate ?
Any one know any other way?
<div v-for="item in items">
<div v-if="!_.isEmpty(item.files)">
<ul >
<li v-for="file in item.files">
<span v-html="fileIcon(file.fileExtension)"></span>
</li>
</ul>
</div>
</div>
var vm = new Vue({
el: '#root',
data: {
items: [
{
"id": 126,
"content": "blah...",
"files": [
{
"id": 8,
"filename": "test.doc",
"fileExtension": "doc"
}
]
},
{
"id": 127,
"content": "blah...",
"files": []
}
]
},
methods: {
fileIcon: function (extension) {
var str;
switch (extension)
{
case "jpg":
case "jpeg":
case "png":
case "gif":
str = "<i class='fa fa-file-image-o file-picture'></i>";
break;
case "pdf":
str = "<i class='fa fa-file-pdf-o file-pdf' ></i>";
break;
case "xls":
case "xlsx":
str = "<i class='fa fa-file-excel-o file-excel' ></i>";
break;
case "doc":
case "docx":
str = "<i class='fa fa-file-word-o file-word' ></i>";
break;
default:
str = "<i class='fa fa-file-file-o file-empty' ></i>";
break;
}
return str;
}
}
});
Please note I'm very new to Vue so some code snippet to guide would be appreciated.