Error encountered when attempting to pass a custom component as a prop in VueJS that is undefined

Can you please clarify why Vue is indicating that

Unknown custom element: <MyComponent> - did you register the component correctly?
is an issue in this code snippet:

const CustomComponent = Vue.component('CustomComponent', {
  template: '<div>test</div>'
})

const RenderComponent = Vue.component('RenderComponent', {
  props: {
    component: {
        type: [Function, String]
    }  
  },
  template: '<component :is="component"></component>'
})

new Vue({
  el: '#app',
  components: {
    MyComponent: CustomComponent,
    RenderComponent
  },
  template: '<render-component component="MyComponent"></render-component>'
})

https://jsfiddle.net/2qobuj4y/1/

What is the correct way to pass components to a prop in this scenario? Do you have any examples to share?

Answer №1

MyComponent does not exist, so it should not be listed in the components of your app. Update your app code to:

new Vue({
  el: '#app',
  components: {
    CustomComponent, // Not used; template passes a string instead
    RenderComponent
  },
  template: '<render-component component="CustomComponent"></render-component>'
})

Check out this fiddle for reference.

Explanation

You correctly registered CustomComponent as MyComponent in the main app component and can use it in the template directly. However, you are also passing a string with the value "MyComponent" to the RenderComponent component. This causes RenderComponent to attempt instantiating a MyComponent, which is not found in the global scope. It exists only within the main app component where it was registered.

If you try using a binding (:component) instead of passing MyComponent, it still won't work because you cannot pass components as props.

(EDIT: You can do it in an unconventional way like this: :foo="$options.components.foo". Refer to this comment from Evan You)

Interesting Alternative

You mentioned attempting to pass it from data, and while it may work, I cannot confirm if it's a good practice. It might become more complex when migrating to single-file components without access to a global scope. Despite being confusing, this method works:

new Vue({
  el: '#app',
  data: {
    MyComponent: CustomComponent // Retrieved from global scope
  },
  components: {
    // CustomComponent, // Omitted in this version
    RenderComponent
  },
  template: '<render-component :component="MyComponent"></render-component>'
})

View the updated fiddle here.

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

What is the best method for eliminating a character from all elements in jQuery classes?

I am working on an Angular 4 app where every .inner-page class in a html element includes a "/". For instance: <div _ngcontent-c0="" class="inner-page /login"> <div _ngcontent-c0="" class="inner-page /register"> I need to eliminate the "/" c ...

Using JavaScript to locate a child element within its parent

What is the most effective approach to locate a child element (using class or ID) of a specific parent element using only pure JavaScript without relying on jQuery or other frameworks? In this scenario, I am interested in finding child1 or child2 within p ...

What is the best way to uninstall the Google Translation Plugin when transitioning to a new Vue component?

I'm facing a minor issue as I develop a website builder. It consists of two main parts: the builder and the preview section. Within the preview, I've included the Google Translation plugin: onMounted(() => { new google.translate.TranslateEle ...

Is there a way to remove a checkbox node that has already been generated?

I've been working on creating a to-do list, and I've run into an issue with deleting the checkbox once the task is complete. I attempted to create a function within the main function containing an if/else statement, but it didn't seem to wo ...

JQuery form not triggering the submit event

Currently, I am facing some issues with my code while trying to trigger on submit event on a form and validate it. The main problems I encountered are that the onsubmit event is not being triggered, and the input field of type email is not validated proper ...

AWS Amplify-hosted Nuxt applications are resilient to errors during the build process

My website is built using Nuxt js and hosted on AWS Amplify. I've encountered a major issue where the website still gets generated successfully even when there's a failure in the nuxt generate command (like a JavaScript error in my code). Below i ...

The functionality of angular-ui's ui-utils and ui-scroll module is currently nonfunctional in version 0.1.0

I have been trying to implement the features from this Angular UI library: http://angular-ui.github.io/ui-utils/, particularly focusing on this aspect: https://github.com/angular-ui/ui-utils/blob/master/modules/scroll/README.md Unfortunately, despite my e ...

The CssDependency dependency type in vue-cli@3 does not have any module factory available

After using vue-cli@3 to npm run build The system showed an error message: No module factory available for dependency type: CssDependency I have extensively searched for related solutions, but they all pertain to Angular. I also attempted the following ...

Enable Google Chart to visualize multiple sets of data beyond just 2

This Google chart displays 2 data sets (Tea, Coffee). I've attempted to modify it to show 5 data sets but encountered difficulties. I experimented with changing the button.onclick function and the button value. Below you will find the original code (2 ...

How to use jQuery to extract a JSON snippet from a lengthy string

I received an AJAX response in the form of a lengthy string, which includes HTML and JSON data. The JSON object is embedded within the string and not always at the end. My goal is to extract the JSON object from the string so that I can utilize it with th ...

How come the Jquery :odd selector picks out the even elements?

<html> <head> <script type="text/javascript" src="jquery-1.7.1.js" ></script> <script type="text/javascript"> $(function() { $("p:odd").html('modified'); }); </script> </head> & ...

finding the current page id in Vue.js and Laravel: step-by-step guide

Focusing on my blog project using Laravel and Vue.js, I have developed the comment section as a Vue component and integrated it into the show function of my post like this: @section('content'); . . . <comment></comment> @end ...

Transform Promise-based code to use async/await

I'm attempting to rephrase this code using the async \ await syntax: public loadData(id: string): void { this.loadDataAsync() .then((data: any): void => { // Perform actions with data }) .catch((ex): v ...

Unable to display data retrieved from axios in a datatable using VueJS

I'm currently attempting to retrieve data from axios and display it in a datable component. The hardcoded data is being rendered successfully every time, but I am struggling to integrate the data from the axios call. I am fetching data in the same for ...

What is the best way to retrieve the value of this event in a React component?

Is there a way to retrieve the value of an input field? Typically, I would use event.target.value to do so. However, in this case, that method is not viable because the path of event.target leads me to an li element instead: https://i.sstatic.net/0iB9v.pn ...

Refreshing a Nested Component Within a Parent Component in React

Currently, I am in the final stage of a small project where a Higher Order Component (HOC) is being utilized to display a basic Tinder-like application. The internal component is a class-based component containing its own fetch() call that presents user d ...

Guide on sending data to MongoDB using Postman

I am currently facing an issue while trying to send data to the MongoDB database using Postman. Despite everything seeming fine, I keep encountering errors. https://i.stack.imgur.com/Gcf5Q.jpg https://i.stack.imgur.com/ntjwu.jpg https://i.stack.imgur.co ...

The Vue.js application appears to be functioning properly with no error messages, however

Currently, I am in the process of learning Vue. Recently, I came across a helpful tutorial that I've been trying to implement using the standard vue-cli webpack template by splitting it into single file components. Despite not encountering any errors ...

Error encountered when importing components in VueJS due to module not found

Currently, I am facing an issue with my app where I'm loading components using <router-view/>. The specific problem arises when importing the components in router.js, as it consistently gives me this error: These relative modules were not foun ...

My initial junior UI/UX assignment: Can you confirm whether this form modal dialog is pixel-perfect, and offer any suggestions for improvements

Currently diving into my first project as a Junior UX/UI Designer. Coming from a background in software engineering, I decided to challenge myself by focusing on design. I'm seeking feedback on the pixel perfection of this modal window, which my seni ...