zoukankan      html  css  js  c++  java
  • ng-http

    启用 Http 服务

    • open the root AppModule,
    • import the HttpClientModule symbol from @angular/common/http,
    • add it to the @NgModule.imports array.
    // app.module.ts:
     
    import {NgModule} from '@angular/core';
    import {BrowserModule} from '@angular/platform-browser';
     
    // Import HttpClientModule from @angular/common/http
    import {HttpClientModule} from '@angular/common/http';
     
    @NgModule({
      imports: [
        BrowserModule,
        // Include it under 'imports' in your application module
        // after BrowserModule.
        HttpClientModule,
      ],
    })
    export class MyAppModule {}
    

    发起一个 get 请求

    @Component(...)
    export class MyComponent implements OnInit {
     
      results: string[];
     
      // Inject HttpClient into your component or service.
      constructor(private http: HttpClient) {}
     
      ngOnInit(): void {
        // Make the HTTP request:
        this.http.get('/api/items').subscribe(data => {
          // Read the result field from the JSON response.
          this.results = data['results'];
        });
      }
    }
    

    Reading the full response

    this.http
      .get('https://jsonplaceholder.typicode.com/posts/1', {observe: 'response'})
      .subscribe(res => {
      console.log(res)
    })
    

    错误处理

    http
      .get('/api/items')
      .subscribe(
        // Successful responses call the first callback.
        data => {...},
        // Errors will call this callback instead:
        err => {
          console.log('Something went wrong!');	
        }
      );
    
  • 相关阅读:
    FineUI第十三天---`列布局
    FineUI第十二天---锚点布局
    FineUI第十一天---布局概述
    FineUI第十天---AJax的特性
    FineUI第九天---表单验证
    FineUI第八天----下拉列表控件
    FineUI第七天---文件上传
    FineUI第六天---表单控件
    FineUI第五天---按钮控件
    快速隐写术的一个小程序
  • 原文地址:https://www.cnblogs.com/ygjzs/p/12228104.html
Copyright © 2011-2022 走看看