{"id":24437,"date":"2024-11-21T14:10:14","date_gmt":"2024-11-21T06:10:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24437\/"},"modified":"2024-11-21T14:10:14","modified_gmt":"2024-11-21T06:10:14","slug":"%e6%8e%a2%e7%b4%a2webman%ef%bc%9a%e4%b8%80%e4%b8%aa%e5%bc%ba%e5%a4%a7%e7%9a%84%e5%89%8d%e7%ab%af%e5%bc%80%e5%8f%91%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24437\/","title":{"rendered":"\u63a2\u7d22Webman\uff1a\u4e00\u4e2a\u5f3a\u5927\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169292908749862.jpg\" class=\"aligncenter\" title=\"\u63a2\u7d22Webman\uff1a\u4e00\u4e2a\u5f3a\u5927\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6\u63d2\u56fe\" alt=\"\u63a2\u7d22Webman\uff1a\u4e00\u4e2a\u5f3a\u5927\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6\u63d2\u56fe\" \/><\/p>\n<p>\u6807\u9898\uff1a\u63a2\u7d22Webman\uff1a\u4e00\u4e2a\u5f3a\u5927\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u968f\u7740\u4e92\u8054\u7f51\u7684\u8fc5\u901f\u53d1\u5c55\uff0c\u524d\u7aef\u5f00\u53d1\u6210\u4e3a\u8d8a\u6765\u8d8a\u70ed\u95e8\u7684\u804c\u4e1a\u9886\u57df\u3002\u4e3a\u4e86\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\uff0c\u524d\u7aef\u5f00\u53d1\u6846\u67b6\u5e94\u8fd0\u800c\u751f\u3002\u800cWebman\u4f5c\u4e3a\u4e00\u4e2a\u5f3a\u5927\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6\uff0c\u5728\u6700\u8fd1\u51e0\u5e74\u91cc\u9010\u6e10\u53d7\u5230\u4e86\u5f00\u53d1\u8005\u7684\u5173\u6ce8\u548c\u4f7f\u7528\u3002\u672c\u6587\u5c06\u63a2\u7d22Webman\u6846\u67b6\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u6f14\u793a\u5176\u5728\u524d\u7aef\u5f00\u53d1\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662fWebman\u6846\u67b6\uff1f<br \/>Webman\u662f\u4e00\u4e2a\u57fa\u4e8eJavaScript\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6\uff0c\u96c6\u6210\u4e86\u591a\u79cd\u529f\u80fd\uff0c\u5305\u62ec\u8def\u7531\u7ba1\u7406\u3001\u6a21\u5757\u5316\u5f00\u53d1\u3001\u7ec4\u4ef6\u5316\u5f00\u53d1\u7b49\u3002\u5b83\u901a\u8fc7\u63d0\u4f9b\u4e00\u5957\u9ad8\u6548\u800c\u6613\u7528\u7684\u5de5\u5177\uff0c\u5927\u5927\u51cf\u5c11\u4e86\u524d\u7aef\u5f00\u53d1\u7684\u590d\u6742\u6027\u548c\u91cd\u590d\u6027\u5de5\u4f5c\uff0c\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p>\u4e8c\u3001\u8def\u7531\u7ba1\u7406<br \/>Webman\u6846\u67b6\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8def\u7531\u7ba1\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u6784\u5efa\u5355\u9875\u5e94\u7528\u548c\u591a\u9875\u5e94\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import Webman from 'webman'\n\nconst router = new Webman.Router()\n\nrouter.route('\/home', () =&amp;gt; {\n  \/\/ \u5904\u7406\u9996\u9875\u903b\u8f91\n})\n\nrouter.route('\/about', () =&amp;gt; {\n  \/\/ \u5904\u7406\u5173\u4e8e\u9875\u9762\u903b\u8f91\n})\n\nrouter.start()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u5b9a\u4e49\u8def\u7531\u89c4\u5219\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u8def\u7531\u89c4\u5219\u7ed1\u5b9a\u76f8\u5e94\u7684\u5904\u7406\u51fd\u6570\u3002\u5f53\u7528\u6237\u8bbf\u95ee\u4e0d\u540c\u7684URL\u65f6\uff0c\u6846\u67b6\u4f1a\u81ea\u52a8\u8c03\u7528\u5bf9\u5e94\u7684\u5904\u7406\u51fd\u6570\uff0c\u5b9e\u73b0\u9875\u9762\u7684\u5207\u6362\u548c\u903b\u8f91\u5904\u7406\u3002<\/p>\n<p>\u4e09\u3001\u6a21\u5757\u5316\u5f00\u53d1<br \/>Webman\u6846\u67b6\u652f\u6301\u6a21\u5757\u5316\u5f00\u53d1\uff0c\u53ef\u4ee5\u5c06\u524d\u7aef\u4ee3\u7801\u5206\u5272\u4e3a\u591a\u4e2a\u6a21\u5757\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u590d\u7528\u6027\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>\/\/ moduleA.js\nexport function foo() {\n  \/\/ \u6a21\u5757A\u7684\u529f\u80fd\u5b9e\u73b0\n}\n\n\/\/ moduleB.js\nexport function bar() {\n  \/\/ \u6a21\u5757B\u7684\u529f\u80fd\u5b9e\u73b0\n}\n\n\/\/ main.js\nimport { foo } from '.\/moduleA'\nimport { bar } from '.\/moduleB'\n\n\/\/ \u4f7f\u7528\u6a21\u5757A\u548c\u6a21\u5757B\u7684\u529f\u80fd\nfoo()\nbar()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u5c06\u4e0d\u540c\u7684\u529f\u80fd\u5b9e\u73b0\u653e\u5728\u4e0d\u540c\u7684\u6a21\u5757\u4e2d\uff0c\u901a\u8fc7\u5bfc\u5165\u548c\u4f7f\u7528\u6a21\u5757\u7684\u65b9\u5f0f\u6765\u8c03\u7528\u5bf9\u5e94\u7684\u529f\u80fd\u3002\u8fd9\u79cd\u6a21\u5757\u5316\u5f00\u53d1\u7684\u65b9\u5f0f\u4f7f\u4ee3\u7801\u7ed3\u6784\u6e05\u6670\uff0c\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/p>\n<p>\u56db\u3001\u7ec4\u4ef6\u5316\u5f00\u53d1<br \/>Webman\u6846\u67b6\u652f\u6301\u7ec4\u4ef6\u5316\u5f00\u53d1\uff0c\u53ef\u4ee5\u5c06\u9875\u9762\u5212\u5206\u4e3a\u591a\u4e2a\u53ef\u590d\u7528\u7684\u7ec4\u4ef6\uff0c\u5e76\u901a\u8fc7\u7ec4\u4ef6\u95f4\u7684\u6570\u636e\u4f20\u9012\u548c\u4e8b\u4ef6\u76d1\u542c\u6765\u5b9e\u73b0\u590d\u6742\u7684\u4ea4\u4e92\u6548\u679c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>class Button extends Webman.Component {\n  constructor() {\n    super()\n    this.state = {\n      count: 0\n    }\n  }\n\n  handleClick() {\n    this.setState({\n      count: this.state.count + 1\n    })\n  }\n\n  render() {\n    return `\n      &lt;button onclick=\"${this.handleClick.bind(this)}\"&gt;\u70b9\u51fb\u6b21\u6570\uff1a${this.state.count}&lt;\/button&gt;\n    `\n  }\n}\n\nconst button = new Button()\nbutton.mount(document.getElementById('app'))<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aButton\u7684\u7ec4\u4ef6\uff0c\u901a\u8fc7\u7ee7\u627fWebman.Component\u6765\u83b7\u5f97\u7ec4\u4ef6\u7684\u57fa\u672c\u529f\u80fd\u3002\u7ec4\u4ef6\u5185\u90e8\u901a\u8fc7state\u5c5e\u6027\u6765\u5b58\u50a8\u6570\u636e\uff0c\u5e76\u901a\u8fc7setState\u65b9\u6cd5\u6765\u66f4\u65b0\u6570\u636e\u3002\u7ec4\u4ef6\u7684render\u65b9\u6cd5\u7528\u4e8e\u8fd4\u56de\u7ec4\u4ef6\u7684HTML\u4ee3\u7801\u3002\u901a\u8fc7\u8c03\u7528mount\u65b9\u6cd5\uff0c\u5c06\u7ec4\u4ef6\u6302\u8f7d\u5230HTML\u9875\u9762\u7684\u7279\u5b9a\u5143\u7d20\u4e0a\u3002<\/p>\n<p>\u7ed3\u8bed\uff1a<br \/>Webman\u6846\u67b6\u4f5c\u4e3a\u4e00\u4e2a\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u4fbf\u6377\u7684\u64cd\u4f5c\u65b9\u5f0f\u3002\u672c\u6587\u4ecb\u7ecd\u4e86Webman\u6846\u67b6\u7684\u8def\u7531\u7ba1\u7406\u3001\u6a21\u5757\u5316\u5f00\u53d1\u548c\u7ec4\u4ef6\u5316\u5f00\u53d1\u7b49\u529f\u80fd\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u5176\u5728\u524d\u7aef\u5f00\u53d1\u4e2d\u7684\u5e94\u7528\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u4e86\u89e3\u548c\u638c\u63e1Webman\u6846\u67b6\uff0c\u63d0\u9ad8\u524d\u7aef\u5f00\u53d1\u7684\u6548\u7387\u548c\u8d28\u91cf\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u63a2\u7d22Webman\uff1a\u4e00\u4e2a\u5f3a\u5927\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6807\u9898\uff1a\u63a2\u7d22Webman\uff1a\u4e00\u4e2a\u5f3a\u5927\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6 \u5f15\u8a00\uff1a\u968f\u7740\u4e92\u8054\u7f51\u7684\u8fc5\u901f\u53d1\u5c55\uff0c\u524d\u7aef\u5f00\u53d1\u6210\u4e3a\u8d8a\u6765\u8d8a\u70ed\u95e8\u7684\u804c\u4e1a\u9886\u57df\u3002\u4e3a\u4e86\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\uff0c\u524d\u7aef\u5f00\u53d1\u6846\u67b6\u5e94\u8fd0\u800c\u751f\u3002\u800cWebman\u4f5c\u4e3a\u4e00\u4e2a\u5f3a\u5927\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6\uff0c\u5728\u6700\u8fd1\u51e0\u5e74\u91cc\u9010\u6e10\u53d7\u5230\u4e86\u5f00\u53d1\u8005\u7684\u5173\u6ce8\u548c\u4f7f\u7528\u3002\u672c\u6587\u5c06\u63a2\u7d22Webman\u6846\u67b6\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u6f14\u793a\u5176\u5728\u524d\u7aef\u5f00\u53d1\u4e2d\u7684\u5e94\u7528\u3002 \u4e00\u3001\u4ec0\u4e48\u662fWebman\u6846\u67b6\uff1fWebman\u662f\u4e00\u4e2a\u57fa\u4e8eJavaScript\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6\uff0c\u96c6\u6210\u4e86\u591a\u79cd\u529f\u80fd\uff0c\u5305\u62ec\u8def\u7531\u7ba1\u7406\u3001\u6a21\u5757\u5316\u5f00\u53d1\u3001\u7ec4\u4ef6\u5316\u5f00\u53d1\u7b49\u3002\u5b83\u901a\u8fc7\u63d0\u4f9b\u4e00\u5957\u9ad8\u6548\u800c\u6613\u7528\u7684\u5de5\u5177\uff0c\u5927\u5927\u51cf\u5c11\u4e86\u524d\u7aef\u5f00\u53d1\u7684\u590d\u6742\u6027\u548c\u91cd\u590d\u6027\u5de5\u4f5c\uff0c\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u3002 \u4e8c\u3001\u8def\u7531\u7ba1\u7406Webman\u6846\u67b6\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8def\u7531\u7ba1\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u6784\u5efa\u5355\u9875\u5e94\u7528\u548c\u591a\u9875\u5e94\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import Webman from &#8216;webman&#8217; const router = new Webman.Router() router.route(&#8216;\/home&#8217;, () =&amp;gt; { \/\/ \u5904\u7406\u9996\u9875\u903b\u8f91 }) router.route(&#8216;\/about&#8217;, () =&amp;gt; { \/\/ \u5904\u7406\u5173\u4e8e\u9875\u9762\u903b\u8f91 }) router.start() \u767b\u5f55\u540e\u590d\u5236 \u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u5b9a\u4e49\u8def\u7531\u89c4\u5219\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u8def\u7531\u89c4\u5219\u7ed1\u5b9a\u76f8\u5e94\u7684\u5904\u7406\u51fd\u6570\u3002\u5f53\u7528\u6237\u8bbf\u95ee\u4e0d\u540c\u7684URL\u65f6\uff0c\u6846\u67b6\u4f1a\u81ea\u52a8\u8c03\u7528\u5bf9\u5e94\u7684\u5904\u7406\u51fd\u6570\uff0c\u5b9e\u73b0\u9875\u9762\u7684\u5207\u6362\u548c\u903b\u8f91\u5904\u7406\u3002 \u4e09\u3001\u6a21\u5757\u5316\u5f00\u53d1Webman\u6846\u67b6\u652f\u6301\u6a21\u5757\u5316\u5f00\u53d1\uff0c\u53ef\u4ee5\u5c06\u524d\u7aef\u4ee3\u7801\u5206\u5272\u4e3a\u591a\u4e2a\u6a21\u5757\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u590d\u7528\u6027\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a \/\/ moduleA.js export function foo() { \/\/ \u6a21\u5757A\u7684\u529f\u80fd\u5b9e\u73b0 } \/\/ moduleB.js export function bar() { \/\/ \u6a21\u5757B\u7684\u529f\u80fd\u5b9e\u73b0 } \/\/ main.js import { foo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24437","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24437"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24437\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}