{"id":114,"date":"2017-11-11T21:04:12","date_gmt":"2017-11-11T13:04:12","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=114"},"modified":"2020-06-20T23:01:48","modified_gmt":"2020-06-20T15:01:48","slug":"%e4%bd%bf%e7%94%a8angular%e5%bc%80%e5%8f%91api%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2017\/11\/11\/%e4%bd%bf%e7%94%a8angular%e5%bc%80%e5%8f%91api%e6%96%87%e6%a1%a3\/","title":{"rendered":"\u4f7f\u7528angular\u5f00\u53d1API\u6587\u6863"},"content":{"rendered":"<hr \/>\n<p>\u3000\u3000\u3000\u3000\u3000<img src=\"https:\/\/cli.angular.io\/images\/cli-logo.svg\" alt=\"\u4f7f\u7528angular\u5f00\u53d1API\u6587\u6863\" \/><\/p>\n<hr \/>\n<h2>\u542f\u52a8\u9879\u76ee<\/h2>\n<p>\u5b89\u88c5Angular\u3002<\/p>\n<p>\u4f7f\u7528<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/cli.angular.io\/\">Angular CLI<\/a>\uff08\u547d\u4ee4\u884c\u754c\u9762\uff09\u5de5\u5177\u6765\u542f\u52a8\u4e00\u4e2a\u65b0\u7684Angular\u9879\u76ee\u3002<\/p>\n<p>\u5148\u5b89\u88c5\uff0c\u7136\u540e\u4f7f\u7528\u5b83\u6765\u542f\u52a8\u65b0\u9879\u76ee\u5e76\u8df3\u5230\u8be5\u6587\u4ef6\u5939\u4e2d\uff1a<\/p>\n<pre><code class=\"line-numbers\">$ npm install -g @angular\/cli\n$ ng new ang-demo\n$ cd ang-demo\n<\/code><\/pre>\n<h2>\u5f00\u53d1\u9879\u76ee<\/h2>\n<p>\u6b64\u65f6\uff0c\u6587\u4ef6\u5939\u53ea\u80fd\u8fd0\u884c\u4e00\u4e2aAngular\u9879\u76ee\u3002\u6211\u4eec\u6709\u4e00\u4e9b\u8c03\u6574\u6765\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528Angular\u3002<\/p>\n<p>\u9996\u5148\uff0c\u4e0e\u5176\u4ed6\u7684<br \/>\n\u5e94\u7528\u7a0b\u5e8f\u4e00\u6837\uff0c\u5fc5\u987b\u521b\u5efa\u4e00\u4e2a<code>main.js<\/code> \u6587\u4ef6\uff0c\u5b83\u53ef\u4ee5\u8ba9\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u5e76\u8fd0\u884c\u4e00\u4e9b\u9ed8\u8ba4\u8bbe\u7f6e\u3002<\/p>\n<p>\u5728\u9879\u76ee\u7684\u6839\u6587\u4ef6\u5939\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a<code>main.js<\/code>\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">const {app, BrowserWindow} = require('electron')\nconst path = require('path')\nconst url = require('url')\n\nlet win\n\nfunction createWindow () {\n  win = new BrowserWindow({width: 800, height: 600})\n\n  win.loadURL(url.format({\n    pathname: path.join(__dirname, 'dist\/index.html'),\n    protocol: 'file:',\n    slashes: true\n  }))\n\n  \/\/ win.webContents.openDevTools()\n\n  win.on('closed', () =&gt; {\n    win = null\n  })\n}\n\napp.on('ready', createWindow)\n\n\napp.on('window-all-closed', () =&gt; {\n  if (process.platform !== 'darwin') {\n    app.quit()\n  }\n})\n\napp.on('activate', () =&gt; {\n  if (win === null) {\n    createWindow()\n  }\n})\n<\/code><\/pre>\n<p>\u4fdd\u5b58\u6587\u4ef6\uff0c\u7136\u540e\u7f16\u8f91<code>\/src\/index.html<\/code>\u6587\u4ef6\u5e76\u66f4\u6539\uff1a<\/p>\n<pre data-language=HTML><code class=\"language-markup line-numbers\">&lt;!-- CHANGE FROM: --&gt;\n  &lt;base href=\"\/\"&gt;\n\n&lt;!-- TO: --&gt;\n  &lt;base href=\".\/\"&gt;\n<\/code><\/pre>\n<p>\u4ee5\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u80fd\u591f\u627e\u5230Angular\u6587\u4ef6\u3002<\/p>\n<p>\u7f16\u8f91\u8bbf\u95ee<code>\/package.json<\/code>\u6587\u4ef6\u8fdb\u884c\u4e00\u4e9b\u5c0f\u8c03\u6574\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-json line-numbers\">{\n  \"main\": \"main.js\",\n  \"scripts\": {\n    \"electron\": \"ng build &amp;&amp; electron .\",\n    \"electron-aot\": \"ng build --prod &amp;&amp; electron .\",\n  },\n}\n<\/code><\/pre>\n<p>\u5148\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a<code>main<\/code>\u7684\u65b0\u5c5e\u6027\uff0c\u6307\u5b9a\u542f\u52a8\u6587\u4ef6\u4e3a<code>main.js<\/code>\u6587\u4ef6\u3002<\/p>\n<p>\u6dfb\u52a0\u4e00\u4e2a\u65b0<code>npm run<\/code>\u547d\u4ee4\uff0c\u5b83\u9996\u5148\u4f7f\u7528Angular CLI\u4e3a\u6211\u4eec\u7684Angular\u5e94\u7528\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a<code>build<\/code>\uff0c\u7136\u540e\u8c03\u7528\u7535\u5b50\u6765\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><code>electron-aot<\/code>\u547d\u4ee4 \u4e3aAngular \u6dfb\u52a0\u4e86<code>--prod<\/code>\u6807\u5fd7\uff0c\u5b83\u4f1a\u751f\u6210\u4e86App\u7684\u751f\u4ea7\u7248\u672c\u3002\u8fd9\u5f15\u5165\u4e86\u8bb8\u591a\u589e\u5f3a\u529f\u80fd\uff0c\u4f46\u6784\u5efa\u8be5\u9879\u76ee\u9700\u8981\u66f4\u957f\u7684\u65f6\u95f4\u3002<\/p>\n<p><em>\u6ce8\u610f\uff1a<\/em><br \/>\n<em>\u5f53\u4f60\u51c6\u5907\u4e3a\u5176\u4ed6\u4eba\u4f7f\u7528\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u6784\u5efa\u65f6\uff0c\u8bf7\u786e\u4fdd\u4f7f\u7528electron-aot\u3002<\/em><\/p>\n<h2>\u542f\u52a8\u9879\u76ee<\/h2>\n<p>\u8bbf\u95ee\u63a7\u5236\u53f0\u5e76\u8f93\u5165\uff1a<\/p>\n<pre><code class=\"line-numbers\">npm run electron\n<\/code><\/pre>\n<p>\u5982\u679c\u4e00\u5207\u987a\u5229\uff0c\u65b0\u7684Angular\u5e94\u7528\u7a0b\u5e8f\u5c31\u4f1a\u52a0\u8f7d\u6210\u529f\u3002<\/p>\n<p><img src=\"http:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/Javascript\u5f00\u53d1\u73af\u5883\u642d\u5efa\u6d41\u7a0b-angular1.png\" alt=\"\u4f7f\u7528angular\u5f00\u53d1API\u6587\u6863\" \/><\/p>\n<h2>API<\/h2>\n<p>\u901a\u8fc7\u4e00\u4e2a\u540d\u4e3a<code>ngx-electron<\/code>\u7684\u8f6f\u4ef6\u5305\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u8f7b\u677e\u5730\u83b7\u5f97API \u3002<\/p>\n<p>\u4ece\u63a7\u5236\u53f0\u5b89\u88c5\uff1a<\/p>\n<pre><code class=\"line-numbers\">$ npm install ngx-electron --save\n<\/code><\/pre>\n<p>\u6dfb\u52a0\u5230<code>\/src\/app\/app.module.ts<\/code>\u4e2d\u7684<code>imports<\/code>\u6570\u7ec4\u4e2d\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">import { NgxElectronModule } from 'ngx-electron';\n\n@NgModule({\n  ...\n  imports: [\n    BrowserModule,\n    NgxElectronModule\n  ],\n  ...\n})\nexport class AppModule { }\n<\/code><\/pre>\n<p>\u6253\u5f00<code>\/src\/app\/app.component.ts<\/code>\u5e76\u5c06\u4ee5\u4e0b\u5185\u5bb9\u6dfb\u52a0\u5230\u5bfc\u5165\u4e2d\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">import { Component } from '@angular\/core';\nimport { ElectronService } from 'ngx-electron';\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u5728\u7ec4\u4ef6\u7c7b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\u6765\u521b\u5efa\u5b83\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u8fd9\u5c06\u4f7f\u6211\u4eec\u80fd\u591f\u8bbf\u95ee\u5404\u79cd\u4e0eAPI\u4ea4\u4e92\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">export class AppComponent {\n\n  constructor(private _electronService: ElectronService) {}   \/\/ DI\n\n  launchWindow() {\n    this._electronService.shell.openExternal('http:\/\/indeex.cc');\n  }\n\n}\n<\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>launchWindow\uff08\uff09<\/code>\u7684\u65b9\u6cd5\u3002\u70b9\u51fb\u6309\u94ae\u540e\uff0c\u5c06\u542f\u52a8\u4e00\u4e2a\u5e26\u6709Coursetro\u7684\u6d4f\u89c8\u5668\u7a97\u53e3\u7f51\u7ad9\u3002\u8fd9\u53ea\u662f\u4e3a\u4e86\u8bc1\u660e<code>ngx-electron<\/code>\u7684\u5df2\u7ecf\u53ef\u7528\u3002<\/p>\n<p>\u5b83\u4e3a\u60a8\u63d0\u4f9b\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n<blockquote>\n<ul>\n<li>desktopCapturer: \u684c\u9762\u6355\u6349API<\/li>\n<li>ipcRenderer: Electron.IpcRenderer<\/li>\n<li>remote: Electron.Remote &#8211; \u901a\u4fe1\u529f\u80fd<\/li>\n<li>webFrame: Electron.WebFrame &#8211; WebFrame<\/li>\n<li>clipboard: Electron.Clipboard &#8211; \u526a\u8d34\u677fAPI<\/li>\n<li>crashReporter: Electron.CrashReporter<\/li>\n<li>process: NodeJS.Process &#8211; \u8fdb\u7a0b\u5bf9\u8c61<\/li>\n<li>screen: Electron.Screen &#8211; \u5c4f\u5e55API<\/li>\n<li>shell: Electron.Shell &#8211; Shell API<\/li>\n<li>nativeImage: Electron.NativeImage &#8211; NativeImage API<\/li>\n<li>isElectronApp: boolean &#8211; \u8868\u793a\u5e94\u7528\u662f\u5426\u5728\u5185\u90e8\u6267\u884c<\/li>\n<\/ul>\n<\/blockquote>\n<p>\u6253\u5f00<code>\/src\/app\/app.component.html<\/code>  \u5e76\u5c06\u6587\u4ef6HTML\u66ff\u6362\u4e3a\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre data-language=HTML><code class=\"language-markup line-numbers\">&lt;div style=\"text-align:center\"&gt;\n  &lt;h1&gt;\n    Welcome!\n  &lt;\/h1&gt;\n\n  &lt;button (click)=\"launchWindow()\"&gt;Launch a Window&lt;\/button&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<h2>\u8fd0\u884c\u9879\u76ee<\/h2>\n<p>\u5728\u63a7\u5236\u53f0\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code class=\"line-numbers\">npm run electron\n<\/code><\/pre>\n<p>\u70b9\u51fb\u542f\u52a8\u7a97\u53e3\u6309\u94ae\uff0c\u5b83\u4f1a\u52a0\u8f7d\u7f51\u9875\uff0c\u540c\u65f6\uff0c\u8fd8\u53ef\u4ee5\u8bbf\u95eeAPI\u3002<\/p>\n<p>\u8fd9\u8ba9\u65b0\u624b\u4e5f\u80fd\u8f7b\u677e\u4e0a\u624b\u3002<\/p>\n<p>code enjoy!   \u0669(\u0e51\u275b\u1d17\u275b\u0e51)\u06f6<\/p>\n<p>\u4f5c\u8005\uff1aindeex<\/p>\n<p>\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/indeex.cc\/\">http:\/\/indeex.cc<\/a><\/p>\n<p>\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u3000\u3000\u3000\u3000 \u542f\u52a8\u9879\u76ee \u5b89\u88c5Angular\u3002 \u4f7f\u7528Angular CLI\uff08\u547d\u4ee4\u884c\u754c\u9762\uff09\u5de5\u5177\u6765\u542f\u52a8\u4e00\u4e2a\u65b0<a href=\"https:\/\/blog.indeex.club\/index.php\/2017\/11\/11\/%e4%bd%bf%e7%94%a8angular%e5%bc%80%e5%8f%91api%e6%96%87%e6%a1%a3\/\" class=\"read-more\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[4],"_links":{"self":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/114"}],"collection":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/comments?post=114"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/114\/revisions"}],"predecessor-version":[{"id":121,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/114\/revisions\/121"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}