Struggling with integrating Axios with Vue3

Can someone assist me in figuring out what is going wrong with my Axios and Vue3 implementation?

The code I have makes an external call to retrieve the host IP Address of the machine it's running on...

<template>
  <div id="app">
    <Incoming :hostname="hostname" />
  </div>
</template>

<script lang="ts">
import axios from 'axios'
import CustomerWaiting from "./components/Incoming.vue";
export default {
  name: "app",
  components: { Incoming },
  data() {
    return {
      hostname: ''
    }
  },
  mounted() {
    axios.get('https://api.ipify.org?format=json')
    .then((response) => {
      this.hostname = response.data.ip
    })
    console.log(this.hostname); // Added for debugging purposes.
  }
};
</script>

This data is then passed to a prop which is further used by a child component via v:bind. The child component utilizes the data to create a URL that sends commands to an externally connected device.

<template>
    <button @click="hello">HELLO</button>
    <button @click="timePlease">TIME PLEASE</button>
    <button @click="outtaHere">I AM OUTTA HERE</button>
</template>

<script lang="ts">
    import axios from 'axios';
    let self = this;
    export default({
        name: 'waiting',
        props: ['hostname'],
        methods: {
            hello() {
                axios({
                    url: `http://${self.hostname}:8989/?action=doSomething&additional=true`,
                    headers: { accesstoken: 'myAccessToken' }
                });
            },
            timePlease() {
                axios({
                    url: `http://${self.hostname}:8989/?action=doSomething&additional=true&anothere=true`,
                    headers: { accesstoken: 'myAccessToken' }
                });
            },
            outtaHere() {
                axios({
                    url: `http://${self.hostname}:8989/?action=doNothing`,
                    headers: { accesstoken: 'myAccessToken' }
                });
            }
        }
    });
</script>

Despite following multiple online tutorials, I am encountering some errors which are perplexing me. Any guidance on where I may be going wrong would be highly appreciated since I'm relatively new to both Axios and Vue3.

I've attempted various solutions related to the error but haven't been able to sort it out.

Answer №1

Eliminate the parentheses after export default

export default ({
    name: 'waiting',
    props: ['hostname']
    ...
})

Update it to:

export default {
    name: 'waiting',
    props: ['hostname']
    ...
}

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

Is there a way in typescript to transform empty strings into null values?

As a newcomer to TypeScript, I have been exploring it for some time now. I am working with two interfaces, one is fetching data from a database that I do not have control over and cannot guarantee the values returned are as expected. // Retrieved from the ...

Utilizing Sequelize with Typescript for referential integrity constraints

After defining these two Sequelize models: export class Users extends Model<Users> { @HasMany(() => UserRoles) @Column({ primaryKey: true, allowNull: false, unique: true }) UserId: string; @Column({ allowNull: false, unique: tru ...

Placing files in the "Dist" folder is causing an issue by disrupting the functionality of the Angular 2 app

For testing my login component in Angular2, I am using a mockBackend within my app. Initially, the standalone version of the login worked perfectly fine. However, when trying to integrate it into my ongoing development project, I encountered an issue. Duri ...

The function parameter in Angular's ngModelChange behaves differently than $event

How can I pass a different parameter to the $event in the function? <div class='col-sm'> <label class="col-3 col-form-label">Origen</label> <div class="col-4"> <select ...

Ensuring type compatibility in a declarative manner: What are the methods?

My goal is to establish a compile-time constraint that ensures a variable of type T2 can be assigned to a variable of type T1. If I have a value (t2) of type T2, I can simply do the following: const t1: T1 = t2; Is there an alternative method to achiev ...

Dynamic Setting of Content-Type Header (Multipart/Data) Through Http Interceptor

I have been trying to upload a CSV file using an HttpInterceptor as a middleware. Everything works fine for normal requests, but I need to modify the request header to 'multipart/data' specifically for CSV uploads. Below is the code snippet: ex ...

Filter a two-dimensional array based on the presence of a string value found in the second array

My logic for this assignment is not very good, as I need to filter a 2D array based on the values of another array. Let me provide an example of the 2-Dimensional Array: const roles = [ ['roles', 'admin', 'write'], ['ro ...

What could be the reason behind the validation error occurring in this Laravel 8 and Vue 3 application?

Currently, I am developing a registration form with a Laravel 8 API and integrating it with a Vue 3 front-end. In the AuthController, I have implemented the following code snippet for user registration: ... On the front-end side, my setup looks like this ...

Unable to identify TypeScript version in Visual Studio Code, causing TS Intellisense to not function properly

Global Installation of TypeScript Below is what I see in my terminal when I run the command tsc --version. tsc --version // Version: 3.8.3 The TypeScript "version" is not showing up in the Status bar. When I try to select the TypeScript version fr ...

Is there a way to incorporate several select choices using specific HTML?

I am currently trying to dynamically populate a select tag with multiple option tags based on custom HTML content. While I understand how to insert dynamic content with ng-content, my challenge lies in separating the dynamic content and wrapping it in mat ...

The Relationship between Field and Parameter Types in TypeScript

I am currently working on a versatile component that allows for the creation of tables based on column configurations. Each row in the table is represented by a specific data model: export interface Record { attribute1: string, attribute2: { subAt ...

Tips for utilizing jest.mock following the removal of @types/jest (^jest@24)

After upgrading from version 7 to version 8 of @angular-builders/jest, I followed the instructions provided in the migration guide which advised removing @types/jest since it now comes bundled with Jest v24. Additionally, changes were made to my tsconfig a ...

What is the method to remove curly brackets from a different data category?

If I have a type like this type Z = {a: number} | {} | {b: boolean} | {c: string} | ...; Is there a way to get the same type but without {}? type Y = Exclude<Z, {}>; ⇧This will result in Y = never, because all variants can be assigned to {} and a ...

Navigating the process of transferring a function to a functional component within React

Hey there, I'm fairly new to using React and TypeScript and I am facing a small issue with moving a function from the file App.tsx to a functional component called WordAddingForm.tsx. Any help would be greatly appreciated! Let's start with my Ap ...

What is the reason TypeScript does not recognize the type when dealing with promises?

I am encountering an unexpected behavior where there is no error even though there should be one in TypeScript when using promises. I assigned a number value to a string variable, but surprisingly, no error was thrown. Why does this happen? https://codesa ...

Organizing Telephone Number Entries in Angular

In my search for a way to format a phone number input field in Angularjs, I have come across many solutions, but none specifically for Angular 7. What I am looking to achieve is for the user to enter the textfield like so: 123456789 and have the textfi ...

Error encountered in Storybook: The value is not iterable (property Symbol(Symbol.iterator) cannot be read)

I recently created a React library using and opted for the React + TypeScript + Storybook template. You can find the entire codebase here → https://github.com/deadcoder0904/react-typical I encountered the following error: undefined is not iterable ( ...

Testing React Hooks in TypeScript with Jest and @testing-library/react-hooks

I have a unique custom hook designed to manage the toggling of a product id using a boolean value and toggle function as returns. As I attempt to write a unit test for it following a non-typescripted example, I encountered type-mismatch errors that I' ...

Assign the mapState function to dynamically retrieve state objects from the Vuex store within the component

I am currently working on developing a watchlist using Vue3 and Vuex. The goal is for a watchlist component to subscribe to a symbol and receive updates from the store related to that specific symbol. However, if the subscription is removed from the compon ...

What is the process of converting a union type into a union of arrays in TypeScript?

I have a Foo type that consists of multiple types For example: type Foo = string | number I need to receive this type and convert it into an array of the individual types within the union type TransformedFoo = ToUnionOfArray<Foo> // => string[] ...