Bringing in a Native JavaScript File to Your Vue Component in Vue Js

After developing a frontend application using Vue Js, I encountered the need to integrate a native JavaScript file into one of my Vue components.

This native js file contains various utility functions that I would like to access and use within my Vue component.

The native js file is named util.js

var helper = function(t) {
                var u = 'product',
                    A = 'version',
                return {
                    buildHelpUrl: function(a, b, c) {
                                   return "http://"
                                 }
               /* Snippets */
             }

I attempted to import the native js file into my .vue file using two different methods:

Case 1: import OTHHUrlBuilder from '@/util.js';
Case 2: import * as urlbulider from '@/util.js';

Unfortunately, neither approach was successful. I am seeking guidance on the correct way to import and utilize the functions provided in the native js file. Your assistance is greatly appreciated!

Answer №1

Start by exporting all the necessary functions to your .vue file.

For example, in your .js file:

const addFunction = (num1, num2) =>  {
   return (num1 + num2)
}

const subtractFunction = (num1, num2) =>  {
    return (num1 - num2)
}

export { addFunction, subtractFunction }

Then, you can import these exported functions into your .vue file.

In your .vue file:

<script>

import { addFunction, subtractFunction } from '@/util.js'

 ...

If you only have one function to export, you can use export default

In your .js file:

const singleFunction = (text) => {
          var x = 'example',
                    y = 'output',
                return {
                    generateResult: function(a, b, c) {
                                   return "http://"
                                 }
               /* Additional logic */
             }

    export default singleFunction

and in your .vue file:

import singleFunction from '@/util.js'

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

Implement a procedure for printing a page once the columnize operation has been completed

Hello, I have run into a problem that I need help with. Currently, I am trying to print a page after the function "columnize" has finished its task. However, the print function is executing before the columnize function completes. How can I ensure that t ...

Creating an HTML Canvas using an AngularJS template

I am facing an issue with rendering html elements on a canvas using html2canvas JS. I am utilizing AngularJS for data binding on the html page, but unfortunately, the dynamic data is not showing up on the canvas generated from these html elements. For inst ...

What is the best method to extract information from JavaScript tables using Python and Selenium?

As a beginner in Python, JavaScript, and Web-Scraping, I am facing the challenge of extracting data from tables on a specific webpage (https://www.mcmaster.com/cam-lock-fittings/material~aluminum/) and saving it into a csv file. Initially, I attempted to ...

Retrieve the most recently added item in the Material-ui Autocomplete component

Currently, I am incorporating the material-ui Autocomplete component with multiple selection feature. In one specific scenario, I require the ability to retrieve only the value of a newly added item. Despite utilizing the onChange listener, the event pro ...

Determining the scrollWidth of a div with an absolutely positioned child div

Having some trouble determining the width of a div's content instead of the div itself. The typical solution would involve using Javascript's scrollWidth property. However, there is a complication in this case. Inside the div, another div is ab ...

Invalid content detected in React child element - specifically, a [object Promise] was found. This issue has been identified in next js

Why am I encountering an error when I convert my page into an async function? Everything runs smoothly when it's not an async function. The only change is that it returns a pending object, which is not the desired outcome. This is how data is being f ...

What sets srcset apart from media queries?

Can you explain the contrast between srcset and media query? In your opinion, which is more optimal and what scenarios are ideal for each? Appreciate it! ...

Incorporating a JavaScript advertisement zone within a PHP function

Currently in the PHP template, I am trying to embed a JavaScript ad zone inside a function in order to have control over each page's ad placement. Here is what I have: <?php if(is_page('welcome-president')) { oiopub_b ...

Double Calling of Angular Subscription

I am currently working with a series of observables that operate in the following sequence: getStyles() --> getPrices() Whenever a config.id is present in the configs array, getStyles() retrieves a style Object for it. This style Object is then passed to ...

Using Vue's $set method to update nested JSON values

[ { "id": 1, "question": "Main Inquiry", "answers": [ {"id" : 1, "isSelected" : false}, {"id" : 2, "isSelected" : true}, {"id" : 3, "isSelected" : false}, {"id" : 4, "isSelected" : false}, {"id" : 5, "isSel ...

The installation of vue-cli failed due to permission issues with error code EPERM in npm

I keep encountering an error message when I try to run npm install -g vue-cli: npm ERR! path C:\Users\End User\AppData\Roaming\npm\node_modules\vue-cli\node_modules\nan\package.json npm ERR! code EPERM ...

How can I detect a change in user input using jQuery?

When utilizing jquery .oninput with an element, the event will trigger instantly whenever there is a change in the input value. In my scenario, it is necessary for me to validate the input value by calling the service immediately after any changes occur. ...

Can ajax requests be made without a web browser?

Currently, I am in the process of developing JavaScript tests using mocha and chutzpah, which means conducting all my tests without a browser. However, I have encountered an issue where all my AJAX calls are resulting in empty strings. Even when using the ...

How to retrieve and delete a row based on the search criteria in an HTML table using jQuery

Here is the HTML code snippet: <table class="table" id="myTable"> <tr> <th>Type</th> <th>Counter</th> <th>Remove</th> <th style="display:none;">TypeDistribution</t ...

Transforming an AJAX call into a reusable function

My goal is to simplify my ajax calls by creating a function that can be reused. Although I'm unsure if I'm doing it correctly, I would like to attempt this approach. <script> $(document).ready(function(){ var reg_no=$("#reg_no").va ...

Error encountered while rendering page in vue 2 due to undefined getters

Here is my global state and shared component where I set and define getters: export default { getters: { loading(state) { return state.loading; }, statusMessage(state) { return state.statusMessage; }, }, } The store expor ...

What is the process for obtaining the dimensions (height/width) of a particular instance within a dynamically rendered list?

[QUESTION REVISED FOR CLARITY] I am attempting to retrieve the dimensions, specifically the width and height, of a rendered <div/> within the Child.js component. While I found a helpful example on Stack Overflow, my scenario involves multiple dynami ...

Is there a way to retrieve a promise from a function that triggers a $http.get request in AngularJS?

Looking for a solution to modify this function: getX: function ($scope) { $http.get('/api/X/GetSelect') .success(function (data) { ... ... }) .error(function (data) { ...

Tips for sending an icon as a prop in React components

I'm struggling to implement an icon as a prop while using props for "optionText" and "optionIcon". The optionText is working fine, but I'm facing issues with the OptionIcon. File where I'm creating props import Icon from ...

Guide: Installing Vuetify 2.5.6 on Vue 3

take a look at the code snippet from my main.js file: import { createApp } from 'vue' import vuetify from './plugins/vuetify' import App from './App.vue' const app = createApp(App) app.use(vuetify) app.mount('#app' ...