{"id":58689,"date":"2024-10-12T21:00:18","date_gmt":"2024-10-12T13:00:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58689\/"},"modified":"2024-10-12T21:00:18","modified_gmt":"2024-10-12T13:00:18","slug":"angular%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9b%b4%e6%96%b0%e6%95%99%e7%a8%8b%ef%bc%9a%e8%bc%95%e9%ac%86%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e6%9b%b4%e6%96%b0%e5%8a%9f%e8%83%bd-angular-%e6%9b%b4%e6%96%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58689\/","title":{"rendered":"Angular\u6578\u64da\u5eab\u66f4\u65b0\u6559\u7a0b\uff1a\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u66f4\u65b0\u529f\u80fd (angular \u66f4\u65b0\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"angular%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9b%b4%e6%96%b0%e6%95%99%e7%a8%8b%ef%bc%9a%e8%bc%95%e9%ac%86%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e6%9b%b4%e6%96%b0%e5%8a%9f%e8%83%bd-CoylkXHfEq\">Angular\u6578\u64da\u5eab\u66f4\u65b0\u6559\u7a0b\uff1a\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u66f4\u65b0\u529f\u80fd<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0cAngular\u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u524d\u7aef\u6846\u67b6\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u69cb\u5efa\u52d5\u614b\u548c\u97ff\u61c9\u5f0f\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u7576\u6d89\u53ca\u5230\u6578\u64da\u5eab\u7684\u66f4\u65b0\u529f\u80fd\u6642\uff0cAngular\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u5be6\u73fe\u9019\u4e00\u9700\u6c42\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728Angular\u4e2d\u5be6\u73fe\u6578\u64da\u5eab\u7684\u66f4\u65b0\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u5be6\u7528\u7684\u7bc4\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"1-%e7%90%86%e8%a7%a3angular%e7%9a%84%e6%95%b8%e6%93%9a%e7%b6%81%e5%ae%9a-CoylkXHfEq\">1. \u7406\u89e3Angular\u7684\u6578\u64da\u7d81\u5b9a<\/h2>\n<p>\u5728Angular\u4e2d\uff0c\u6578\u64da\u7d81\u5b9a\u662f\u5be6\u73fe\u6578\u64da\u66f4\u65b0\u7684\u6838\u5fc3\u6982\u5ff5\u3002\u6578\u64da\u7d81\u5b9a\u5141\u8a31\u958b\u767c\u8005\u5c07\u6a21\u578b\u6578\u64da\u8207\u8996\u5716\u9032\u884c\u540c\u6b65\uff0c\u9019\u6a23\u7576\u6a21\u578b\u6578\u64da\u767c\u751f\u8b8a\u5316\u6642\uff0c\u8996\u5716\u4e5f\u6703\u81ea\u52d5\u66f4\u65b0\u3002Angular\u652f\u6301\u5169\u7a2e\u4e3b\u8981\u7684\u6578\u64da\u7d81\u5b9a\u65b9\u5f0f\uff1a<\/p>\n<ul>\n<li><strong>\u55ae\u5411\u6578\u64da\u7d81\u5b9a\uff1a<\/strong>\u6578\u64da\u5f9e\u6a21\u578b\u6d41\u5411\u8996\u5716\u3002<\/li>\n<li><strong>\u96d9\u5411\u6578\u64da\u7d81\u5b9a\uff1a<\/strong>\u6578\u64da\u5728\u6a21\u578b\u548c\u8996\u5716\u4e4b\u9593\u96d9\u5411\u6d41\u52d5\u3002<\/li>\n<\/ul>\n<p>\u5728\u66f4\u65b0\u6578\u64da\u6642\uff0c\u96d9\u5411\u6578\u64da\u7d81\u5b9a\u7279\u5225\u6709\u7528\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u7c21\u5316\u7528\u6236\u8f38\u5165\u7684\u8655\u7406\u904e\u7a0b\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8angular%e6%9c%8d%e5%8b%99%e9%80%b2%e8%a1%8chttp%e8%ab%8b%e6%b1%82-CoylkXHfEq\">2. \u4f7f\u7528Angular\u670d\u52d9\u9032\u884cHTTP\u8acb\u6c42<\/h2>\n<p>\u8981\u66f4\u65b0\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\uff0c\u901a\u5e38\u9700\u8981\u901a\u904eHTTP\u8acb\u6c42\u8207\u5f8c\u7aefAPI\u9032\u884c\u4ea4\u4e92\u3002Angular\u63d0\u4f9b\u4e86HttpClient\u6a21\u7d44\u4f86\u7c21\u5316\u9019\u4e00\u904e\u7a0b\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528HttpClient\u9032\u884c\u6578\u64da\u66f4\u65b0\u7684\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"2-1-%e5%ae%89%e8%a3%9dhttpclient%e6%a8%a1%e7%b5%84-CoylkXHfEq\">2.1. \u5b89\u88ddHttpClient\u6a21\u7d44<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u5728Angular\u61c9\u7528\u4e2d\u5c0e\u5165HttpClient\u6a21\u7d44\u3002\u5728app.module.ts\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>import { HttpClientModule } from '@angular\/common\/http';\n\n@NgModule({\n  declarations: [ \/* \u4f60\u7684\u7d44\u4ef6 *\/ ],\n  imports: [\n    HttpClientModule,\n    \/* \u5176\u4ed6\u6a21\u7d44 *\/\n  ],\n  providers: [],\n  bootstrap: [ \/* \u4f60\u7684\u6839\u7d44\u4ef6 *\/ ]\n})\nexport class AppModule { }<\/code><\/pre>\n<h3 id=\"2-2-%e5%89%b5%e5%bb%ba%e6%9c%8d%e5%8b%99%e4%be%86%e8%99%95%e7%90%86http%e8%ab%8b%e6%b1%82-CoylkXHfEq\">2.2. \u5275\u5efa\u670d\u52d9\u4f86\u8655\u7406HTTP\u8acb\u6c42<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5275\u5efa\u4e00\u500b\u670d\u52d9\u4f86\u8655\u7406\u6578\u64da\u66f4\u65b0\u7684\u8acb\u6c42\uff1a<\/p>\n<pre><code>import { Injectable } from '@angular\/core';\nimport { HttpClient } from '@angular\/common\/http';\nimport { Observable } from 'rxjs';\n\n@Injectable({\n  providedIn: 'root'\n})\nexport class DataService {\n  private apiUrl = 'https:\/\/your-api-url.com\/data';\n\n  constructor(private http: HttpClient) { }\n\n  updateData(id: number, data: any): Observable {\n    return this.http.put(`${this.apiUrl}\/${id}`, data);\n  }\n}<\/code><\/pre>\n<h2 id=\"3-%e5%9c%a8%e7%b5%84%e4%bb%b6%e4%b8%ad%e4%bd%bf%e7%94%a8%e6%9c%8d%e5%8b%99-CoylkXHfEq\">3. \u5728\u7d44\u4ef6\u4e2d\u4f7f\u7528\u670d\u52d9<\/h2>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u7d44\u4ef6\u4e2d\u4f7f\u7528\u9019\u500b\u670d\u52d9\u4f86\u66f4\u65b0\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>import { Component } from '@angular\/core';\nimport { DataService } from '.\/data.service';\n\n@Component({\n  selector: 'app-update',\n  template: `\n    \n      \n      <button type=\"submit\">\u66f4\u65b0<\/button>\n    \n  `\n})\nexport class UpdateComponent {\n  data = { name: '' };\n\n  constructor(private dataService: DataService) { }\n\n  onSubmit() {\n    this.dataService.updateData(1, this.data).subscribe(response =&gt; {\n      console.log('\u6578\u64da\u66f4\u65b0\u6210\u529f', response);\n    }, error =&gt; {\n      console.error('\u66f4\u65b0\u5931\u6557', error);\n    });\n  }\n}<\/code><\/pre>\n<h2 id=\"4-%e9%8c%af%e8%aa%a4%e8%99%95%e7%90%86-CoylkXHfEq\">4. \u932f\u8aa4\u8655\u7406<\/h2>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u932f\u8aa4\u8655\u7406\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u53ef\u4ee5\u5728\u670d\u52d9\u4e2d\u6dfb\u52a0\u932f\u8aa4\u8655\u7406\u908f\u8f2f\uff0c\u4e26\u5728\u7d44\u4ef6\u4e2d\u6839\u64da\u9700\u8981\u986f\u793a\u932f\u8aa4\u4fe1\u606f\u3002<\/p>\n<h2 id=\"5-%e7%b8%bd%e7%b5%90-CoylkXHfEq\">5. \u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u5728Angular\u61c9\u7528\u4e2d\u5be6\u73fe\u6578\u64da\u5eab\u7684\u66f4\u65b0\u529f\u80fd\u3002\u5229\u7528Angular\u7684\u6578\u64da\u7d81\u5b9a\u548cHttpClient\u6a21\u7d44\uff0c\u6211\u5011\u80fd\u5920\u9ad8\u6548\u5730\u8207\u5f8c\u7aefAPI\u9032\u884c\u4ea4\u4e92\uff0c\u5be6\u73fe\u6578\u64da\u7684\u589e\u522a\u6539\u67e5\u64cd\u4f5c\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684Angular\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728Angular\u4e2d\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u5eab\u66f4\u65b0\u529f\u80fd\uff0c\u638c\u63e1\u6578\u64da\u66f4\u65b0\u7684\u6700\u4f73\u5be6\u8e10\u8207\u6280\u5de7\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-58689","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=58689"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58689\/revisions"}],"predecessor-version":[{"id":58690,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58689\/revisions\/58690"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}