Passing a custom Vue component as a property to a parent component

Struggling to find answers to my unique question, I'm attempting to create an interface where users can drag or input images using Vue. I've developed a component called Card, which combines Vue and Bulma to create card-like objects with props for title and content. Easy enough to use when the data is pre-loaded text or image.

However, my challenge lies in embedding another vue component, specifically the PictureInput component from vue-picture-input on NPM, within the Card. I've delved into reactive props and dynamic components in the Vue documentation, but neither addresses this scenario directly.

Code:

Card.vue:

 // Code snippet for the Card.vue goes here

ImageInput.vue:

// Code snippet for the ImageInput.vue goes here

My attempt at dynamically generating the PictureInput object and passing it as the content prop to Card failed due to errors related to cyclic object values and missing methods in the PictureInputClass object. Even simplifying to

<Card :title="makeCardTitle()" :content="PictureInput"/>
resulted in undefined references during rendering. Perhaps changing content to a named slot could be a solution?

I value Vue's flexibility in component reusability and integration, hence my persistence in finding a way to make the PictureInput a child of the Card component without drastic template modifications.

Answer №1

In order to incorporate the PictureInput component into the Card component, I would utilize slots. The following code snippet provides a simplified example:

Card.vue

<template>
  <div class="card is-size-7-mobile is-fluid">
        <div class="card-header">
            <a class="card-header-title">

                <span class="is-info" @click="isExpanable = !isExpanable">{{title}}</span>
            </a>
        </div>
        <div v-if='isExpanable'>
            <slot/> <!-- Slot for inserting the <PictureInput/> -->
        </div>
    </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanable: false,
    }
  },
  props: {
    title: String,
  }
};
</script>

<style scoped>
.card {
  border-radius: 1em;
  background-color: transparent !important;
  border-color: transparent !important;
}
.card-header {
  box-shadow: transparent;
}
</style>

ImageInput.vue

<template>
<div class="content">
    <Card :title="makeCardTitle">
        <PictureInput/>
    </Card>
</div>
</template>

<script>
import PictureInput from 'vue-picture-input';
import Card from './Card';

export default {
    name: 'ImageInput',
    components: {
        PictureInput,
        Card
    },
    props: {
        idx:{
            type:Number,
            default: 0
        }
    },
    computed: {
        makeCardTitle: function(){
            return "page ".concat(String(this.idx));
        }
    }
}
</script>

I hope this helps point you in the right direction. Remember, you typically don't need to use Vue.extend(PictureInput) as long as it's added to the components option key.

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

Alter mustache content according to the variable

Is there a way to dynamically change the contents of a mustache based on different conditions? I am working with 3 different arrays and depending on which one is "selected", I need to adjust the v-for loop by changing the key names in each array. <tem ...

The current date is cycling back to the month before

There is a datetime received from my api as 2018-09-01T00:00:00.000Z, referred to as frame.scandate. Another date is generated within the program as 2018-09, simply known as scandate. These examples can represent any year/month combination. In my code: ...

Does adding the Angular bootstrap script at the end of the body tag impact webpage speed more than using ng-app on the body tag?

In the past, we had placed ng-app on the body tag which led to problems with dependency injection when multiple modules were being used on the same webpage. This required module creators to be aware of the existing app and inject their app into it. We are ...

Exploring the integration of Styled-components in NextJs13 for server-side rendering

ERROR MESSAGE: The server encountered an error. The specific error message is: TypeError: createContext only works in Client Components. To resolve this issue, add the "use client" directive at the top of the file. More information can be found here i ...

Building a multi-form application with HTML5 validation and Vue.js

I am working on a vue.js form that consists of multiple steps and requires HTML5 validation to be simplified. Here is a snippet of the code: <template> <div> <div v-if="activeForm === 1"> <h2> ...

[ElectronJS][VueJs] ERROR_OSSL_EVP_UNSUPPORTED

I am in the process of developing an Electron app with VueJs. Currently, I am following a tutorial available at : https://www.youtube.com/watch?v=LnRCX074VfA However, I have encountered a bug and I am unsure about its meaning... Here is the error message ...

JavaScript code for iframe auto resizing is not functioning properly on Firefox browser

I have implemented a script to automatically resize the height and width of an iframe based on its content. <script language="JavaScript"> function autoResize(id){ var newheight; var newwidth; if(document.getElementById){ newh ...

Having trouble with implementing both filter and infinite scroll simultaneously in an Ionic list?

I've encountered an issue with my ionic hybrid app related to angularjs filters. The code snippet below showcases the problem: <input type="search" placeholder="Search personalities" ng-model="name" ng-change='alert("changed!")&apo ...

Troubleshooting vue.js jest tests using vscode

Currently, I am using vscode version 1.69.2 and encountering an issue. When I set a breakpoint in a Vue file and attempt to debug a Jest test, the breakpoint appears in the compiled code rather than in my source code. The breakpoint seems to open in a file ...

Integrate Ruby parameter into JavaScript in Rails

I have a div that I want to turn into a link to another page. Typically, we achieve this using link_to link. <% for item in @items %> <%= link_to "Item", item %> # -> or <%= link_to(item) %> <% end %> However, I need the ent ...

Sending the user to their destination

There is a code in place that triggers when an email is sent to the user, containing a link that leads them to a specific endpoint upon opening. I am experiencing issues with redirection at the end of the function. The redirect does not seem to be working ...

Tips for conducting unit tests on Vue.js components utilizing nuxt-i18n

When attempting to execute the code below using yarn run jest, an error is thrown: TypeError: _vm.$t is not a function. This occurs because the component SearchField relies on a translation ("$t('search')"). import { mount } from "@vue/test-util ...

In Vue templates, any spaces or line breaks between element tags are not disregarded

<template> <div> <p> hello universe </p> </div> </template> Result: <p> hello universe </p> We need to get rid of the unnecessary spaces before and after "hello universe" ...

The value of req.user is not defined in a stack involving node, express, passport,

When I use console.log(req.session); I receive the message: Session {cookie:{ path: '/',_expires: null,originalMaxAge: null,httpOnly:true },passport: { user: 5b427a2d117d7c3f6087db8a } } However, when using console.log(req.user); I get un ...

Positioning elements at the bottom of the container

There is a dilemma that only those in the world of web development will understand. Beware! I have envisioned a layout for a website I am constructing. The concept involves tilted <hr/> elements on the page, with text wrapping around them (refer to ...

What is the best way to incorporate white-space:normal within the text of a jQuery Mobile grid button?

Below is the code for my jQuery Mobile Grid buttons: <div class="ui-grid-b my-breakpoint"> <div class="ui-block-a"><button type="button" data-theme="c"> <img src="res/icon/android/business.png" height="45"><br>Business</ ...

Is it feasible to display a message for a certain duration without using the alert() function upon clicking a button?

I'm working on a NEXT.JS project and I need to display a <p> element under my button for a specific duration before it disappears. I don't want to use the alert() function. Any suggestions on how to achieve this? ...

A guide to showcasing items based on their categories using React.js

After successfully displaying Categories from a port (http://localhost:5000), accessing my MongoDB database, I encountered an issue when attempting to display the products for each category separately. Despite trying the same method as before, I keep rec ...

The Material UI button feature neglects to account for custom CSS styles when attempting to override the default settings

Utilizing a custom bootstrap css styles in my react app, I am seeking to enhance the default material ui components with the bootstrap styles. import React, {useState} from 'react'; import 'cg-bootstrap/core/build/cg-bootstrap-standard.css&a ...

I'm looking to switch out the `~` to turn it into a URL for the backend, seeing as `<img alt="" src="~/media/Banner/plane.JPG">` is returning a 404 error

1. Received an HTTP Request and JSON response from the backend localhost:3000 (entered value using wysiwyg) { "Description": "&lt;img alt=&quot;&quot; src=&quot;~/media/Banner/plane.JPG&quot; /&gt;1 test berita&lt ...