I'm currently leveraging Vue.js and Python Flask for my backend development. I'm looking to establish some local variables. What is the best way to accomplish this?

Here is my Vue js file where I am utilizing two URLs from localhost. My goal is to create a configuration file that will allow me to make changes in one place and have those changes reflected throughout.

<template>
 <div>
  <div class="global-buttons">
  <a href="http://127.0.0.1:5000/sent_mail/book" target="_blank"><button>See Previously Sent Mail</button></a>
  <button @click="saveData()">Save Current Changes</button>
  <button @click="loadData(savedUrl)">Load Previously Saved Data</button>
  <a href="http://127.0.0.1:5000/mail/book" target="_blank"><button>Send Mail &#9992;</button></a>
 </div>
</template>

In Python, importing from one file to another is easy, but I'm unsure how to achieve this in Vue js. I have 3 Vue components where I use localhost URLs at 6 different locations. When I deploy the project, these URLs will need to be updated individually. This is why I am looking for a configuration file where I can centralize these URL changes.

I envision something like

config.js

URL = http://127.0.0.1:5000/

And then in my template

<a href="config.URL + sent_mail/book"

Answer ā„–1

To simplify your code, you can create an external object called config.js where you define your configurations.

export default {

    config: {
      url: 'myurl'
    }   

}

Next, import the config file into your component script.

import ConfigFile from '../config'

You can then access the configuration data in your component's data section like this:

data() {
  return {
   url: ConfigFile.config.url
  }
}

Finally, you can use the url variable in your template to display the URL.

<p>{{ url }}</p>

Answer ā„–2

If you are utilizing flask as your backend, it is recommended to incorporate config.js as a script prior to including the vue scripts.

const API_URL = http://127.0.0.1:5000/

By implementing this approach, you will be able to access the API_URL variable within your vue files seamlessly.

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

Changing an array in PHP to a variable in JavaScript

I am struggling with converting a PHP array into a JavaScript variable using json_encode. When I print out my PHP variable: <?php $damage_array = $listing->tire_detail->damage_details; ?> It displays as: Array ( [lf] => 4 [rf] => 9 [lr ...

What causes jquery height and javascript height to both be returned as 0?

I'm facing an issue with a visible div on my screen - despite being visible, its height always returns as 0. I've attempted various jQuery and JavaScript methods to retrieve the height, but it consistently shows as 0. Here's the structure of ...

Is it possible for me to verify the login status of an Auth0 user within my custom NextJS _app.js file?

Currently working on a NextJS application with nextjs-auth0 for authentication, which is completely new to me. I followed the documentation's suggestion and wrapped my _app.js with UserProvider, also using getInitialProps to set a global online/offlin ...

concealing the upper header while scrolling and shifting the primary header upwards

Is there a way to use CSS to move the main header navigation, including the logo and links, on my website up when scrolling down in order to hide the top black bar header that contains contact information? The website in question is atm.truenorthmediasol ...

Combining Data in Django using Ajax with JavaScript and Python Integration

I am in need of assistance understanding a basic ajax call from JavaScript to Python. I have a Python function that takes a simple parameter and returns a result. My goal is to send this parameter from JavaScript, receive the function result back in JavaSc ...

Some components react to history.push() with react-router-dom while others simply don't seem to respond

As the title states, I am using React-router-dom in my App.js file with a Router containing multiple Routes and a Switch. I have been successful in manipulating history and navigating my app using useHistory and history.push() in smaller components. Howev ...

Incorporating string input values into a function

I am currently working on a function that retrieves the value of an input upon clicking a button. My goal is to then have another event that will incorporate that value into a function when the button is clicked. var getInput = function() { $('#inpu ...

What is the best method for deleting an uploaded image from a box?

My code is quite lengthy, so I'll just showcase the JavaScript portion here. Here is a snippet of my JavaScript code: <script type="text/javascript"> let current = 0; for(let i = 0; i < 5; i++) { $('#thumbnail-view- ...

JavaScript plugin specifically designed to enable playback of media content, such as videos from

I am working on a spring-based project and I require the ability to play audio/video in various formats similar to YouTube. I am currently searching for a free JS plugin that would be the best fit for my needs. Any suggestions or recommendations would be ...

express-session is failing to maintain persistence and refusing to transmit a cookie to the browser

I am currently developing a web application using the MERN stack (React, Node.js, Express, and MongoDB). I have integrated express-session in my Node.js project, but for some reason, I cannot see the connect.sid cookie in the browser. Additionally, it appe ...

Choose a random element from a string with Javascript

Could someone please help me figure out why my code isn't functioning as expected? I'm trying to randomly select three names from a list and ensure that no name is repeated. While I believe I am on the right track, something seems to be missing. ...

What is the proper way to transfer data to PHP using AJAX?

I'm facing an issue while trying to send FormData to a PHP file. When I press submit, nothing happens. However, when I send data separately, it works fine. <form method="post" class="code_form" enctype="multipart/form-data& ...

What is the best way to save numerous records using the saveRecord function in nlapi methods?

After customizing the default promotion form, I implemented a feature where clicking on a specific promotion triggers dynamic elements to be displayed using jQuery. Since the fields or records for this requirement haven't been created yet, multiple re ...

Make sure that the function displays the output once it has been received using the jQuery.get method

This script fetches data using the $.get method from an XML file on an ASP ashx server: $.get("http://www.example.com/example.ashx", { From: txtFrom, To: txtTo, Date: txtTime }, function (data) { var arr ...

What are the steps needed to establish a distinct data scope in AngularJS along with the application of filters?

Iā€™m currently practicing my skills in AngularJS and I have been experimenting with creating reusable components. At this stage, I have successfully created a component using the directive method. However, I now have some doubts that I need clarification ...

"Allow users to select only the year with Material-UI DatePicker

Is there a method to display solely years using the Material UI DatePicker component in React? I would like to enable users to choose only the year, excluding months or days. Please note that neither the openToYearSelection option nor the autoOk feature ...

Unlock the power of React Testing Library with the elusive waitFor function

I came across an interesting tutorial about testing React applications. The tutorial showcases a simple component designed to demonstrate testing asynchronous actions: import React from 'react' const TestAsync = () => { const [counter, setC ...

Creating dynamic routes in React by pulling data from a JSON file

Creating multiple routes based on JSON data file is the goal: routes.jsx import React from 'react'; import { Route, Router, IndexRoute } from 'react-router'; import ReactDOM from 'react-dom'; import App from './index.j ...

What could be the reason why my JavaScript code for adding a class to hide an image is not functioning properly?

My HTML code looks like this: <div class="container-fluid instructions"> <img src="chick2.png"> <img class="img1" src="dice6.png"> <img class="img2" src="dice6.png" ...

ajaxStop and ajaxStart not functioning as expected

Currently working on a project to create a Wikipedia viewer, and running into an issue with the following code snippet: $(document).ready(function() { $(document).on('click', '#search_button', function() { ...