Bringing in a variable to a Vue file

I am trying to import a variable into my Vue component file using the following method:

require('imports-loader?myVar=>{test:12345}!./my-com.vue');

However, I am receiving the error message

[Vue warn]: Error in render function: "ReferenceError: myVar is not defined"
.

While I am aware of using props, I specifically need to pass a variable.

This is how the content of "my-com.vue" looks like:

<template>
  <div>...</div>
</template>
<script>
  console.log(myVar); // <--- this triggers the vue warn
  export default {
    props:['rows'],
    ...
  }
</script>

The import statement can be found here:

module.exports = function(params){
  return function(resolve, reject){
    resolve({
      components:{
        'my-com': require('imports-loader?myVar=>{test:12345}!./my-com.vue')
      },
      ...
    })
  }
}

What am I doing wrong?
How can I successfully import a variable into my Vue component file?

Thank you for your help.

Answer №1

To include a variable from another file in your current JavaScript file, you can employ the import statement.

<script>
  import { myData } from './path_to_the_external_file';
  console.log(myData); 
  export default {
    props:['info'],
    ...
  }
</script> 

Ensure that in the external file you are exporting myData using the export statement like so:

export var myData = 'some data value';

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

Guide to implementing 301 redirection from HTTP to HTTPS in Next.js

What is the process for implementing a 301 redirection from HTTP to HTTPS in Next.js? One example of this would be redirecting from "http://stackoverflow.com/" to "https://stackoverflow.com/". ...

Using AJAX along with the append method to dynamically add identical HTML content multiple times to a single element

Although I have successfully implemented most of the desired functionality with this JavaScript code, there is a persistent bug that is causing unnecessary duplicates to be created when appending HTML. Detecting Multiples The problem lies in the fact tha ...

Cannot extract the 'name' property from 'e.target' because it is not defined

I encountered an error message stating that I cannot destructure the property 'name' of 'e.target' because it is undefined within the createform() method. Despite highlighting the line causing the error, I am still unable to comprehend ...

Why is my jQuery animate function with arrow function not working?

I am facing an issue with my animate method using an arrow function within an Angular class. The method does not seem to be executing properly when utilizing the this.innerWidth property to obtain the device width. Any assistance would be greatly appreci ...

Leveraging a callback function with Array.Filter() to perform comparisons between two arrays

Instead of using an anonymous function, how can I convert the statement arr2.includes(element) into a named function that can be passed in? const ArrayOverlap = (arr1, arr2) => { let newArr = []; return newArr = arr1.filter(element => arr2.i ...

What is the best way to handle a specific submit button using jQuery/Ajax?

I created a web page with 4 submit buttons that all call the same PHP page process.php using jQuery/Ajax. I trigger this PHP page using the onClick event as shown below: <div class="col-md-12"> <input type="hidden" name="_token" value="<?p ...

Can AngularJS Filters be used to convert a number into a string, but not the other way around?

After researching Angular JS filters, I discovered that the number filter is used to format a number as a string. However, there doesn't seem to be a built-in filter for converting a string to a number. In an attempt to solve this issue, here is so ...

Tips on choosing a text option from a selection menu

When I try to retrieve the text displayed in my select menu upon selection, I encounter a challenge. While this.value allows me to fetch the value, both this.label and this.text return as undefined. Is there a method to extract the visible text from the d ...

When using a custom AJAX function, make sure that bindings are functioning properly when setting properties within a callback in Ember

I've encountered some unexpected behavior while trying to set an Ember property inside a custom AJAX function callback. The AJAX function is triggered in the route, as seen in the code snippet below. The success callback updates the 'session.aja ...

Integrating mermaid into a Vue component using webpack

I am currently in the process of migrating a Vue project that utilizes mermaid from CDN to webpack. As someone who is new to this, I decided to use npm install save mermaid in the project folder. When I load mermaid cdn as static js (in /public/index.html ...

I am using JavaScript to randomly choose a function, but once it's selected, the code within it does not execute

I am attempting to randomly choose a function from an array that contains various functions. While the console is showing me the selected function as "[Function: cycle3]," the code within the chosen function is not being executed. How can I ens ...

Create random animations with the click of a button using Vue.js

I have three different lottie player json animation files - congratulations1.json, congratulations2.json and congratulations3.json. Each animation file is configured as follows: congratulations1: <lottie-player v-if="showPlayer1" ...

Having difficulty retrieving the initial selection value

Despite receiving all other values, I am unable to retrieve the event.everyday value. Can you please help me identify what I might be doing incorrectly? Situation - Choose Every day from the dropdown menu. Click on the "click it" button and review the ...

In JavaScript, attempting to trigger a click event on an element with a <ul> and <li id> and <a href>, but only after the <li id> has been selected

Apologies if this question is too basic, I've been struggling with it for some time... I am trying to trigger an <a href> from within a tag. I have found several methods to do so, however... the entire script is contained within a <ul>, ...

What could be the reason that the elements are not being written to a file in this for loop?

Check out my code below: var data = require('./campSample.json'); var dataArray = data.resultset.result; var fs = require('fs'); for(var i = 0; i < dataArray.length; i++){ fs.writeFile("./campList", dataArray[i]["-facilityName ...

Troubleshooting: Difficulty assigning a value to a nested object in Angular

I'm a beginner in Angular and TypeScript so please forgive me if this question seems silly. I am attempting to store the value of a returned object into a nested property within an object with a type of any. Unfortunately, it is not allowing me to do ...

Incorporating map reduce and other methods, is it possible to locate the initial item that meets specific criteria within a nested array and terminate the search upon discovery?

How can you locate the first item that meets specific criteria within a nested array and halt the search once it is found? In a one-dimensional array, this task can be accomplished with the Array.find function. But how would you tackle this in a two-dimen ...

Whenever I attempt to log the retrieved data using console.log, it shows up as undefined initially. However, it eventually

One of the functions in my React app is used to fetch data from a Django API: const [filterData, setFilterData] = useState([]); const fetchFilterData = async () => { const filter_url = 'http://127.0.0.1:8000/api/filters/' try { ...

Using radio buttons in a popup on Ionic framework

Controlling Food Choices .controller('FoodController', function($scope, $ionicPopup) { $scope.favorite = { 'food': 'egg' }; $scope.setFavoriteFood = function() { var popup = $ionicPopup.show({ &ap ...

Efficiently Extracting Information from JSON Objects

Currently, I am in the process of parsing a JSON file. Let's assume that the JSON data looks like this: {"data" : [ {"ID":12, country: "UK"}, {"ID":13, country: "USA"}, {"ID":14, country: "BRA"}, ]} Instead of just having three entries as show ...