Display Vue component based on a condition

I am currently working on rendering different components in the navbar based on the email/account that is signed in. There is a function called "getUser()" which retrieves the user's email. The items in the navbar include: Home, Procurement Site, Monitoring, Admin, and Log out. I want to show the "Admin" section only if the user's email matches [email protected]. Initially, I tried to check if the function successfully returns the email address and used a conditional template as shown below:

<template v-if="this.email == '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1c0c5ccc8cfe1c6ccc0c8cd8fc2cecc">[email protected]</a>'">
     <li class="nav-item">
         <router-link to= "/Admin"> Admin </router-link>
     </li>
</template>

Although this code successfully hides the admin section when not logged in with the specified email, it fails to display the section even when logging in with the [email protected] account. Any assistance would be greatly appreciated. Thank you.

Below is the full code for the navbar:

<div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav">
          <li class="nav-item">
              <router-link to= "/Procurement"> Home </router-link>
          </li>
          <li class="nav-item">
              <router-link to= "/ProcurementSite"> Procurement Sites </router-link>
          </li>
          <li class="nav-item">
              <router-link to= "/Monitoring"> Monitoring </router-link>
          </li>
            <template v-if="this.email == '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f392979e9a9db3949e929a9fdd909c9e">[email protected]</a>'">
                <li class="nav-item">
                    <router-link to= "/Admin"> Admin </router-link>
                </li>
            </template>
          <li class="nav-item">
              <router-link to="/" v-on:click="logOut()"> Log Out </router-link>
          </li>
        </ul>
      </div>
<script>
    import $backend from './../backend'
    
    export default {
      name: 'nav',
      data () {
          return {
            email: ''
          }
          
      },
      methods: {
        getUser () {
            console.log("in get user in monitor vue")
            $backend.getUser()
            .then(responseData => {   
            this.email = responseData.result
            console.log(this.email)
            }).catch(error => {
            this.error = error.message
            })
        }
      }
    }
</script>

Answer №1

Looking to see some code examples? Look no further!

If you want to test this code out yourself, simply click Vue SFC Playground.

"Yes, you are the admin"
will only display if the associated email is
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cbaaafa6a2a58baca6aaa2a7e5a8a4a6">[email protected]</a>
.

<template>
  <div>
    E-mail:
    <span>
      {{email}}
    </span>
  </div>
  <div>
    Are you an admin? <span v-show="email == '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dabbbeb7b3b49abdb7bbb3b6f4b9b5b7">[email protected]</a>'">Yes, you are the admin</span>
  </div>
  <input v-model="email" >
</template>

<script>
import { defineComponent } from "vue";
export default defineComponent({
  data() {
    return {
      email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d2c292024230d0c2a202c2421632e2220">[email protected]</a>"
    }
  }
});
</script>

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

Implementing Vuex to dynamically update an object's array property by pushing new elements inside a mutation with reactivity

As per the Official documentation, vuex mutations have certain limitations when it comes to reactivity. When we need to add a new property to an object, we usually use: Vue.set(obj, 'newProp', 123) However, adding a new array property and push ...

Effortlessly implement CSS styling in a scoped shadow element with Vue3

I am facing an issue with applying styles to an anchor element in my code. Below is a snippet of the code: <template> <custom-button> #shadow-root (open) <a href="#"></a> <other-custom></other-c ...

What steps can I take to make the replaceData function function properly?

Struggling to implement replaceData, setData, or updateData functions in my project. I am using bootstrap vue along with tabulator tables. The API is functioning correctly. Tabulator works smoothly unless the API URL depends on the modal select option; how ...

What is the best way to initiate the handling of newly inserted values in a Vuex store?

I am working with a Vuex store that stores entries: const store = createStore({ state() { return { entries: [ { id: 1, date-of-birth: "2020-10-15T14:48:00.000Z", name: "Tom", }, ...

vuex: The decision between using $store.commit and directly calling the function

Recently, I discovered an interesting technique where store mutation functions can be passed into HTML events. Here's an example: //In the template <input name="the_field" :value="the_field" @input="updateField"/> // In the component methods ...

Arranging an array based on relationships between children and parents

I have an array of objects like this: const data = [{id: 3, name: ''}, {id: 4, name: ''}, {id: 5, name: '', parent: 3}, {id: 6, name: '', parent: 5}, {id: 7, name: '', parent: 4}, {id: 8, name: '&ap ...

Unable to instantiate a Vue reference with a potential null type

When working in Vue v3, I encountered a situation where I needed to create a ref with a type that could possibly be null. This specific scenario in my app involves data that starts off as null and then gets populated after loading completes. Just to illus ...

What is the best way to create a new row within a Bootstrap table?

Struggling to format an array inside a table with the `.join()` method. The goal is to have each car on a separate row. Attempts using `.join("\r\n")` and `.join("<br />")` have been unsuccessful. What am I overlooking? ...

Preventing the default event propagation in a Vue 2.x directive: A guide

Vue.directive('login-to-click', { bind (el) { const clickHandler = (event) => { event.preventDefault() event.stopImmediatePropagation() alert('click') } el.addEventListener('click', clickHand ...

The transition from Vuetify 2 to Vuetify 3 involves replacing deprecated properties like app, clipped-left, and offset-y with the new property called "

Seeking guidance on upgrading from Vuetify/Vue 2 to 3. As a non front-end developer tasked with updating legacy code, I'm facing challenges due to the migration guide assuming CSS knowledge and lacking examples for resolving removed features. The gui ...

Experimenting with a customizable Vue.js autocomplete feature

Check out this sample code: https://jsfiddle.net/JLLMNCHR/09qtwbL6/96/ The following is the HTML code: <div id="app"> <button type="button" v-on:click="displayVal()">Button1</button> <autocomplete v- ...

Enhancing Vue.js functionality with v-model for seamless global data access

I am currently developing a web application that allows users to collaborate on projects. The app's architecture is structured in the following manner: Component A (the main app container) Components B1-Bn (including header, footer, and main window, ...

The v-autocomplete component's watcher is not functioning as intended

Despite numerous attempts, I have been unable to get my watcher to work properly with the v-autocomplete component. <template> <v-app id="inspire"> <v-content> <v-form ref="form" v-model="valid" :lazy-validation="lazy"> ...

What is causing the data not to react with the props in this particular scenario?

In addition to the central App.vue component, I have two other components - Rooms.vue and Desks.vue. When a swiper element in the Rooms.vue component is clicked, it triggers the opening of the Desks.vue component and emits the room object to App.vue. This ...

Array contains a copy of an object

The outcome I am striving for is: dataset: [ dataset: [ { seriesname: "", data: [ { value: "123", }, { value: &q ...

A comprehensive guide to utilizing Vue for navigating between various pages and showcasing their corresponding titles and text

I am currently developing a blog website using vue.js. My current challenge is as follows: The website has two main pages - one for displaying all the blogs and another for viewing a specific blog post. I am utilizing vuex to manage the state of my applic ...

When incorporating a background-image into my Vue.js project, I encountered this error

I encountered an issue after adding a background-image to my vue.js project: ./src/assets/scss/argon.scss (./node_modules/css-loader??ref--8-oneOf-3-1!./node_modules/postcss-loader/src??ref--8-oneOf-3-2!./node_modules/sass-loader/dist/cjs.js??ref--8-oneOf ...

Can html-webpack-plugin be configured to create <style> elements from CSS files?

I am managing a static site with Vue and Webpack. In my project, I have a file named style.css containing global CSS rules which I import using import './styles.css' in my index.js file. Additionally, I have some .vue files that generate their o ...

Having trouble setting a cookie. Sharing the complete code on codeSandBox for both backend and frontend. Can anyone spot the issue? Appreciate the help! Using Express with Vue 3

Presenting a sandbox of a backend, featuring a simple setup with one route and one controller. Curious to explore the full code for the backend? Check it out here. As for the frontend, it's equally straightforward. I began by creating a new Vue 3 pr ...

I aim to design a unique child window specifically for an "about" section within an electron js application on the Windows platform

I am looking to create a child browser window to showcase some key points about my application. According to the Electron JS documentation, it supports the "about" role for Mac OS but does not have built-in support for Windows. Therefore, I am in the pro ...