Storing a variable in Vue on one page and accessing it on another page

Is there a way to transfer a value from config.js file to Geturl.vue?

The content of the config.js file is as follows:

 var myUrl = "http://localhost:7070/#/";

The code snippet in my view file (Geturl.vue) is shown below:

<div id="app>>
 <p>{{loadNewUrl}}</p>
</div>

Here is the script for Geturl.vue:

<script>

  import config from '/config.js';

  export default {
  name: 'App',
  components: {
  },

  data(){
    return{
      loadNewUrl: myUrl,  /** How can we retrieve the value of myUrl from config.js file? **/
    }
  }
</script>

Answer №1

In order to access a variable from config.js in your Geturl.vue file, you need to first export the variable in config.js and then import it into Geturl.vue:

config.js

export var myUrl = "http://localhost:7070/#/";

Script for Geturl.vue

<script>

  import { myUrl } from '/config.js';

  export default {
  name: 'App',
  components: {
  },

  data(){
    return{
      loadNewUrl: myUrl,  /** how to get myUrl value from config.js file **/
    }
  }
</script>

For more information on import/export in JavaScript, you can refer to the following resources:

By using export, variables in JavaScript files can be made "public" and accessible from other files.

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

A guide on sending JavaScript variables to PHP using AJAX

I've been facing an issue while trying to pass the selected month from a select list in HTML using Ajax. Every time I select a month, it doesn't show up as expected. Initially, there's a placeholder "nothing" displayed due to the if-else con ...

On initial load, React router switch fails to find a match

I am encountering an issue with my switch and react-router. Whenever I open a new tab and navigate to any path, it always redirects me to /login. However, if I remove the bottom switch (the one without a path - the default), everything works as expected. A ...

Adding style using CSS to a dynamically generated table row in Vue.js

Currently, I am working with a table that dynamically generates rows using v-for: <table> <tr><th class='name'>Name</th><th>Surname</th></tr> <tr v-for='data in datas'><td class=&a ...

Exploring Vue.js and its various layout options

If you have 3 different layouts for your app (e.g. one for customers, one for employers, and one for the admin interface), how would you go about laying out the router-views? Currently, I have implemented the customer view like this: <template> & ...

Mastering the use of npm and sails to create HTML-PDF files effortlessly

UPDATE: I am simplifying my question and will address any other concerns in separate posts if necessary. The initial post was too lengthy, hoping for a straightforward guide on utilizing sails to its fullest potential. Apologies. To begin with, my knowled ...

I'm confused by the function: "sort((a: Article, b: Article) => b.votes - a.votes);"

I'm having trouble grasping the concept of the return statement in sortedArticles(). Can anyone provide me with a resource or explain how this sorting function works? sortedArticles(): Article[] { return this.articles.sort((a: Article, b: Article ...

Is it possible to use multiple schemas for one collection name?

I am currently working on creating different schemas for a single collection, such as User or subUser. I aim to store both User and subuser data in the same collection but with different schemas. Here is an example of my schema file: export const AryaSchem ...

Problems with JQuery UI buttonset behavior

As I develop a UI application utilizing JQuery UI components, I encounter an issue with aligning radio buttons within the interface. While using JQuery buttonset in isolation functions correctly, integrating it with other UI elements causes misalignment: ...

The HTTP request arrives with no content within the body

I am in the process of developing a basic client-server application using Node and Express. The goal is for the program to receive a JSON input on the client-side, perform some operations, and then send data to the server-side. Currently, I am able to sen ...

Trouble with translating code from JavaScript to Python due to hash algorithm discrepancy

In an attempt to achieve consistent hash function results between JavaScript and Python, I encountered a roadblock when trying to convert my JavaScript function to its Python equivalent, resulting in unexpected outcomes. Original JavaScript Function: func ...

An undefined variable was encountered within the 'else' statement

I am encountering an issue with my code where it is returning an 'undefined' error. The problem arises when I attempt to remove an id from an array using the variable 'id', but instead, it throws an error stating 'undefined'. ...

The radio button is displaying the text 'on' instead of its designated value

function perform_global(tablecounter) { for (index = 1; index <= 2; ++index) { var dnsname = "dns_name"+index; oRadio = document.getElementsByName(dnsname); alert (" radio ID " + dnsname + " " + index + "length " + oRadio.leng ...

Coding with a combination of JavaScript, AngularJS, and manipulating brackets

I am currently performing the following action: myArray.push(pageCount); After that, I end up with something like this: $scope.myArray = Pages.getAllPageCount(); Finally, when I utilize AngularJS to display it in my .html file. {{myArray}} If there i ...

Vows.js: Utilizing data from parent topics in nested topics

Is there a way to access the return value of an outer topic from within a test in an inner topic? To clarify, consider this example: "build.css" : { topic : function(file) { fs.readFile(fixtures + "/public/build.css", "utf8", this.callback); }, ...

Create new instances of Backbone Models using an existing Backbone Model as a template

Within my app, I am planning to include a configuration file called config.json as a Backbone Model. Here is an example of how it will be loaded: var Config = Backbone.Model.extend({ defaults: { base: '' }, url: 'config. ...

Using Node.js and Express to import a simple JavaScript file as a router

Can anyone help me understand how to import the user.json json file into my user.js? I want the json file to be displayed when typing /user but I'm struggling with the new version of Node. index.js import express from 'express'; import body ...

Develop a React component that organizes an array based on specified index ranges in JavaScript

Imagine having an array structured like this: let numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17, 18, 19]; I am interested in creating a React component that displays the elements of the array in groups of 10. The desired output should look like t ...

Tips for incorporating various color schemes into all sides of a shape generated through the extrude geometry feature in three.js

I have recently designed a wall-like structure using extrude geometry and now I want to assign different colors to each side based on the settings below: var extrusionSettings = { curveSegments:5, steps: 10, amount: 200, be ...

Using v-for to pass two properties to a single component in VueJS

Hey there! I'm trying to create a v-for loop with a component that has two different props COMPONENT <template> <div class="bg-light rounded p-2 px-5"> <h5> {{ number }}</h5> <h3>{{ item }} ...

Using 'require' within a nested directive that relies on the parent directive in AngularJS

Implementing a sub directive to be utilized in multiple directives is my current challenge. These parent directives share a common controller that has useful methods for updating scope variables within these directives: (potentially changing controllers ...