Strategies for Repurposing local file.js across multiple Vue projects

I have a file called myfile.js with functions that I want to reuse in multiple vue projects, specifically within the App.vue file.

Here is the file structure:

-- projec1
---- src
------ App.vue

-- project2
---- src
------ App.vue

-- myfile.js

Directly importing it like this does not work:

import * as alias from '../../myfile.js'

as the file path cannot be found during the production build resulting in an error message:

ERROR Failed to compile with 1 error3: 21: 38 PM
This relative module was not found: ../../myfile.js

Is there a simple solution to solve this issue? Perhaps something similar to defining a local dependency in package.json:

{
    myLocalDependency: path/to/myFile.js
}

Then in App.vue we could import and use it like this:

<script>
import myLocalDependency from '...'

export default {
     mounted: {
         myLocalDependency.myPreciousFunction()
     }
}
</script>

Answer №1

If you have a simple file.js that needs to be integrated into your Vue.js application, you can follow the steps outlined in Adding Instance Properties. For instance, in your main.js file, include the following code:

function customFunction(){}
Vue.prototype.$customFunction = customFunction();

Afterwards, you will be able to access this function anywhere in your Vue project using this.$customFunction

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

Is it possible to determine if a selected date falls within the current week using JavaScript?

Currently facing an issue with JavaScript. I have multiple dates retrieved from a database, and I need to extract the date that falls within the current week. ...

Error with Laravel and Vue template integration

I have recently started using Vue in Laravel 5.3 and I am able to retrieve data through a jQuery AJAX request within Vue. However, I am facing difficulties with displaying the data. Below is my current script in the view: <li> <!-- inner men ...

The default route in Laravel is not functioning properly in conjunction with Vue.js

I am currently working on a single page application with Laravel and Vue, but I'm facing an issue where the index component is not loading upon initial page load. In my web.php file: Route::get('{any}', function () { return view('welco ...

Harnessing the power of VUE.js: Exploring the versatility of V-bind with Array and

I've encountered an issue with an app I'm currently working on. In my project, I am utilizing Vue.js to create the front-end and attempting to apply two classes to a div within a v-for loop. The first class is bound with a filter that functions ...

Utilize the toString function along with the substring method within an ejs template

I am attempting to showcase an employee's ID by displaying only the last four digits in a table on an EJS page. The data is stored in MongoDB and I am using Express for handling routes. Here is my Express route: app.get('/routehere', async ...

Extracting information from a JSON data within an API

How can I retrieve data with a name tag from JSON data using Ajax? The function showCard is not functioning properly when I try to grab data with a name tag. My goal is to display the name of the API data when an img element with the class found is clicked ...

Trigger an action in the clean-up function of useEffect

I am facing an issue with a form component in a Material UI <Tab /> where users can update their address information. The data is fetched from the API using redux-thunk, and the form fields are pre-filled with server data before the update occurs. h ...

Creating a replica of a Parse Server object

I've been struggling to clone Parse objects without affecting the original ones. Despite trying various methods like Parse.Object.clone() and converting to JSON, I haven't found a working solution. I came across some recommendations online but no ...

Adding items dynamically to a React-Bootstrap accordion component can enhance the user experience and provide a

I am retrieving data from a database and I want to categorize them based on "item_category" and display them in a react-bootstrap accordion. Currently, my code looks like this: <Accordion> { items.map((item, index) => ...

Adding a QR code on top of an image in a PDF using TypeScript

Incorporating TypeScript and PdfMakeWrapper library, I am creating PDFs on a website integrated with svg images and QR codes. Below is a snippet of the code in question: async generatePDF(ID_PRODUCT: string) { PdfMakeWrapper.setFonts(pdfFonts); ...

Utilizing the <slot> feature in Angular 5 for increased functionality

Currently, I am working on a single page application (SPA) where Vue framework has been utilized for development purposes. Front-End: Vue Back-End: NodeJs Within my application, there are other sub-modules built in Angular 4. I am looking to replicate th ...

Mapping keys in a react component for efficient sorting

I'm facing some challenges when attempting to reorder keys from a state within my map function in React.. The original output is: monday : {1200: {…}, 1500: {…}, 1800: {…}, 2000: {…}, 2200: {…}, 0000: {…}, 0100: {…}, 0500: {…}, 0600: ...

Using canvas to smoothly transition an object from one point to another along a curved path

As a beginner in working with canvas, I am facing a challenge of moving an object from one fixed coordinate to another using an arc. While referring to the code example of a solar system on https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutori ...

Creating a span element to replace the list item class in a JavaScript Modal Pop-up

I recently set up a portfolio website that showcases my projects. When you click on a project, a modal window opens with a carousel gallery inside. On my .html page, the projects are organized like this: <li class="Project" data-modal="myModal_1"> ...

The Angular Google Maps Directive zooms in too much after the "place_changed" event has fired

Currently, I am developing a store locator app for DHL accessible at storefinder.hashfff.com/app/index.html For this project, I decided to utilize the angular-google-maps library for its features. However, in hindsight, working directly with the Google Ma ...

Is Socket.io combined with Redis the best architecture for an interactive gaming experience?

My current setup involves a NodeJS scaling architecture with Redis, specifically designed for a real-time multiplayer game. However, I'm facing challenges in configuring separate lobbies for players, as the load balancer assigns users to different ser ...

What could be causing wavesurferjs to exceed the boundaries of its parent div?

I need assistance with my wavesurferjs issue The waveform is overflowing the parent div This problem occurs for the first time and upon resize of the parent div Upon resizing, it should automatically adjust to fit the parent div Question: When the pare ...

Indicate in the package.json file that the installation of a package must exclude any optional dependencies

I have developed a chat bot for Discord using the discord.io library. Although this library does support voice-related automation, I found that by adding the --no-optional switch during installation with npm, unnecessary features can be avoided, saving val ...

Deleting outdated files in a temporary uploads directory - NodeJS best practices

My process for removing old files from a tmp upload directory involves the code below: fs.readdir( dirPath, function( err, files ) { if ( err ) return console.log( err ); if (files.length > 0) { files.forEach(function( file ) { ...

Pass data from a higher-level component to a pop-up modal component, make modifications, and then save the changes to the database using

When working with two components in vue3.js, let's examine the following: PostsComponent.vue <template> <table> <template v-for="attributes in posts"> <tr> <td>{{ attributes.attributes.title }}& ...