{"id":113,"date":"2022-09-05T09:33:22","date_gmt":"2022-09-05T01:33:22","guid":{"rendered":"https:\/\/www.icfyless.com\/?p=113"},"modified":"2022-09-05T09:42:07","modified_gmt":"2022-09-05T01:42:07","slug":"vue%e4%bd%bf%e7%94%a8h%e5%87%bd%e6%95%b0%e5%88%9b%e5%bb%ba%e5%ad%90%e7%bb%84%e4%bb%b6%ef%bc%88%e6%b6%89%e5%8f%8a%e5%88%b0emitprops%e7%9a%84%e4%bc%a0%e9%80%92%e4%bb%a5%e5%8f%8a%e5%a4%9a%e4%b8%aa","status":"publish","type":"post","link":"https:\/\/www.icfyless.com\/index.php\/2022\/09\/05\/vue%e4%bd%bf%e7%94%a8h%e5%87%bd%e6%95%b0%e5%88%9b%e5%bb%ba%e5%ad%90%e7%bb%84%e4%bb%b6%ef%bc%88%e6%b6%89%e5%8f%8a%e5%88%b0emitprops%e7%9a%84%e4%bc%a0%e9%80%92%e4%bb%a5%e5%8f%8a%e5%a4%9a%e4%b8%aa\/","title":{"rendered":"vue\u4f7f\u7528h\u51fd\u6570\u521b\u5efa\u5b50\u7ec4\u4ef6\uff08\u6d89\u53ca\u5230$emit,props\u7684\u4f20\u9012\u4ee5\u53ca\u591a\u4e2a\u5177\u540d\u63d2\u69fd\u7684\u4f7f\u7528\uff09"},"content":{"rendered":"<h2>h\u51fd\u6570\u662f\u4ec0\u4e48<\/h2>\n<blockquote>\n<p>h\u51fd\u6570\u672c\u8d28\u5c31\u662fcreateElement() \u7684\u7b80\u5199\uff0c\u4f5c\u7528\u662f\u6839\u636e\u914d\u7f6e\u521b\u5efa\u5bf9\u5e94\u7684\u865a\u62df\u8282\u70b9<\/p>\n<\/blockquote>\n<h2>h\u51fd\u6570\u7684\u914d\u7f6e<\/h2>\n<h3>\u53c2\u6570<\/h3>\n<p>\u63a5\u6536\u4e09\u4e2a\u53c2\u6570\uff1a type,props,children<\/p>\n<h3>type<\/h3>\n<ul>\n<li>\u7c7b\u578b\uff1a String|Object|Function<\/li>\n<li>\u8be6\u7ec6\uff1aHTML\u6807\u7b7e\u540d\u3001\u7ec4\u4ef6\u3001\u5f02\u6b65\u7ec4\u4ef6\u6216\u51fd\u6570\u5f0f\u7ec4\u4ef6 <strong>\uff08\u6ce8\u610f\uff1aVue3\u4e0d\u652f\u6301\u7ec4\u4ef6\u540d\u7528\u5b57\u7b26\u4e32\u6807\u8bc6\u4e86\uff0c\u5fc5\u987b\u76f4\u63a5\u4f7f\u7528\u7ec4\u4ef6\u540d\uff09<\/strong><\/li>\n<\/ul>\n<pre><code class=\"language-typescript\">import MySon from &#039;.\/son.vue&#039;\n\nh(&#039;div&#039;, {}, [\n    h(MySon, {props: {name: &#039;hhh&#039;}}) \/\/ MySon \u4e0d\u53ef\u5199\u6210 &#039;MySon&#039;\n])<\/code><\/pre>\n<h3>prop<\/h3>\n<ul>\n<li>\u7c7b\u578b\uff1a Object<\/li>\n<\/ul>\n<pre><code class=\"language-typescript\">{\n  \/\/ \u548c`v-bind:class`\u4e00\u6837\u7684 API\n  &#039;class&#039;: {\n    foo: true,\n    bar: false\n  },\n  \/\/ \u548c`v-bind:style`\u4e00\u6837\u7684 API\n  style: {\n    color: &#039;red&#039;,\n    fontSize: &#039;14px&#039;\n  },\n  \/\/ \u6b63\u5e38\u7684 HTML \u7279\u6027\n  attrs: {\n    id: &#039;foo&#039;\n  },\n  \/\/ \u7ec4\u4ef6 props\n  props: {\n    myProp: &#039;bar&#039;\n  },\n  \/\/ DOM \u5c5e\u6027\n  domProps: {\n    innerHTML: &#039;baz&#039;\n  },\n  \/\/ \u4e8b\u4ef6\u76d1\u542c\u5668\u57fa\u4e8e `on`\n  \/\/ \u6240\u4ee5\u4e0d\u518d\u652f\u6301\u5982 `v-on:keyup.enter` \u4fee\u9970\u5668\n  \/\/ \u9700\u8981\u624b\u52a8\u5339\u914d keyCode\u3002\n  on: {\n    click: this.clickHandler\n  },\n  \/\/ \u4ec5\u5bf9\u4e8e\u7ec4\u4ef6\uff0c\u7528\u4e8e\u76d1\u542c\u539f\u751f\u4e8b\u4ef6\uff0c\u800c\u4e0d\u662f\u7ec4\u4ef6\u5185\u90e8\u4f7f\u7528\n  \/\/ `vm.$emit` \u89e6\u53d1\u7684\u4e8b\u4ef6\u3002\n  nativeOn: {\n    click: this.nativeClickHandler\n  },\n  \/\/ \u81ea\u5b9a\u4e49\u6307\u4ee4\u3002\u6ce8\u610f\uff0c\u4f60\u65e0\u6cd5\u5bf9 `binding` \u4e2d\u7684 `oldValue`\n  \/\/ \u8d4b\u503c\uff0c\u56e0\u4e3a Vue \u5df2\u7ecf\u81ea\u52a8\u4e3a\u4f60\u8fdb\u884c\u4e86\u540c\u6b65\u3002\n  directives: [\n    {\n      name: &#039;my-custom-directive&#039;,\n      value: &#039;2&#039;,\n      expression: &#039;1 + 1&#039;,\n      arg: &#039;foo&#039;,\n      modifiers: {\n        bar: true\n      }\n    }\n  ],\n  \/\/ Scoped slots in the form of\n  \/\/ { name: props =&gt; VNode | Array&lt;VNode&gt; }\n  scopedSlots: {\n    default: props =&gt; createElement(&#039;span&#039;, props.text)\n  },\n  \/\/ \u5982\u679c\u7ec4\u4ef6\u662f\u5176\u4ed6\u7ec4\u4ef6\u7684\u5b50\u7ec4\u4ef6\uff0c\u9700\u4e3a\u63d2\u69fd\u6307\u5b9a\u540d\u79f0\n  slot: &#039;name-of-slot&#039;,\n  \/\/ \u5176\u4ed6\u7279\u6b8a\u9876\u5c42\u5c5e\u6027\n  key: &#039;myKey&#039;,\n  ref: &#039;myRef&#039;\n}<\/code><\/pre>\n<h3>children<\/h3>\n<ul>\n<li>\u7c7b\u578b\uff1aString|Object|Array<\/li>\n<li>String<\/li>\n<\/ul>\n<pre><code class=\"language-typescript\">h(&#039;div&#039;, {}, &#039;Some text comes first.&#039;)<\/code><\/pre>\n<ul>\n<li>Array<\/li>\n<\/ul>\n<pre><code class=\"language-typescript\">h(&#039;div&#039;, {}, [\n  &#039;Some text comes first.&#039;,\n  h(&#039;h1&#039;, &#039;A headline&#039;),\n  h(MyComponent, {\n    props: {\n        name: &#039;hhh&#039;\n    }\n  })\n])<\/code><\/pre>\n<p>\u751f\u6210\u7684\u865a\u62df\u8282\u70b9<\/p>\n<pre><code class=\"language-vue\">&lt;div&gt;\n    Some text comes first.\n    &lt;h1&gt;A headline&lt;\/h1&gt;\n    &lt;MyComponent name=&quot;hhh&quot; \/&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<ul>\n<li>Object<\/li>\n<\/ul>\n<pre><code class=\"language-tsx\">\/\/ \u5b50\u7ec4\u4ef6\uff1a\n&lt;template&gt;\n    &lt;div&gt;\n        \u4f60\u597d\uff0c\u6211\u662f\u5b50\u7ec4\u4ef6\uff0c\u4e0b\u9762\u662f\u4e24\u4e2a\u5177\u540d\u63d2\u69fd\n        &lt;template #content \/&gt;\n        &lt;template #contentTips \/&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n\n\/\/ \u7236\u7ec4\u4ef6\uff1a\nconst props = {\n    name: &#039;aaa&#039;,\n    age: 18\n}\nconst slots = {}\n[&#039;content&#039;, &#039;contentTips&#039;].forEach(name =&gt; slots[name] = h(&#039;div&#039;, {key: name}, name));\nh(MySon, {...props}, slots);<\/code><\/pre>\n<h2>\u5173\u4e8eVue3\u4e2dh\u51fd\u6570\u5982\u4f55\u63a5\u6536\u5b50\u7ec4\u4ef6$emit\u53d1\u9001\u7684\u4e8b\u4ef6<\/h2>\n<pre><code class=\"language-tsx\">\/\/ vue2\u5199\u6cd5\n\/\/ \u5b50\u7ec4\u4ef6 MySon \uff1a\n&lt;template&gt;\n    &lt;div&gt;\n        &lt;button @click.stop=&quot;$emit(&#039;start&#039;, &#039;gogogo!!!&#039;)&quot;\/&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n\n\/\/ \u7236\u7ec4\u4ef6\u7684 h \u51fd\u6570\uff1a\nimport MySon from &#039;.\/son.vue&#039;\n\nh(MySon, {\n    \/\/\u5b50\u7ec4\u4ef6 $emit \u4f20\u9012\u51fd\u6570\n    start(data) {\n        console.log(data);\n    },\n})\n\/\/ vue3\u5199\u6cd5 \uff08\u7ed1\u5b9a\u7684\u4e8b\u4ef6\u540d\u9700\u8981\u52a0\u591a\u4e00\u4e2aon\u524d\u7f00\uff09\n\/\/ \u5b50\u7ec4\u4ef6\uff1a\n&lt;template&gt;\n    &lt;div&gt;\n        &lt;button @click.stop=&quot;$emit(&#039;start&#039;, &#039;gogogo!!!&#039;)&quot; \/&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n\n\/\/ \u7236\u7ec4\u4ef6\u7684 h \u51fd\u6570\uff1a\nh(TableActionButtons, {\n    \/\/ \u5b50\u7ec4\u4ef6 $emit \u4f20\u9012\u51fd\u6570\uff01\uff01\uff01\uff01\n    onStart(data) {\n        console.log(data);\n    },\n})<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>h\u51fd\u6570\u662f\u4ec0\u4e48 h\u51fd\u6570\u672c\u8d28\u5c31\u662fcreateEleme &hellip;<\/p>\n","protected":false},"author":1,"featured_media":114,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vue"],"_links":{"self":[{"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":3,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":117,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/posts\/113\/revisions\/117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/media\/114"}],"wp:attachment":[{"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.icfyless.com\/index.php\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}