nuxt.js use axios

Recently developed a micro-project use nuxt.js letter, from commissioning, publishing record encountered little trouble

According to the document: https://zh.nuxtjs.org/guide/installation

Use: $ npx create-nuxt-app nuxt-first

Creating nuxt-first project of course, will be asked whether the various components, including axios. I choose to use directly. Because the creation process (scaffolding) added, we will find that dependencies no axios, but you can find relevant content in modules of node nuxt.config.js

 

 

 

How to use it? You can refer to this document: https://axios.nuxtjs.org/usage

So I chose to use:

this.$axios.post("/trans",this.model).then( (res) => {...

Preliminary tests without problems. Now modify the port. I add server nodes nuxt.config.js

 

 

 So test: npm run dev, no problem, but the build, so run: npm run start, the question arises

 

 

 Under the helpless, try to use the proxy settings in nuxt.config.js axios

 axios: {
    prefix: '/api',
    credentials: true,
    proxy:true
  },
  proxy:
  {
      '/api':'http://localhost:4000'
  },

 

All the problem is solved. Why after release uses the default port? Agent Why were set at the localhost, access to domain names is no problem. After coming up to talk to everyone and so the level of interpretation.

One thing to note here, in the case of post code is not changed, or so:

this.$axios.post("/trans",this.model).then( (res) => {...

With the above configuration, the actual post address will be added / api, becomes http: // localhost: 4000 / api / trans, this is our actual service lies.

 

Guess you like

Origin www.cnblogs.com/kevin-Y/p/12509165.html