0

I just started with VUE and I am stuck.

I have a simple VUE component and the script part of it is:

<script>
import Dish from './dish.vue'
import SearchBar from './search.vue'
import axios from 'axios'

export default {
    components: {Dish, SearchBar},
    name: "selectDish",
    data() {
        return {
            dishes : []
        }
    },
    mounted(){     
    axios.get('http://example.com/',{method :'GET'})
    .then(response =>{
        console.log(response);
        this.dishes=response.data
        })
    }
}

What I am trying to achieve is to remove the axios API GET call that I have inside the VUE compoennt as above and instead call the function that I have written inside my APIService.TS file which ultimately makes the GET call.

My apiService.ts file is as Below:

import Promise, { config, resolve, reject } from 'bluebird';
import Config from '../config';
import axios from 'axios';

export class APIService{
    constructor(){  }
    public giveMeAllDishes(dishType, query) : Promise<string>{

          return new Promise((resolve,reject) =>{
               axios.get('http://example.com',{method : 'GET'})
               .then(function(respone){
                   console.log(respone);
                   resolve(respone.data);
               })
               .catch(function(err){
                   reject(err);
               })
           })
    }
}

When I put the import APIService from '../../services/apiService' inside the VUE component, I get an error saying :

ERROR in ./~/babel-loader/lib!./~/vue-loader/lib/selector.js?type=script&index=0!./src/components/selectDish/selectDish.vue
Module not found: Error: Can't resolve '../../services/apiService'

Also, when I solve this referencing issue, How can I call the 'giveMeAllDishes' function of API Service from VUE Component ?

2
  • 1
    The question doesn't contain stackoverflow.com/help/mcve . The module either doesn't exist at the path you listed (../../services/apiService) or the project isn't configured for TS. Any way, the problem only can be investigated on your side. Commented Sep 27, 2019 at 14:02
  • You do not need to wrap the axios.get() call in a new Promise. This is the classic promise construction anti-pattern Commented Sep 30, 2019 at 4:01

1 Answer 1

1

Your import is slightly wrong. Here is what it needs to be:

import { APIService } from '../../services/apiService'
Sign up to request clarification or add additional context in comments.

1 Comment

OP will need to do this but first they have to fix the "Module not found" error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.