Example Analysis of fetch Network request Encapsulation
This article focuses on "fetch network request encapsulation example analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "fetch network request encapsulation example analysis"!
Export default ({url, method = 'GET', data = null,}) = > {/ / request configuration let options = {method} / / data is not empty It is that post requests if (data) {options = {... options, body: JSON.stringify (data), headers: {'content-type':' application/json'}} return fetch (url, options). Then (res = > res.json ()). Then (data = > data)} use get
Post import fetchApi from'. / js/fetch.js' const vm = new Vue ({el:'# app', data: {users: []}, / / initiate a network request mounted () {let url = 'http://localhost:3000/api/users' / / fetchApi ({url}). Then (data = > console.log (data)) fetchApi ({url, method:' POST', data: {id: 200) Name: 'aaa'}}) .then (data = > console.log (data))}}) so far I believe that you have a deeper understanding of the "fetch network request encapsulation example analysis", might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!