{"id":1015,"date":"2018-12-26T20:14:30","date_gmt":"2018-12-26T12:14:30","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1015"},"modified":"2022-02-17T15:36:14","modified_gmt":"2022-02-17T07:36:14","slug":"angular-material-first-tutorial","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2018\/12\/26\/angular-material-first-tutorial\/","title":{"rendered":"Angular Material \u521d\u9ad4\u9a57"},"content":{"rendered":"<h1>Angular Material<\/h1>\n<p>\u5728 <a href=\"\/blog\/2018\/11\/16\/2018-angular-taiwan-conference-note\/\" title=\"Angular Taiwan\u8ad6\u58c7\u7b46\u8a18\">Angular Taiwan\u8ad6\u58c7\u7b46\u8a18<\/a> \u4e2d\u6709\u63d0\u5230 Angualr Material \uff0c\u4f46\u662f\u7b46\u8005\u6c92\u6709\u591a\u52a0\u6df1\u5165\u8aaa\u660e\uff0c\u4eca\u5929\u4f86\u9032\u4e00\u6b65\u804a\u804a Angualr Material \uff0c\u85c9\u7531\u521d\u6b65\u7684\u64b0\u5beb\u7a0b\u5f0f\u78bc\u4f86\u5efa\u69cb\u4e00\u500b\u8a3b\u518a\u9801\u9762\u5143\u4ef6\u8b93\u8b80\u8005\u5011\u5c0d\u65bc Angular Material \u6709\u57fa\u672c\u7684\u8a8d\u8b58\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u521d\u6b65\u8a8d\u8b58<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\u7576\u7136\u662f\u8981\u4ecb\u7d39\u4e00\u4e0b\u751a\u9ebc\u662f <a href=\"https:\/\/material.angular.io\/\" title=\"Angular Material \u5b98\u7db2\" rel=\"nofollow noopener\" target=\"_blank\">Angular Material<\/a> \u4e86\uff0c Angular Material \u662f <a href=\"https:\/\/angular.io\/\" title=\"Angular \u5b98\u7db2\" rel=\"nofollow noopener\" target=\"_blank\">Angular<\/a> \u7684\u4e00\u500b\u9644\u5c6c\u5957\u4ef6\uff0c\u5176\u5167\u5bb9\u5305\u542b\u8a31\u591a UI \u4ecb\u9762\u4f8b\u5982\u57fa\u790e\u7684 Form\u3001Button\u3001Edit Box&#8230; \u7b49\u7b49\u7684\uff0c\u9084\u6709\u9032\u4e00\u6b65\u7684 Navigation\u3001Tab&#8230; \u4e4b\u985e\u7684\u539f\u4ef6\uff0c\u7c21\u55ae\u8b1b\u5c31\u662f UI \u9700\u8981\u7528\u5230\u5143\u4ef6\u61c9\u8a72\u56ca\u62ec\u5728\u5167\u4e86\uff0c\u6709\u4e86\u9019\u500b\u5957\u4ef6\u5728\u958b\u767c\u7db2\u9801\u6642\u7b49\u65bc\u6709\u4eba\u628a\u5f88\u591a\u6771\u897f\u90fd\u505a\u597d\u4e86\u7b49\u4f60\u4f86\u4f7f\u7528\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u7684\u901f\u5ea6\u5927\u5927\u7684\u63d0\u5347\u3002<\/p>\n<p>\u8b1b\u5b8c Material \u4e0d\u80fd\u4e0d\u63d0 The Component Dev Kit (CDK) \uff0c\u5b83\u5011\u5169\u500b\u7684\u95dc\u4fc2\u5c31\u50cf\u662f\u76ae\u8207\u9aa8\uff0c <code>CDK<\/code> \u662f\u9aa8\uff0c\u6240\u6709\u7684UI\u4e92\u52d5\u90fd\u662f\u7531 <code>CDK<\/code> \u5be6\u4f5c\uff0c\u800c <code>Material<\/code> \u5247\u662f\u5305\u8986\u9aa8\u7684\u76ae\uff0c <code>Material<\/code> \u4e2d\u5305\u542b\u4e00\u4e9b\u98a8\u683c\u63d0\u4f9b\u958b\u767c\u4eba\u54e1\u4f7f\u7528\uff0c\u6240\u4ee5\u904b\u7528\u4e0a\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>Material<\/code> \u642d\u914d\u5957\u4ef6\u6240\u9644\u7684\u4e3b\u984c\u4f7f\u7528\uff0c\u6216\u662f\u76f4\u63a5\u4f7f\u7528 <code>CDK<\/code> \u4f86\u5ba2\u88fd\u5316\u5c08\u5c6c\u5143\u4ef6\u3002<\/p>\n<p>\u4eca\u5929\u5148\u4f86\u8ac7\u8ac7 Material \u7684\u521d\u9ad4\u9a57\uff0c\u6240\u4ee5\u5148\u4e0d\u6df1\u5165\u8aaa\u660e\uff0c\u4ee5\u4e0b\u958b\u59cb\u9032\u5165\u4e3b\u984c\uff0c\u900f\u904e Angular Material \u4f86\u5efa\u69cb\u4e00\u500b\u8a3b\u518a\u9801\u9762\u5143\u4ef6\u3002<\/p>\n<h2>\u5b89\u88dd\u5fc5\u8981\u5957\u4ef6<\/h2>\n<p>\u9019\u90e8\u5206\u8981\u8acb\u5404\u4f4d\u5148\u5b89\u88dd Angular CLI \uff0c\u7576\u7136\u9996\u5148\u8981\u5148\u5b89\u88dd <a href=\"https:\/\/nodejs.org\/\" title=\"nodesjs \u5b98\u7db2\" rel=\"nofollow noopener\" target=\"_blank\">nodejs<\/a> \u4e86\uff0c\u5404\u4f4d\u53ef\u4ee5\u7531\u5b98\u7db2\u4e0b\u8f09\u5b89\u88dd\uff0c\u5f85\u5b89\u88dd\u5b8c nodejs \u5f8c\u900f\u904e\u57f7\u884c\u4e0b\u5217\u6307\u4ee4\u5b89\u88dd Angular CLI\uff1a<\/p>\n<pre><code class=\"language-bash\">$ npm install -g @angular\/cli<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c Angular CLI \u5f8c\u53ef\u4ee5\u4f7f\u7528 cli \u7522\u751f\u4e00\u500b\u65b0\u7684\u5c08\u6848\uff0c\u8acb\u8f38\u5165\u4ee5\u4e0b\u6307\u4ee4\uff1a<\/p>\n<pre><code class=\"language-bash\">$ ng new new-project<\/code><\/pre>\n<p>\u9019\u6642 cli \u6703\u8a62\u554f\u4e00\u4e9b\u95dc\u65bc\u65b0\u5c08\u6848\u7684\u5167\u5bb9\uff0c\u4f8b\u5982\u662f\u5426\u8981\u4f7f\u7528 router \u4e4b\u985e\u7684\uff0c\u9019\u90e8\u5206\u90fd\u4f7f\u7528\u9810\u8a2d\u503c\u5373\u53ef\uff0c\u76f8\u95dc\u5167\u5bb9\u5f85\u4e4b\u5f8c\u6709\u5c08\u9580\u4ecb\u7d39 angular \u7684\u7cfb\u5217\u6587\u7ae0\u518d\u4f86\u70ba\u5927\u5bb6\u8aaa\u660e\u3002\u5f85\u65b0\u5c08\u6848\u5efa\u7acb\u597d\u4e4b\u5f8c\u57f7\u884c\u4e0b\u5217\u6307\u4ee4\u4f86\u5b89\u88dd Angular Material\uff1a<\/p>\n<pre><code class=\"language-bash\">$ ng add @angular\/material<\/code><\/pre>\n<p>\u900f\u904e\u4ee5\u4e0a\u6b65\u9a5f\u53ef\u4ee5\u5b89\u88dd\u597d\u76f8\u95dc\u7684\u5957\u4ef6\uff0c\u63a5\u4e0b\u4f86\u8aaa\u660e\u4e00\u4e0b\u5982\u4f55\u8a2d\u5b9a\u5c08\u6848\u76f8\u95dc\u8a2d\u5b9a\u3002<\/p>\n<h2>\u8a2d\u5b9a\u5c08\u6848\u5167\u5bb9<\/h2>\n<p>\u9996\u5148\u4f9d\u64da\u700f\u89bd\u5668\u6240\u9700\u5167\u5bb9\u4f86 import \u76f8\u95dc\u5143\u4ef6\uff0c\u9996\u5148\u5728 module \u5167\u8a2d\u5b9a\u76f8\u95dc\u5167\u5bb9\uff0c\u5982\u4e0b\u5217\u6b65\u9a5f\u6240\u793a\uff1a<\/p>\n<h3>\u8a2d\u5b9a\u52d5\u756b\u6a21\u7d44<\/h3>\n<p>\u9996\u5148\u5148 import \u52d5\u756b\u6a21\u7d44\u9032\u4f86\uff1a<\/p>\n<pre><code class=\"language-typescript\">import {BrowserAnimationsModule} from \n    &#039;@angular\/platform-browser\/animations&#039;;\n\n@NgModule({\n  ...\n  imports: [BrowserAnimationsModule],\n  ...\n})<\/code><\/pre>\n<h3>\u8a2d\u5b9a\u76f8\u4f9d\u6a21\u7d44<\/h3>\n<p>\u9019\u908a\u5c31\u6240\u9700\u6a21\u7d44 import \u9032\u4f86\u5373\u53ef\uff0c\u6c92\u7528\u5230\u7684\u5c31\u4e0d\u9700\u8981 import \u5982\u6b64\u53ef\u4ee5\u5728 build production \u6642\u5c07 package \u6700\u5c0f\u5316\uff0c\u9019\u88e1\u6211\u5011\u7528\u5230\u7684\u5143\u4ef6\u6709 Button\u3001FormField\u3001Datepicker\u3001Icon\u3001Input \u5e7e\u9805\uff1a<\/p>\n<pre><code class=\"language-typescript\">import {\n  MatButtonModule,\n  MatFormFieldModule,\n  MatDatepickerModule,\n  MatNativeDateModule,\n  MatIconModule,\n  MatInputModule\n} from &#039;@angular\/material&#039;;\n\n@NgModule({\n  ...\n  imports: [\n    BrowserModule,\n    BrowserAnimationsModule,\n    MatButtonModule,\n    MatFormFieldModule,\n    MatDatepickerModule,\n    MatNativeDateModule,\n    MatIconModule,\n    MatInputModule\n  ],\n  ...\n})<\/code><\/pre>\n<h3>\u8a2d\u5b9a\u76f8\u95dccss<\/h3>\n<p>\u9019\u908a\u6211\u5011\u9078\u64c7 <code>indigo-pink<\/code> \u9019\u500b\u4e3b\u984c\uff0c\u9019\u90e8\u5206\u6709\u5e7e\u500b\u65b9\u5f0f\u53ef\u4ee5\u8a2d\u5b9a\u9019\u5167\u5bb9\uff0c\u9019\u908a\u9078\u64c7\u5728 angular.json \u9019\u500b\u8a2d\u5b9a\u9ee8\u8a2d\u5b9a\u9019\u4e3b\u984c\uff0c\u6240\u4ee5\u5148\u627e\u5230 styles \u7684\u5340\u6bb5\u52a0\u5165\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code class=\"language-json\">...\n&quot;styles&quot;: [\n   &quot;.\/node_modules\/@angular\/material\/prebuilt-themes\/indigo-pink.css&quot;,\n   &quot;src\/styles.css&quot;\n],\n...<\/code><\/pre>\n<p>\u6700\u5f8c\u7531\u65bc\u6211\u5011\u6709\u4f7f\u7528\u5230 Material Icon \u6240\u4ee5\u8981 import \u76f8\u95dc css \uff0c\u9019\u90e8\u5206\u5728 index.html \u5167\u52a0\u5165\u4ee5\u4e0b\u8a2d\u5b9a\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;link href=&quot;https:\/\/fonts.googleapis.com\/icon?family=Material+Icons&quot; rel=&quot;stylesheet&quot;&gt;<\/code><\/pre>\n<p>\u5230\u6b64\u5c31\u5b8c\u6210\u6211\u5011\u6240\u9700\u7684\u76f8\u95dc\u8a2d\u5b9a\uff0c\u63a5\u4e0b\u4f86\u8981\u544a\u8a34\u5927\u5bb6\u600e\u9ebc\u4f7f\u7528\u9019\u4e9b\u5143\u4ef6\u3002<\/p>\n<h2>\u4f7f\u7528\u76f8\u95dc\u5143\u4ef6<\/h2>\n<p>\u9019\u90e8\u5206\u6709\u500b\u8981\u9818\uff0c\u5c31\u662f\u9700\u8981\u7528\u7684\u751a\u9ebc\u5143\u4ef6\u518d\u5230 <a href=\"https:\/\/material.angular.io\/\" title=\"Angular Material \u5b98\u7db2\" rel=\"nofollow noopener\" target=\"_blank\">\u5b98\u65b9\u7db2\u7ad9<\/a> \u53c3\u95b1\u76f8\u95dcapi\u8aaa\u660e\u6587\u4ef6\u5373\u53ef\uff0c\u4eca\u5929\u5df2\u8a3b\u518a\u9801\u9762\u70ba\u4f8b\u5b50\u4f86\u5c55\u793a\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528\u3002<\/p>\n<h3>input<\/h3>\n<p>\u9019\u500b\u662f\u4f7f\u7528\u8005\u8f38\u5165\u7684\u57fa\u672c\u5143\u4ef6\uff0c\u4e3b\u8981\u5728html\u5167\u8f38\u5165\u4e0b\u5217\u8a9e\u6cd5\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;input matInput placeholder=&quot;Name&quot;&gt;<\/code><\/pre>\n<h3>form-field<\/h3>\n<p>\u5982\u679c\u8981\u8f38\u5165\u7684\u5167\u5bb9\u4e0d\u53ea\u4e00\u9805\uff0c\u53ef\u4ee5\u900f\u904e form-field \u628a\u76f8\u95dc input \u7d44\u5408\u8d77\u4f86\uff0c\u4f8b\u5982\u4e0b\u5217\u8a9e\u6cd5\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-html\">  &lt;mat-form-field class=&quot;example-full-width&quot;&gt;\n    &lt;input matInput placeholder=&quot;Name&quot;&gt;\n  &lt;\/mat-form-field&gt;<\/code><\/pre>\n<h3>icon<\/h3>\n<p>\u8981\u4f7f\u7528 icon \u7684\u6a23\u5f0f\u8f38\u5165\u4e0b\u5217\u8a9e\u6cd5\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;mat-icon matDatepickerToggleIcon&gt;\n    keyboard_arrow_down\n&lt;\/mat-icon&gt;<\/code><\/pre>\n<p>\u9019\u88e1\u7684\u91cd\u9ede\u5728\u65bc <code>keyboard_arrow_down<\/code> \u9019\u4ee3\u8868 icon \u7684\u6a23\u5f0f\uff0c matDatepickerToggleIcon \u4e0b\u5217 datepicker \u6703\u52a0\u4ee5\u8aaa\u660e\u3002<\/p>\n<h3>datepicker<\/h3>\n<p>\u5982\u679c\u8981\u8f38\u5165\u65e5\u671f\uff0c\u7528\u4e0b\u5217\u8a9e\u6cd5\u6e96\u6c92\u932f\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;mat-form-field class=&quot;example-full-width&quot;&gt;\n    &lt;input matInput \n        [matDatepicker]=&quot;picker&quot; \n        placeholder=&quot;Birthday&quot;&gt;\n    &lt;mat-datepicker-toggle \n        matSuffix [for]=&quot;picker&quot;&gt;\n        &lt;mat-icon matDatepickerToggleIcon&gt;\n            keyboard_arrow_down\n        &lt;\/mat-icon&gt;\n    &lt;\/mat-datepicker-toggle&gt;\n    &lt;mat-datepicker #picker&gt;\n    &lt;\/mat-datepicker&gt;\n&lt;\/mat-form-field&gt;<\/code><\/pre>\n<p>\u9019\u88e1\u7684\u95dc\u9375\u5728\u65bc <code>mat-datepicker-toggle<\/code> \u4ee5\u53ca <code>mat-datepicker<\/code> \uff0c\u7576\u9078\u64c7\u5b8c\u65e5\u671f\u5f8c\u6703\u628a\u65e5\u671f\u653e\u5230 <code>matInput<\/code> \u5167\uff0c\u800c <code>matDatepickerToggleIcon<\/code> \u7576\u9ede\u64ca\u6642\u6703\u89f8\u767c\u9078\u64c7\u65e5\u671f\u7684\u8996\u7a97\u51fa\u73fe\u3002<\/p>\n<h3>button<\/h3>\n<p>\u6700\u5f8c\u8aaa\u660e\u7684\u662f\u6309\u9215\u7684\u90e8\u5206\uff0c\u8f38\u5165\u4e0b\u5217\u8a9e\u6cd5\u5373\u53ef\u5448\u73fe\u6309\u9215\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;button mat-raised-button color=&quot;primary&quot;&gt;Signin&lt;\/button&gt;<\/code><\/pre>\n<p>\u5176\u4e2d<code><code>mat-raised-button<\/code><\/code>\u662f\u6309\u9215\u7684\u6a23\u5f0f\u3002<\/p>\n<p>\u900f\u904e\u4e0a\u8ff0\u8aaa\u660e\uff0c\u76f8\u4fe1\u8b80\u8005\u4e0d\u96e3\u5b8c\u6210\u4e00\u500b\u8a3b\u518a\u7684\u9801\u9762\u5143\u4ef6\u3002<\/p>\n<h1>\u7d50\u8a9e<\/h1>\n<p>\u4eca\u5929\u5148\u8ddf\u5927\u5bb6\u7c21\u55ae\u4ecb\u7d39\u4e00\u4e0b Angular Material \u7684\u76f8\u95dc\u5167\u5bb9\uff0c\u5e0c\u671b\u900f\u904e\u4e00\u500b\u7c21\u55ae\u7684\u8aaa\u660e\u8b93\u5927\u5bb6\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\uff0c\u63a5\u4e0b\u4f86\u8b80\u8005\u518d\u642d\u914d\u5b98\u65b9 api \u6587\u4ef6\u61c9\u8a72\u4e0d\u96e3\u5b8c\u6210\u4e00\u500b\u524d\u7aef\u7db2\u9801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Angular Material \u5728 Angular Taiwan\u8ad6\u58c7\u7b46\u8a18 \u4e2d\u6709\u63d0\u5230 Angualr Mate &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2018\/12\/26\/angular-material-first-tutorial\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Angular Material \u521d\u9ad4\u9a57\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[54,58],"class_list":["post-1015","post","type-post","status-publish","format-standard","hentry","category-develop","tag-angular","tag-angular-material"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1015"}],"version-history":[{"count":24,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1015\/revisions"}],"predecessor-version":[{"id":3026,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1015\/revisions\/3026"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}