Assigning attributes to inner components in VueJS based on prop values

Experimenting with some common VueJS syntax, but I am struggling to get this Button.vue SFC to function properly:

<script setup>
defineProps({
    ...
    href: String,
    ...
});
</script>

...

<template>

<Link :href="href"
      :as="href == null ? 'button' : null"
      :type="href == null ? 'button' : null">
    <slot />
</Link>

</template>

I want to make sure that if a href is present, the Link acts like an anchor, using the provided URL. However, if no href prop is given, it should behave as a regular button by adding as="button" and type="button" attributes to the Link component.

When I try to instantiate a Button with href="register", I get this error in the browser console:

[Vue warn]: Unhandled error during execution of render function 
  at <Link href="http://myproject.test/register" as=null type=null  ...> 

The value of href seems fine in this case since the href prop is provided, but the as=null type=null part seems to be causing an issue. It appears that "null" is not being recognized as null.

Just for reference, I am using Vue3 :)

Any suggestions or thoughts on how to resolve this? Thank you in advance

Answer №1

If you are encountering a null issue, you can attempt the following solution:

<Link :href="href"
      :as="href == null ? 'button' : a"
      :type="href == null ? 'button' : a">
    <slot />
</Link>

Within the Inertia Link component, the as props correspond to the HTML tag name.

Answer №2

After some thorough investigation, I have pinpointed the issue. Upon consulting Vue3 documentation, it became evident that optional props without values are interpreted as undefined, contrary to Vue2 which treats them as null.

Consequently, by replacing every instance of null with undefined in my code, the problem was resolved.

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

Tips for retrieving the state value and using it as a parameter in methods

Is there a way to utilize Vuex state and access it in the methods property of components? I have a state called "currentThreadId" that I would like to use within my methods. The issue I am facing is being able to retrieve the "currentThreadId" from the Vu ...

Issue with NuxtChild component not displaying child component

https://i.stack.imgur.com/7skBL.png Within the _collection.vue file, there is a structure like this: <div> This represents a collection <NuxtChild /> </div> In the category > index.vue file, the content is as follows: <div&g ...

Finding out which side of a cube has been clicked---Let's identify the side

Currently, I am in the process of developing a navigation menu and I am looking for a way to detect which side the user has clicked. Is it possible to achieve this using raycasting or is there an alternative method that would work better? If you require i ...

Deploying tracking scripts within GTM containers during Turbolinks transitions

Is there a solution for getting a Google Tag Manager container to fire all its tags under Turbolinks? In my Rails 4.0 application with Turbolinks, I have included the following code in a footer that is rendered on every page within the <head> tags: ...

What is the best way to extract keys from a hash in Ruby using a Javascript string?

I am currently developing a command line tool using Ruby that is designed to parse JSON data from diverse sources and perform certain operations on the retrieved information. To make it user-friendly, I have incorporated a feature where users can configure ...

Achieve Dual Functionality with Vue.JS Button within a Parent Element

My parent component structure looks like this: <template> <b-container> <b-modal id="uw-qb-add-item-modal" ref="uw-qb-add-item-modal" title="Enter Item Number" @ok="handleNewItem"> <f ...

Avoid the expansion of line decorations in Monaco editor

I am looking to create a Monaco editor line decoration that remains contained within its original position when I press enter after the decoration. For instance, in a React environment, if I set up a Monaco editor and add a line decoration using the code ...

Ajax fails to function within a loop

I'm looking to implement Ajax in a loop to retrieve data sequentially. This is what I have in my JavaScript function: var resultType = $("input[name='resultType']:checked").val(); var finalResult = ""; var loadingMessage = "<img src=&ap ...

Automated pagination in Jquery running seamlessly

I have successfully created pagination using jQuery. Although the script is functioning properly, I now want it to automatically switch between different pages: <script> $(document).ready(function(){ $("#article_load_favourites").load("indexer_favo ...

Utilize HTML strings to serve static files in express.js

While I primarily focus on frontend development, I often face challenges when working on the server side. Currently, I have a server.js file that includes: const express = require('express'); const http = require('http'); const path = ...

Importing components in real-time to generate static sites

My website has a dynamic page structure with each page having its unique content using various components. During the build process, I am statically pre-rendering the pages using Next.js' static site generation. To manage component population, I have ...

Updating the NODE_ENV variable in Cypress

Working with Cypress, I have a variable in my code "process.env.VUE_APP_API_URL" that is defined in my .env.development file. I need to change this variable while running Cypress. Any suggestions on how I can achieve this? Just a note: When launching Cyp ...

"Troubleshooting: Why are errors not appearing in ts-node

Whenever I encounter an error in my code while compiling with ts-node, the error does not seem to appear in the console. For instance:let data = await fs.readFileSync(path); In the following code snippet, I am using "fs" to read a file by passing a path ...

Utilizing JavaScript variables imported from an external library in Next.js: A Guide

I am currently working on a Next.js with Typescript website and I am in the process of adding advertisements. The ad provider has given me instructions to embed this JavaScript code on my site: <script src="//m.servedby-buysellads.com/monetization. ...

Error message: Impossibile to locate module 'formik' in 'C:Live projectsNew folder (2)src\_metronicpartialsmodalscreate-app'

"dependencies": { "@formatjs/intl-pluralrules": "^4.0.28", "@formatjs/intl-relativetimeformat": "^9.1.7", "@fortawesome/fontawesome-free": "^5.15.3", "@popperjs/core": "~2.10.1", "animate.css": "^4.1.1", "apexcharts": "^3.27.1", ...

Switching on click using jQuery

I'm having some difficulties with my code and I can't quite figure out how to solve the problem at hand. To be honest, I'm not even sure what the exact question is here. If it seems a bit confusing, I apologize - I'm still new to Jquery ...

Searching for data across multiple tables using the Laravel framework combined with Vue.js and API integration

I am currently implementing a multi-search feature for a single table The search functionality is functioning correctly but only for one column Here is the snippet from my controller: public function index() { return Matter::when(request('sear ...

The Value Entered in Angular is Unsaved

I have encountered an issue with my app's table functionality. The user can enter information into an input field and save it, but upon refreshing the page, the field appears empty as if no data was entered. Can someone please review my code snippet b ...

Unable to reach props during mounted lifecycle stage

I am currently working on a Vue component that looks like this: <template> <div> <div class="page-head"> <h4 class="mt-2 mb-2"> {{form.name}} </h4> </div> <f ...

When attempting to delete a resource using an HTTP request in Insomnia, I encountered a TypeError stating that it was unable to read the property 'id'

I recently started using Express and am in the process of setting up the Delete function for my database within the MERN stack. While testing my CRUD operations using Insomnia, I have encountered an issue specifically with the Delete operation. The proble ...