Switching up icon images using the switch statement

In order to change the icon image of a notification based on its type without using conditions, you can implement a switch statement. There are three types of notifications:

notifications_type:

  • answer_created
  • user_subscribed
  • answer_selected

The icon image is located inside an i tag within a span tag. To achieve this using a switch statement, you need to bind a function to the span element with the v-bind directive.

Below is an example code snippet that demonstrates how to accomplish this:

<div class="media g-mb-20">
<span v-bind="changeIcon" class="u-icon-v3 g-rounded-50x g-mt-2 g-mr-15 g-height-40 g-width-40 g-bg-gray-light-v5">
  <i class="icon-bubble g-font-size-18 g-color-gray-light-v1"></i>
</span>
  <a href="#"
     class="media-body g-brd-around g-brd-gray-light-v4 g-py-10 g-px-15 g-bg-gray-light-v5--hover u-link-v5 g-rounded-3">
    <p class="mb-0 g-font-size-16 g-color-gray-dark-v3">
      <span class="g-font-stag-medium">{{ item.message }}</span>
    </p>
  </a>
</div>

You can add the necessary logic inside the changeIcon function to handle the switch statement and update the icon image for each notification type.

Answer №1

To display the correct icon, there is no need to bind anything. You can achieve this by utilizing specific classes. Begin by defining your icon in the following manner:

<i class="g-font-size-18 g-color-gray-light-v1" :class="switchIcon"></i>

Next, create a new computed property like so:

computed: {
  switchIcon () {
    switch (this.notification_type) {
      case 'some_type':
        return 'icon-bubble'
      default:
        return ''
    }
  }
}

With this setup, when notification_type is set to some_type, your icon will be displayed as follows:

<i class="icon-bubble g-font-size-18 g-color-gray-light-v1"></i>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is your div not loading correctly due to JQuery or Javascript?

PHP Version 7.0 JQuery Version 3.1.0 It is noticeable that the content above gets copied and pasted every five seconds instead of refreshing. The aim is to refresh the include every five seconds. Website: Code: <!doctype html> <html><h ...

Error encountered in Express Router middleware (`app.use() function must be provided with a middleware function`)

I've seen plenty of similar questions on this topic, but after reviewing them all, I still haven't found a solution. My current dilemma involves creating an app using Express Router, however I keep encountering the following error: app.use() re ...

Troubleshooting problems with local references in an Angular date picker

I am currently working with an Angular date picker component and trying to access its values using a local reference. Unfortunately, when I attempt to console log the local reference, it returns undefined. The datepicker, function, and trigger are provid ...

Using Vue.js to build Chrome extensions

Is vuejs suitable for creating chrome extensions? I have experience with react and redux, but now I'm looking to explore something different. Although there isn't much information available, I am curious about how I can leverage vue components al ...

What steps can be taken to modify the style of a single button belonging to a broader group of elements?

Is there a way to hide the save button within a Vue Modal without affecting other buttons with the same class? .btn.btn-primary { display: none; } Simply targeting .btn-primary in the CSS affects all elements with that class, and adding .modal woul ...

A step-by-step guide on leveraging ethereumjs-tx within a web browser

Is it necessary to install npm ethereumjs-tx when utilizing the browser-based version downloaded directly from GitHub? If so, how can we incorporate the ethereumjs-tx module into our script file? It seems like these are two separate components based on ...

Guide on utilizing substring string functions in the updated version of Selenium IDE

I am facing a challenge with extracting the word "Automation" from a given string "Welcome to the Automation World" using Selenium IDE Record and Play feature. I have tried using the execute script command, but it doesn't seem to be working as expecte ...

Show each text field individually in JavaScript

Is it possible to display text fields one by one upon button click? Initially, all text fields are hidden, but when the button is clicked, they should be displayed one after another with their visibility property set to visible? This is what I have attemp ...

What is the best way to generate an extensive table with HTML and AngularJS?

I am looking to implement an expandable table feature where a single header with a plus icon can be clicked to reveal the child rows beneath it. Currently, I have been able to achieve this functionality. However, I am facing an issue where the child rows ...

Utilizing ASP.NET AJAX: Enhancing User Experience with ScriptManagerProxy for HTML Button Click Events

I am currently working on a solution for the following issue: Managing the client-side click event of a button - preventing postback if the JavaScript call returns false (e.g. onclick="js:return IsThisAllowed()") The JavaScript method will either return ...

Encountering an ongoing problem with trial repetition in JsPsych, which is causing the looping to continue endlessly without

As a beginner in JsPsych, I'm diving into creating a basic math quiz task to sharpen my skills. The goal is to generate random math questions as prompts and stop the task after 10 correct answers. I've managed to code that selects random math pro ...

perform an action if any division element is void of content

Currently, I have a statement that checks if any of the Divs with the ID #Drop are empty. If one is found to be empty, an alert is shown. However, there seems to be an issue where the statement stops working when any div contains content. What I am trying ...

Using ng-disabled on input elements within an ng-repeat directive

Showing different inputs based on an array looks like this <div data-ng-repeat="n in langInput.values"> <input type="text" id="auction_name_{{n.selected}}" class="form-control" name="auction_name_{{$index}}" ...

Sign Out Page for AngularJS

I am experiencing a problem with the logout page. When the logout option is clicked from a page, the login page should be loaded and even if the user attempts to click the back arrow in the browser, only the login page should be shown. I have attempted usi ...

Assign external data to component prior to rendering

When using Vue Router with file based components, I encountered an issue with rendering data from an API in a component called CaseDetail.vue. This component receives a parameter (slug) which is used to fetch a JSON from the API using axios. To handle thi ...

Obtain the HTML content of a webpage a few seconds following its initial loading

Currently, I am working on a script in nodejs to automate the process of fetching data from an online directory. Although this is new territory for me, I decided to go with javascript as it is a language I am comfortable with. After some research on Googl ...

There seems to be a glitch in the AJAX code

I am currently working on a project that involves displaying categories and subcategories of products on a webpage. When users click on either a category or a subcategory, AJAX is used to send the selected item to a php script which then generates HTML cod ...

Trigger a functional component's function in React using Chrome developer tools

function App() { const myFunction = () => { console.log('hello world!') } return <div>...</div> } After the website has fully loaded and the component is mounted, can we access the JavaScript to call myFunction()? ...

Looking to Move the Image Up?

I've been experimenting with creating a grid layout where the bottom image will move up until it is 10px away from the image directly above it. However, no matter what I attempt, the spacing seems to be based on the tallest image rather than the one a ...

Loading a page with a Chitika ad using Jquery can cause site malfunctions

I am experiencing an issue with my header.php file on my website. I included another PHP file for my ad code, but it seems to load the ad and then redirect me to a blank page. Can someone please review my code and let me know if there is an error? Thank yo ...