Leveraging the Axios content delivery network for Vue Template presentation

I am looking to extract JSON data using the Axios library. By utilizing my {{ id }}, I aim to target a specific parameter in the request.

Furthermore, I have an external script file for this purpose.

const Home = { 
    template: `

    <div class="user">
        <h2>user {{ id }}</h2>
        bet
    </div>

    `,
    props: {
        id: {
            type:    String,
            default: 'N/A'
        }
    } 
}

Key CDNs that are included:

<script src="<https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js>"></script>
<script src="<https://unpkg.com/vue-router/dist/vue-router.js>"></script>
<script src="<../../jobs/job.js>"></script>

The routing structure is relatively straightforward:

const router = new VueRouter({
        mode: 'history',
        routes: [
            {
                path: '/jobs/:id?',
                component: Home,
                props:     true
            },
        ],
    })

Many thanks!

Answer №1

There seems to be some confusion whether ID is a property or a route parameter in this scenario. It appears that you are utilizing it as a route parameter. If that's the case, and you're not directly passing it into the component, you could consider removing it and instead implementing a computed property to retrieve the ID. Then, include a method that executes your axios request. You can call this method within the mounted hook of your component.

Here's my perspective:

const Home = { 
  template: `

  <div class="user">
    <h2>user {{ id }}</h2>
    bet
  </div>

  `,
  mounted: function() {
    this.myRequest()
  }, 
  computed: {
    id: {
      get: function() {
        return this.$route.params.id
      }
    }
  },
  methods: {
    myRequest() {
      axios.get('/path/to/my/request', { params: { id: id }})
        .then(response => {
          console.log(response)
        }).catch(error => {
          console.log(response)
        })
    }
  }
}

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

Importing Vue Components

Despite numerous attempts, I am still unable to figure out how to efficiently import all Vue single file components from a directory at once. Allow me to elaborate... For instance, I have a directory named Add/Items where I have stored various Vue compone ...

Alignment of content layout across two wrapper elements

My goal is to achieve a specific layout where each pair of cards in a two-column layout is aligned based on the card with the largest content. Both cards share the same content layout and CSS. If you have any ideas or implementations that could help me ac ...

Tips for transferring a json-encoded variable from a Python function to an embedded JavaScript code within an HTML file using Flask

I am currently working on integrating a sensor into my website using Python and JavaScript. The main challenge I am facing is passing the data retrieved from Python into my JavaScript code for display on the website. @app.route('/sensor', methods ...

VueJs does not refresh the JavaScript when the browser back button is pressed

On a login page, I have integrated a Google reCAPTCHA. It functions properly initially, but if I navigate to another page and return using the browser's back button or by manually entering the URL, the reCAPTCHA fails to work. To get it functioning ag ...

Troubleshooting issues with using Laravel's history mode in conjunction with VueJS

Looking to create a seamless web single-page app using Laravel and VueJS but without the annoying "#" in the URL that appears when visiting a new page. Currently, I have: const router = new VueRouter({ mode: 'history', history: false ... ...

Unable to cancel $interval within factory

I created a factory for long-polling, complete with start and stop methods. However, I am struggling to cancel the timer. Any suggestions or ideas? app.controller("AuthCtrl", function($scope, $http, $window, User, Poller) { Poller.start(1, $scope.sess ...

YepNope - Holding off on progressing until all dependencies have been loaded

Within my ASP.NET Masterpage, I am utilizing YepNope to asynchronously load jQuery (from the Google CDN, with a local fallback) along with other scripts used across all pages on the site. I have included a ContentPlaceHolder in the MasterPage before the cl ...

Protecting website pages on both the admin and user side in Next.js 14 to ensure maximum security

I'm currently using nextjs 14 and I am working on developing a website similar to a property app. It will have an admin dashboard and a user side. How can I ensure the security of both the admin and user sides, and what should my folder structure look ...

What is the best way to convert a string to an object?

I wrote this code snippet: var regex={"$regex":req.query.query,"$options":req.query.options } db.collection('coders', function(err, collection) { collection.find( {"name":regex} ).toArray(function(err, items) { res.send(item ...

Step-by-step guide on building an endless reviews carousel

Can someone assist me in creating an endless reviews carousel? I've been grappling with it for the past two days. Just so you know, I'm a newcomer to web development and my code may not be the cleanest. Here's the code snippet: <!DOCTYPE ...

Is there a way to use setTimeout in JavaScript to temporarily stop a map or loop over an array?

data.forEach((d, i) => { setTimeout(() => { drawCanvas(canvasRef, d); }, 1000 * i); }); I have implemented a loop on an array using forEach with a one-second delay. Now I am looking to incorporate a pause and resume f ...

technique for retrieving div information from a different page with ajax

My goal is to use an ajax call in the first HTML file to change the content of a div from another HTML file's div. However, when I try this, the function alert shows "(object object)" and the content in the div doesn't change. Here is what I have ...

Pass a PHP variable as an array to a JavaScript file, and then print each variable using the echo statement

I have a table where each row contains a button to display more detailed data based on the row the user clicks (using datatables). The process involves using AJAX to pass data from a JavaScript file to a PHP file. In the PHP file, after some processing tha ...

Developing a terminate button: the ins and outs of halting a request in Node/Express.js

I am in the process of building an app and need to include a "Cancel" button. This button is intended to cancel a request that includes a Promise.all method, which typically takes a few minutes to complete due to API rate limiting constraints on the other ...

The checkbox's select all functionality is malfunctioning when employed with jquery

When the "select all" checkbox is clicked, it only selects the checkboxes on the particular page, instead of selecting all pages. This functionality is implemented using jQuery. The datatable contains hundreds of pages and data graph plotting is performe ...

Having difficulty updating an angular variable within a callback function

Currently, I am utilizing the Google Maps directions service to determine the estimated travel time. this.mapsAPILoader.load().then(() => { const p1 = new google.maps.LatLng(50.926217, 5.342043); const p2 = new google.maps.LatLng(50.940525, 5.35362 ...

Incorporate the variables specified in the parent PHP file into the PHP code being received through

I am currently working on a project that involves enabling a user to view his profile upon login. However, I am aiming to have the dashboard load all other profile pages through ajax, such as the view profile and edit profile pages. My progress so far inc ...

If the first <select> option is chosen, then a second <select> validation is necessary

Is it possible to make the second selection required for validation only if the 'yes' option is selected in the first one? <div class="form-group" ng-class="{ 'has-error' : articleEditForm.pnp.$invalid && !articleEditForm.pn ...

After making a request using axios, the background-image URL of a link cannot be modified through jQuery

Here is the code I have created. <html> <head> <title>TODO supply a title</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head&g ...

Having trouble getting Angular Filter to work within a directive expression?

I am currently working on a directive where I need to convert both the attribute and object name values to lowercase. I have tried using filters, as well as the $filter service, but it doesn't seem to be working. Can anyone please provide assistance i ...