enhancement in bringing in ... from

Hey there, I've been working with Vue.js and came across an interesting challenge. In order to use components in Vue.js, you usually need to import them individually. I thought of a solution where we could create a module containing paths to all the components like this:

export default {
  bar: '@/components/chart/bar.vue',
  dashboard: '@/components/chart/dashboard.vue',
  gantt: '@/components/chart/gantt.vue',
  // ... other path
}

We can then use this module in our .vue files like this:

<template>
  <!-- ... some code -->
</template>

<script>
import path from '@/theGoodPath'
import bar from path.bar

// ... some code
</script>

However, it seems that this approach is not working as expected. It would have been convenient to manage component paths centrally in one file for easier maintenance.

What are your thoughts on this?

EDIT : I also tried using re-exporting, but encountered issues when changing my imports from:

<script>
import bar from '@/components/chart/bar'
import gantt from '@/components/chart/gantt'
import dashboard from '@/components/chart/dashboard'
import menuNav from '@/components/mini components/menuNav'

// some code
</script>

to:

<script>
import { bar, gantt, dashboard, menuNav } from '@/path.js'

// some code
</script>

This did not work in the second case. My path.js file looks like this:

export { default as bar } from '@/components/chart/bar'
export { default as dashboard } from '@/components/chart/dashboard'
export { default as gantt } from '@/components/chart/gantt'
export { default as menuNav } from '@/components/mini components/menuNav'

Answer №1

If you want to simplify your code, consider using re-export.

# file: @/theGoodPath
export { default as bar } from '@/components/chart/bar.vue'
export { default as dashboard } from '@/components/chart/dashboard.vue'
export { default as gantt } from '@/components/chart/gantt.vue'

When you use re-export, your code will be structured like this:

<template>
  <!-- ... some code -->
</template>

<script>
import { bar } from '@/theGoodPath'

// ... some code, make use of `bar` here
</script>

To learn more about re-exporting ES6 modules, visit this link.


Take a look at the live demo 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

Create a JavaScript code snippet that replaces the innerHTML of the function document.getElementById with

In my limited knowledge of JavaScript, I have come across an issue with the following code: function example() { document.getElementById("example").innerHTML = "<script>document.write(example)</script>"; } Unfortunately, this code doesn&ap ...

How can you hide all siblings following a button-wrapper and then bring them back into view by clicking on that same button?

On my webpage, I have implemented two buttons - "read more" and "read less", using a Page Builder in WordPress. The goal is to hide all elements on the page after the "Read More" button upon loading. When the button is clicked, the "Read More" button shoul ...

having difficulty placing 3 pop-up windows on a single page

Struggling to implement multiple popups on my page, each with a unique ID assigned. Any assistance would be greatly appreciated. Here is the code snippet: .fa { font-size: 50px; cursor: pointer; user-select: none; } .fa:hover { font-size:20px; t ...

The jQuery form validation feature surprisingly doesn't take into account the value of the submit button

I have implemented jQuery form validation from Here is an example from demo.html <form id="form-signup_v1" name="form-signup_v1" method="get" class="validation-form-container"> <div class="field"> <label for="signup_v1-username">Fir ...

Unable to replicate the exact Bootstrap template found on their website

Attempting to replicate a template from bootstrap.com, but encountering issues with the copied version. The navigation bar is turning white instead of remaining black, and I'm unable to change the color. Additionally, facing problems with the toggle ...

What techniques can be used to avoid blinking while forcefully scrolling to the left?

In my previous inquiry about dynamically adding and removing divs on scroll, I was unable to find a satisfactory solution among the responses provided. However, I decided to take matters into my own hands and attempted to implement it myself. My approach ...

Updating the object does not result in the interpolation value being updated as well

I am facing an issue with this v-for loop: <tr v-for="(product, index) in products" v-bind:key="products.id"> <div v-on:click="decrementQtd(index)" class="action-qtd-button-left"> <strong>-</strong> </div> < ...

Encountering a problem when utilizing the each loop within an ajax request

While attempting to iterate through a each loop within an Ajax call, I encounter the following error: TypeError: invalid 'in' operand e Here is my Ajax call code snippet: $.ajax({ type: "POST", url: "/admin/counselormanagem ...

how to load CSS and JS files on certain views in Laravel 5.2

Currently, I am facing a situation where I need to ensure that the CSS and JS files are loaded only on specific views in Laravel 5.2. Due to my boss's decision to eliminate RequireJS for loading JS files on our blade templates, we are now exploring a ...

Mastering the art of managing promises within nested loops

Embarking on my Promise journey, I find myself faced with a scenario where a list of objects within another list of objects needs to be updated based on responses from an external API. I've attempted to simulate the scenario below. The code snippet f ...

Retrieving JavaScript array data following a page reload

I am facing an issue with updating an array dynamically in my C# server-side code and then utilizing the updated data in a JQuery function on my webpage. When the page first loads, the array is filled with some default values by C#. However, when a user ma ...

Fade in and out of div elements upon clicking on an # anchor

I have been attempting to create a function where different divs fade in and out when clicking on an image with an <a href>. Unfortunately, I have not been successful in getting it to work despite several attempts. Here is the code that I am using: ...

Dealing with side effects in react/redux: Best practices and tips

Trying to find the best way to integrate an async side-effects handler into my react/redux setup has been quite a challenge. In my react-router-driven application, all the main containers at root level are smoothly dispatching actions and receiving update ...

Fatal error: artisan storage:link in Laravel terminated

the non-functional link and the generated file content I have established a directory called "storage" within the public folder and attempted to execute: artisan storage:link however, it results in the message "Killed". I am looking to store images in ...

Showing the output of an HTTP request in hapi.js version 17.2.0

I am attempting to showcase a page utilizing the latest version of HapiJS 17, which has a dependency on HTTP requests. Essentially, I am consuming a REST API and then returning it using the new response toolkit known as 'h toolkit,' but an error ...

Display a bootstrap toast using a JavaScript class method

One way to display a bootstrap toast is by using the following code snippet: let my_toast = new myToast('title', 'hello world'); my_toast.show(); Currently, I am loading the HTML content from an external file named myToast.html: < ...

Is there a way for me to import a variable from one file and use require() to access it in another file?

This is my current folder structure: collegesapp -- |-- node_modules -- express | -- connect | -- jade | -- passport |-- routes -- routes.js ...

Adding elements to a two-dimensional array using AngularJS

How can I assign the value of an input to tasks.name and automatically set status: false when adding a new item to the $scope.tasks array? HTML <input type="text" ng-model="typeTask"> <button ng-click="updateTasks()">Add task</button> ...

Heroku - JavaScript and CSS Updates Causing Loading Issues

Ruby version: 2.1.5 | Rails version: 4.1.1 For some reason, the changes I make to my JavaScript and CSS files are not reflecting on Heroku; it seems like the cached assets are still being used. I've tried removing the assets and precompiling them bef ...

Is there a way to select a checkbox in Google Forms using the console?

I need help with a script I'm creating to automatically populate Google Forms. I am able to select checkboxes using querySelector, but they don't have a .click() method or similar. How can I go about checking the checkboxes? ...