The issue with VueJs arises when a basic v-if statement is utilized for computeds and fails

When using a simple v-if in Vuejs, the following code is used:

<span v-if="!profile_full_name===''">
    @{{ profile_full_name }}
</span>
<span v-else>
    {{auth()->user()->fullName}}
</span>

In this case, the profile_full_name is a computed method:

profile_full_name() {
    return 
       this.$store.state.profile_name 
           + ' ' + 
       this.$store.state.profile_family
}

The goal is to use v-if with profile_full_name whether it's empty or not.

When profile_full_name is empty, we should see:

@{{ profile_full_name }}

And when it's not empty, we should see:

{{auth()->user()->fullName}}

All computed methods work fine, and @{{ profile_full_name }} without v-if and v-else function properly as well.

export default {
    data() {
        //...
    },
    computed: {
        profile_full_name() {
            return this.$store.state.profile_name + ' ' + this.$store.state.profile_family
        }
    },
    methods: {
        change_name({target}) {
            this.$store.state.profile_name = target.value
        },
        change_family({target}) {
            this.$store.state.profile_family = target.value
        },
    }
};

By the way, instead of v-if, v-else can be used interchangeably at any time.

Answer №1

When dealing with a string that contains a space, it will always be different from an empty string (``), causing the condition in your v-if statement to always result in false. Instead, you should use String.prototype.trim() to trim the string and then evaluate it as either truthy or falsy:

<span v-if="profile_full_name.trim()">
    @{{ profile_full_name }}
</span>
<span v-else>
    {{auth()->user()->fullName}}
</span>

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

incorporating a dynamic parameter into the payload of an AJAX post request

I'm currently working on a function call where I want to extract the value of the variable data and place it in the data section of the function. However, despite my efforts, I haven't been successful so far. In PHP, I am attempting to retrieve t ...

Exploring advanced slot functionality in VuetifyJS through autocomplete integration with Google Places API

How can I make VuetifyJS advanced slots work seamlessly with the Google Places API? Currently, some addresses appear in the autocomplete dropdown only after clearing the input text by clicking the "x" icon in the form field. To see the problem in action, ...

What is the best way to use AJAX to navigate to a different webpage while sending data along with

After successfully saving a form and receiving a success message, I am able to redirect to another page using window.location.href = '/home'; with no issues. However, I would like to pass the success message to the home page after the redirect. W ...

Displaying 'hours and minutes' functions properly in all web browsers except for Safari. Utilizing JavaScript Date object

I am retrieving a date from an object within a loop in the following format: 2018-08-06 20:45:00 My objective is to only display "20:45" (always with two digits for minutes) in the client's timezone. To achieve this, I have implemented the below meth ...

What is the best way to embed Javascript scripts within existing Javascript code on the client side?

I am in the process of developing an innovative HTML5 game engine. My goal is to streamline the inclusion process by having just one file, engine.js, required in the HTML document. This script will establish a global Engine object that will grant users acc ...

Having trouble retrieving the pathname of a nested route within middleware.js in next js version 14

I am currently referring to the official App Router documentation for Authentication on this page My goal is to extract the pathname from the next URL export function middleware(request) { console.log('now we are in middleware'); const { ...

Transmit information using jQuery to an MVC controller

I am developing an ASP.NET MVC3 application and need to send three pieces of data to a specific action when the user clicks on an anchor tag: <a onclick='sendData(<#= Data1,Data2,Data3 #>)'></a> Here is the javascript function ...

Ammap interfaces with an external JSON file to generate simulated lines

I am attempting to load external JSON data into my ammap using dataLoader, and then use that data to animate the lines on the map in the postProcess function var map = AmCharts.makeChart("chartdiv", { "type": "map", "theme": "light", "dataLoa ...

Developing a search feature using Ajax in the MVC 6 framework

Embarking on a new project, I have chosen c# .net 6 MVC in VS2022... In my previous projects, this code has run flawlessly. @section Scripts { <script type="text/javascript"> $("#Klijent_Name").autocomplete({ ...

Breaking down an array in Node.js

After web scraping, I retrieved an array structured like this: array: [ 'line1', 'line2', 'line3', 'linen'.. ] My task now is to insert this data into a MySQL table. The challenge is that every 10 lines of ...

What is the best way to persist my data on a page when I navigate to a different page in React.js?

Currently, I am utilizing Material UI tabs with 8 pages as components. Each page contains input areas, and when I switch between tabs, the data in the inputs gets cleared. I want to retain this data even when moving to another tab. How can I achieve this ...

Encountering issue with jQuery - Ajax causing error 500 for select posts

Recently, I encountered an issue with the Ajax functionality on a live website. It was previously working perfectly fine, but suddenly started returning a 500 internal server error instead of the expected page. Oddly enough, I discovered that I could stil ...

Leveraging OAuth 2 with Google

I'm currently working on implementing Google API for user authentication. I have successfully managed to authenticate users, but I am struggling with redirecting users after Sign In and implementing Sign Out functionality. I have been referring to th ...

Transitioning to the Bootstrap library from the jQuery library with the use of several image modals

After coming across this specific question about implementing multiple image modals on a webpage, I noticed that it primarily focused on javascript and jQuery. However, my project involves utilizing the latest version of Bootstrap, so I'm curious if t ...

A guide on displaying modal content in PHP using the CodeIgniter framework

When attempting to print the content inside of a modal, pressing ctrl+p causes the modal and backside page to merge. How can I ensure that only the content inside the modal is printed without any merging? Any advanced solutions would be greatly appreciat ...

issue with nodeJS spawn child_process on glitch.com

I am attempting to set up an express server on glitch.com using the code provided below: const path = require('path'); const { spawn } = require('child_process'); const express = require('express'); const app = express(); app ...

How to access the parent array element in a nested ng-repeat in AngularJS

Hey there, I'm facing a bit of a puzzle with a seemingly simple question that's got me stumped. In my code, I have a nested array structure: $scope.rootItem = { id: '1', type: 'course', title: ' ...

I am encountering difficulties in successfully implementing the src tags for my JavaScript and CSS files

Currently, I am working on a sample web application using HTML5, Bootstrap, Express.js, Angular, and jQuery. I have been struggling with linking the js and css files in my project, as they only seem to work when hosted online. Below is an example of what m ...

Ensuring each field is filled correctly in a step-by-step registration form

I have been working on implementing a step-by-step registration form, and I am facing an issue. When I click on the next button, all fields should be mandatory to proceed to the next step. It's crucial for the user experience that they fill out all th ...

Strategies for retrieving the latest content from a CMS while utilizing getStaticProps

After fetching blog content from the CMS within getStaticProps, I noticed that updates made to the blog in the CMS are not reflected because getStaticProps only fetches data during build time. Is there a way to update the data without rebuilding? I typica ...