1

I try to create a baseURL from .env.local, but in request I get http://localhost:8083/Contracts/users insted of https://api.EXAMPLE.dev/api/users.

I use npm run serve -> http://localhost:8083

Any suggestion, please?

.env.locale

VUE_APP_ENDPOINT="https://api.EXAMPLE.dev/api"

main.js

import axios from "axios";

axios.defaults.baseURL = process.env.VUE_APP_ENDPOINT;

Component.vue

  created: function() {
    axios
      .get("/users")
      .then(response => {
        this.items = response.data;
      })
      .catch(error => {
        console.log(error);
      });
  },
2
  • Is it possible that your process.env.VUE_APP_ENDPOINT is undefined for some reason when you define your axios.defaults.baseURL ? Have you tried to check that? Commented Nov 12, 2019 at 20:24
  • Yes, it didn't know how process.env.VUE_APP_ENDPOINT is, but I define this from .env.local Commented Nov 12, 2019 at 20:41

2 Answers 2

1

Try writing

axios.defaults.baseURL = process.env.VUE_APP_ENDPOINT || "https://api.EXAMPLE.dev/api"

And for Component.vue file

  created: function() {
    axios({
         url: "/users"
      })
      .then(response => {
        this.items = response.data;
      })
      .catch(error => {
        console.log(error);
      });
  }

Check if this works. May be that can be reason that process.env is not giving correct value.

Sign up to request clarification or add additional context in comments.

4 Comments

It works, but I want to insert the url from .env.local. It's like process.env is null
As i know .env.local should not contain url in quotes (" "). Try removing that and see if it works.
Is not from " ". I will do more research about it.
I restart the app. npm run serve, and it works, anyway, thanks for your time!
0

Was no problem with the code, after a restart it worked.

npm run serve

Comments

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.