<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      zwvista

      導航

      使用 fetch + React.js 調用 REST API

      JSON : Placeholder

      JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一個用于測試的 REST API 網站。
      以下使用 RxJS6 + React.js 調用該網站的 REST API,獲取字符串以及 JSON 數據。

      • GET /posts/1
      • GET /posts
      • POST /posts
      • PUT /posts/1
      • DELETE /posts/1

      所有 GET API 都返回JSON數據,格式(JSON-Schema)如下:

      {
        "type":"object",
        "properties": {
          "userId": {"type" : "integer"},
          "id": {"type" : "integer"},
          "title": {"type" : "string"},
          "body": {"type" : "string"}
        }
      }
      

      創建工程

      # 安裝 CLI
      $ npm install -g cra-template-typescript
      # 創建新的應用程序 FetchExample
      $ npx create-react-app fetch-example --template typescript
      $ cd fetch-example
      $ npm start
      

      打開 Intellij IDEA, File / Open...,然后選中工程所在文件夾

      點擊 Add Configurations, 點擊 +npm
      Name: React CLI Server
      Scripts: start
      點擊 OK 完成配置。
      點擊 React CLI Server 啟動程序。
      http://localhost:3000/ 可打開網頁。

      Post

      在 src 文件夾下添加 post.ts,內容如下

      export class Post {
        userId!: number;
        id!: number;
        title!: string;
        body!: string;
        toString(): string {
          return `Post {userId = ${this.userId}, id = ${this.id}, title = "${this.title}", body = "${this.body.replace(/\n/g, '\\n')}"}`;
        }
      }
      

      post 服務

      在 src 文件夾下添加 post.service.ts,內容如下

      import { Post } from './post';
      
      export class PostService {
        private readonly baseUrl = 'https://jsonplaceholder.typicode.com/';
      
        constructor() {
          this.getPostAsString();
          this.getPostAsJson();
          this.getPosts(2);
          this.createPost();
          this.updatePost();
          this.deletePost();
        }
      
        private async getPostAsString() {
          const url = `${this.baseUrl}posts/1`;
          const result = await fetch(url);
          const data = await result.text();
          console.log(data);
        }
      
        private async getPostAsJson() {
          const url = `${this.baseUrl}posts/1`;
          const result = await fetch(url);
          const data = await result.json();
          const post = Object.assign(new Post(), data);
          console.log(post);
        }
      
        private async getPosts(n: number) {
          const url = `${this.baseUrl}posts`;
          const result = await fetch(url);
          const data = await result.json();
          (data as Post[]).slice(0, n).map(v => {
            const post = Object.assign(new Post(), v);
            console.log(post);
            return post;
          });
        }
      
        private async createPost() {
          const url = `${this.baseUrl}posts`;
          const result = await fetch(url, {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({
              userId: 101,
              title: 'test title',
              body: 'test body',
            })
          });
          const data = await result.json();
          const post = Object.assign(new Post(), data);
          console.log(post);
        }
      
        private async updatePost() {
          const url = `${this.baseUrl}posts/1`;
          const result = await fetch(url, {
            method: 'PUT',
            headers: {
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({
              userId: 101,
              title: 'test title',
              body: 'test body',
            })
          });
          const data = await result.json();
          const post = Object.assign(new Post(), data);
          console.log(post);
        }
      
        private async deletePost() {
          const url = `${this.baseUrl}posts/1`;
          const result = await fetch(url, {
            method: 'DELETE'
          });
          const data = await result.text();
          console.log(data);
        }
      }
      
      • getPostAsString 方法取出第1個Post,返回字符串
      • getPostAsJson 方法取出第1個Post,返回Post對象
      • getPosts 方法取出前n個Post,返回n個Post對象
      • createPost 方法創建1個Post,返回字符串
      • updatePost 方法更新第1個Post,返回字符串
      • deletePost 方法刪除第1個Post,返回字符串

      打開 App.tsx,將其改為

      import * as React from 'react';
      import './App.css';
      
      import logo from './logo.svg';
      import { PostService } from './post.service';
      
      class App extends React.Component {
        postService!: PostService;
      
        componentDidMount() {
          this.postService = new PostService();
        }
      
        public render() {
          return (
            <div className="App">
              <header className="App-header">
                <img src={logo} className="App-logo" alt="logo" />
                <h1 className="App-title">Welcome to React</h1>
              </header>
              <p className="App-intro">
                To get started, edit <code>src/App.tsx</code> and save to reload.
              </p>
            </div>
          );
        }
      }
       
      export default App;
      

      輸出結果

      {
        "userId": 1,
        "id": 1,
        "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
        "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
      }
      Post {userId = 1, id = 1, title = "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body = "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"}
      Post {userId = 1, id = 1, title = "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body = "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"}
      Post {userId = 1, id = 2, title = "qui est esse", body = "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"}
      {"params":{"userId":101,"title":"test title","body":"test body"},"id":101}
      {"params":{"userId":101,"title":"test title","body":"test body"},"id":1}
      {}
      

      posted on 2022-09-04 17:22  zwvista  閱讀(112)  評論(0)    收藏  舉報

      主站蜘蛛池模板: 人妻av无码系列一区二区三区| 亚洲男人成人性天堂网站| 蜜桃精品成人影片| 国产v亚洲v天堂a无码| 精品人妻av区乱码| 性色av无码不卡中文字幕| 男人的天堂av一二三区| 日本免费一区二区三区久久| 亚洲欧美综合一区二区三区| 97超级碰碰碰久久久久| 免费午夜无码片在线观看影院| 亚洲一区二区三区丝袜| 日韩人妻少妇一区二区三区| 中文字幕人妻精品在线| 成人午夜污一区二区三区| 中文无码妇乱子伦视频| 亚洲精品日本一区二区| 精品无码国产自产拍在线观看蜜| 国产精品v欧美精品∨日韩| 国产深夜福利在线免费观看 | 国产日韩av二区三区| 亚洲成在人线在线播放无码| 亚洲精品无码久久毛片| 在线观看AV永久免费| caoporn成人免费公开| 久久精品国产99久久久古代| 免费现黄频在线观看国产| 青草热在线观看精品视频| 好男人视频在线播放| 亚洲女同精品久久女同| 不卡视频在线一区二区三区| 在线观看国产午夜福利片| 长腿校花无力呻吟娇喘| 成人午夜在线观看刺激| 安福县| 亚洲国产成人精品av区按摩| 国产精品高清一区二区不卡| 国产午夜精品福利91| 成人精品区| 久久不卡精品| 高清无码18|