Unable to transfer Vue.js components in and out of the project

I have a directory structured like this.

VueTree
  components
    Classic.vue
    Modern.vue
  index.js

The Classic and Modern components consist of a template, export default {}, and a style. I am importing both of them in index.js as:

import Classic from './components/Classic.vue'
import Modern from './components/Modern.vue'

const VueTree= {
  Classic ,
  Modern 
}

export default VueTree

However, when I try to import the module using:

import {Classic , Modern } from '../../modules/VueTree/index.js'

I encounter the following error:

Unknown custom element: - Did you register the component correctly? For recursive components, make sure to provide the "name" option.

Although I have defined name: "Classic" within my components and included it in the current file using components: { Classic },, the same error persists.

It seems to only work if I export just one component like this:

import Classic from './components/Classic.vue'
import Modern from './components/Modern.vue'
export default Classic

This method works for including Classic, but I am unable to export both components simultaneously as shown in this example https://github.com/greyby/vue-spinner/blob/master/src/index.js

Answer №2

Your setup appears to be in good shape.

import Classic from './components/Classic.vue'
import Modern from './components/Modern.vue'

const VueTree = {
  Classic,
  Modern
}
export VueTree

The issue lies with your components. It seems that you are using a Treeview, which involves a recursive component. Be cautious when creating them to avoid triggering an infinite loop.

Check out my github for an example of how the VueTree should function.

Ps: Remember to include a key and name for a recursive component.

Components can recursively call themselves within their template, but only when utilizing the name option. - Vue Docs.

If the above solution doesn't work, feel free to share a github link to the problem, and I'll gladly assist.

Answer №3

Another approach

main.js

export { default as OldSchool } from './components/OldSchool.vue'
export { default as Trendy } from './components/Trendy.vue'

Trendy.vue

import { OldSchool } from './components/main';

export default {
   components: { OldSchool }
}

NOTE: Make sure to export child components just before the parent component in main.js.

Answer №4

In my opinion, it is more optimal to approach it this way 🤔

app.js / app.ts

import MainComponent from './MainComponent.vue'
import SubComponent from './SubComponent.vue'

export default MainComponent

export { SubComponent }

AppComponent.vue

import MainComponent, { SubComponent } from '~/components/abstractComponent'

export default {
   components: {
       MainComponent,
       SubComponent
   }
}

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 correct method of implementing the "OnChange" event to a WooCommerce select element?

My task is to include the onchange="myFunction()" in the select menu below. However, because the select menu is part of woocommerce, I want to ensure that the onchange="myFunction()" remains intact even after updating my theme. How can I achieve this goal ...

Preserve the existing value and then check it against the updated value of a variable within JavaScript

I utilized an API that supplies me with information in JSON format, retrieved the price of a specific currency, and presented it on a screen using JavaScript. I encapsulated this process within a function that dynamically updates the information at set int ...

Display the element only when the request sent with getJSON exceeds a certain threshold of time in milliseconds

Here's a snippet of my JavaScript code: surveyBusy.show(); $.getJSON(apiUrl + '/' + id) .done(function (data) { ... surveyBusy.hide(); }) .fail(function (jqXHR, textStatus, err) { ... surveyBusy. ...

Issue with prop inheritance in componentInheritance problem with passing props to

How can I pass the state function setMyChoice as a prop to the GamePlay component in a rock paper scissors Nextjs application? The goal is to produce a value for comparison. In the Gameplay component, I then pass a prop to another component called PlayButt ...

Delete one object and then sequentially rename all remaining objects

object This is the object I retrieved. How can I remove module_1 object and rename the module object? For example, remove module_1 and rename module_2, module_3... to module_1, module_2... `{ "module_1": { "modulename": "mat ...

Enable automated addition or alteration of phone numbers through Twilio's API

I'm looking to programmatically add and edit phone numbers using the API on this Twilio link. Can you guide me on how to achieve this? ...

Express does not provide a 304 response code for static json files

I have a situation where I am utilizing express.static to serve a large static json file. It seems that express.static is always returning a 200 status code for the static json file, even when it remains unchanged. Given the file's size and the speci ...

using reactjs to dynamically render elements based on the selected condition in a select box

Is there a way to dynamically change an element based on the selected value of a dropdown in React? I'm looking for help with rendering conditions. Here's a snippet of my code: <Col span={12}> <Form.Item label='Qu ...

What are the steps for creating a new npm package based on an existing one?

I'm a newcomer to the node ecosystem and the npm package system. In my redux/react web app, I currently make use of the photoswipe package alongside react-photoswipe. Recently, I decided to extend the functionality of the photoswipe package by making ...

I'm having trouble running my React app on localhost. What steps can I take to fix this issue?

npm install -g serve serve -s build npm ERR! code EACCES npm ERR! syscall symlink npm ERR! path ../lib/node_modules/serve/bin/serve.js npm ERR! dest /usr/local/bin/serve npm ERR! errno -13 npm ERR! Error: EACCES: permission denied, symlink '../lib/nod ...

Tips for assigning a class name to a variable element within a react component?

I am interested in dynamically adding classes to an element. While I am familiar with methods using html-dom and passing a JavaScript expression to className, I am seeking a different approach. Is there a way to add classes similar to pushing them to an ar ...

Is there a way to shut down a browser tab without being asked, "Are you sure you want to close this window"?

Is there a way to gracefully close a browser window without being interrupted by the annoying Do you want to close this window prompt? I've noticed that whenever I attempt to use the window.close(); function, this prompt always pops up. ...

Generate an interactive pie chart with visually appealing animations using jQuery, without any actual data

My task involves creating a pie chart to visually display different categories. However, the challenge is that the chart does not contain any data, ruling out options like Google charts or other data-driven chart makers. As a solution, I have turned the pi ...

Storing Data with expressjs/session in NodeJS

My development project involves 3 files: app.js, index.js (routes), and Users.js (controller). Upon successful user login (verification between POST data and the database), I aim to store information in a session using expressjs/session. Below is how I c ...

Implementing a secure route in React that asynchronously checks user authentication status

Currently, I have a React app utilizing Auth from the aws-amplify package (version 5.3.8) and react-router-dom (version 5.3.0). Although there is a version specifically for React available, it necessitates using at least version 5.0.0 of react-scripts, whe ...

The error message "bind this is not defined in react" is displayed when attempting to read

Is it necessary to use bind in the constructor if you already have bind(this) in JSX? render(){ return( <input onChange={this.myFunc.bind(this)} type="text"/> ) } myFunc(){ alert('should trigger'); } I encountered an erro ...

html form submission error - please refresh

After submitting a form on my page, I keep encountering an error every time I try to refresh the page. I've attempted several solutions that I came across online, but none of them have resolved the issue as I continue to face this error on different ...

What is the most efficient way to transfer a large volume of documents from mongoDB using http?

I'm dealing with a large mongoDB database that contains millions of documents and I need to fetch them all at once without crashing or encountering cursor errors. My goal is to send this data over http using express in nodeJS. The collection contains ...

Error compiling: Cannot locate module '../../common/form' within 'src/components/time'

An attempt to import the file form.jsx into the file time.jsx resulted in an error: Error message: Module not found: Can't resolve '../../common/form' in 'src/components/time' //src //common //form.jsx //compon ...

Moving an item to the top of an array in JavaScript/Vue.js: A simple guide

const a = [{ "iso2": "KH", "countryName": "Cambodia", "nationality": "Cambodian" }, { "iso2": "KI", "countryName": "Kiribati", "nationality": "I-Kiribati" }, { "iso2": "KM", "countryName": "Comoros", "nationality": "Como ...