D
D
Dmitry2021-09-14 18:10:46
Vue.js
Dmitry, 2021-09-14 18:10:46

How to use switch case in vue?

There is a folder structure, and I would like the icon to change depending on the file type using switch case constructions in vue js:

6140ba18bbf4f432447525.jpeg

In switch case constructions, I will use the type field that each file has:
6140ba8e16603827973345.jpeg

How to write such a construction in vue.js 3.0:

switch(type) {
  case "image/svg": {
    return <i class="fas file-icon fa-image"></i>
  }
  case "image/jpeg": {
    return <i class="fas file-icon fa-image"></i>
  }
  case "image/jpg": {
    return <i class="fas file-icon fa-image"></i>
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima Pautov, 2021-09-14
@WafelT

<template>
    <i :class="getIconNameByType(type)"/>
  </template>

setup () {
  function getIconNameByType (type) {
    const iconsMap = {
      'image/svg': 'fas file-icon fa-image',
      'image/jpeg': 'fas file-icon fa-image',
      'image/jpg': 'fas file-icon fa-image',
      'text/javascript': 'fas file-icon fa-image',
      // ....
    };

    return iconsMap[type];
  }

  return {
    getIconNameByType
  };
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question