{"id":206,"date":"2020-09-01T22:54:26","date_gmt":"2020-09-01T14:54:26","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=206"},"modified":"2020-09-30T17:56:26","modified_gmt":"2020-09-30T09:56:26","slug":"%e7%bb%84%e4%bb%b6%e7%bc%96%e7%a8%8b%e4%b8%89","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2020\/09\/01\/%e7%bb%84%e4%bb%b6%e7%bc%96%e7%a8%8b%e4%b8%89\/","title":{"rendered":"\u7ec4\u4ef6\u7f16\u7a0b\u4e09"},"content":{"rendered":"<hr \/>\n<h3>\u6837\u5f0f\u8986\u76d6<\/h3>\n<p>\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7684\u95ee\u9898\u5728\u4e8e\u6837\u5f0f\u8986\u76d6\uff1a<\/p>\n<pre data-language=HTML><code class=\"language-markup line-numbers\">&lt;style type=\"text\/css\"&gt;\n    button {\n      background: #8B008B;\n      color: white;\n      padding: 2ch 4ch;\n      border: 0;\n      font-size: 1.5ch;\n    }\n&lt;\/style&gt;\n&lt;wa-o&gt;\n  &lt;button&gt;WaO&lt;\/button&gt;\n&lt;\/wa-o&gt;\n<\/code><\/pre>\n<pre><code class=\"language-javascript line-numbers\">class WaO extends HTMLElement {\n\n  connectedCallback() {\n    const button = this.querySelector(\"button\");\n    button.addEventListener(\"click\", this.handleClick);\n  }\n\n  handleClick(e) {\n    alert(\"WaO\");\n  }\n\n}\n\ncustomElements.define('wa-o', WaO);\n<\/code><\/pre>\n<p><img src=\"https:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/web_component3_1.png\" alt=\"\u7ec4\u4ef6\u7f16\u7a0b\u4e09\" \/><\/p>\n<p>\u8fd9\u91cc\u9700\u8981\u7ec4\u4ef6\u5185\u6837\u5f0f\u4e0e\u5168\u5c40\u6837\u5f0f\u5206\u5f00\uff0c\u7531\u4e8e\u6807\u7b7e\u5d4c\u5957\uff0c\u53d7\u5168\u5c40\u6837\u5f0f\u5f71\u54cd\uff0c\u7ec4\u4ef6\u5185\u6837\u5f0f\u8ddf\u968f\u5168\u5c40\u6837\u5f0f\u3002<\/p>\n<p><img src=\"https:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/web_component3_2.png\" alt=\"\u7ec4\u4ef6\u7f16\u7a0b\u4e09\" \/><\/p>\n<p>\u540c\u6837\u7684\uff0c\u5982\u679c\u5b9a\u4e49\u7ec4\u4ef6\u5185\u6837\u5f0f\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">const template = document.createElement('template');\ntemplate.innerHTML = `\n&lt;style&gt;\nbutton {\n  background: #8B008B;\n  color: white;\n  padding: 2ch 4ch;\n  border: 0;\n  font-size: 1.5ch;\n}\n&lt;\/style&gt;\n&lt;button&gt;WaOooooo&lt;\/button&gt;`;\n<\/code><\/pre>\n<h3>\u7ec4\u4ef6\u5185\u6837\u5f0f<\/h3>\n<p>\u8fd9\u6837\u5c31\u65e0\u6cd5\u5b9a\u4e49\u5168\u5c40\u6837\u5f0f\uff0c\u5728\u4fdd\u8bc1\u6027\u80fd\u7684\u60c5\u51b5\u4e0b\u9700\u8981\u5c06attachShadow\u7684mode\u6253\u5f00\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">const template = document.createElement('template');\ntemplate.innerHTML = `\n&lt;style&gt;\n@import \".\/wahaha.css\";\n&lt;\/style&gt;\n&lt;button&gt;WaOooooo&lt;\/button&gt;`;\n\n\/\/...\nconnectedCallback() {\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.appendChild(template.content.cloneNode(true));\n\/\/...\n}\n<\/code><\/pre>\n<p>\u4f46\u662f\u8fd9\u6837\u4f9d\u7136\u4f1a\u6709\u95ee\u9898\uff0c\u56e0\u4e3a\u662f\u5bfc\u5165\u7684\u6837\u5f0f\uff0c\u6837\u5f0f\u4f1a\u6709\u52a0\u8f7d\u8fc7\u7a0b\uff0c\u8fd9\u6bb5\u65f6\u95f4\u5185\u662f\u6ca1\u6709\u6837\u5f0f\u53ea\u6709\u5185\u5bb9\u7684\uff08\u79f0\u4e3a\uff1aFOUC-Flash of Unstyled Content\u6216\u8005FUUC-Flash Un unstyled Component\uff09\uff0c\u6240\u4ee5\u8fd8\u9700\u8981\u5b9a\u4e49\u884c\u5185\u6837\u5f0f\uff0c\ud83e\udd14<\/p>\n<p>\u5f53\u7136\u8fd9\u4e5f\u6709\u5176\u4ed6\u7684\u65b9\u6848\uff0c\u6bd4\u5982<strong>part<\/strong>\uff0c\u4e0d\u5728\u6b64\u8ba8\u8bba\u8303\u56f4\u5185\u3002<\/p>\n<h3>\u81ea\u5b9a\u4e49\u6837\u5f0f<\/h3>\n<p>\u4e5f\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u6837\u5f0f\uff0c\u7136\u540e<strong>var<\/strong>\u6765\u4f20\u9012\u6837\u5f0f\u3002\u55ef&#8230;\ud83e\udd14<\/p>\n<p>\u4e0d\u5728\u6b64\u8ba8\u8bba\u8303\u56f4\u5185\uff0c\u7565\u3002<\/p>\n<h3>slot<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528slot\uff1a<\/p>\n<pre data-language=HTML><code class=\"language-markup line-numbers\">&lt;style type=\"text\/css\"&gt;\n    header {\n      background: red;\n    }\n\n    h2 {\n      color: red;\n    }\n    .module {\n      border: 1px solid red;\n    }\n&lt;\/style&gt;\n&lt;i-header&gt;&lt;\/i-header&gt;\n\n&lt;i-module&gt;\n  &lt;h2 slot=\"header\"&gt;Indeex Moduleeeeeeeee&lt;\/h2&gt;\n&lt;\/i-module&gt;\n<\/code><\/pre>\n<pre><code class=\"language-javascript line-numbers\">class IHeader extends HTMLElement {\n  constructor() {\n    super();\n    const headerTemplate = document.createElement('template');\n    headerTemplate.innerHTML = `\n      &lt;style&gt;\n        header {\n          background: #861d1d;\n          color: white;\n          padding: 4ch;\n        }\n      &lt;\/style&gt;\n      &lt;header&gt;\n        WaOoooo Indeex Header Navvvvvvvv....\n      &lt;\/header&gt;\n    `;\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.appendChild(headerTemplate.content.cloneNode(true));\n  }\n\n}\n\nclass IModule extends HTMLElement {\n\n  constructor() {\n    super();\n\n    const moduleTemplate = document.createElement('template');\n    moduleTemplate.innerHTML = `\n      &lt;style&gt;\n        .module {\n            min-height: 60vh;\n           padding: 4ch;\n           background: pink;\n        }\n      &lt;\/style&gt;\n      &lt;div class=\"module\"&gt;\n        &lt;slot name=\"header\"&gt;&lt;\/slot&gt;\n      &lt;\/div&gt;\n    `;\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.appendChild(moduleTemplate.content.cloneNode(true));\n  }\n\n}\n\ncustomElements.define('i-header', IHeader);\ncustomElements.define('i-module', IModule);\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4e2d\u8f6c\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5168\u5c40\u6837\u5f0f <a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/hungking.cc\/web-components\/slot.html\">\u9884\u89c8<\/a><\/p>\n<h3>\u5176\u4ed6\u65b9\u5f0f<\/h3>\n<p>\u5f53\u7136\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6848\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre data-language=HTML><code class=\"language-markup line-numbers\">&lt;submit-form&gt;\n  #shadow-root\n    &lt;x-form exportparts=\"some-input some-box\"&gt;\n      #shadow-root\n        &lt;x-bar exportparts=\"some-input some-box\"&gt;\n          #shadow-root\n            &lt;x-foo exportparts=\"some-input some-box\"&gt;&lt;\/x-foo&gt;\n        &lt;\/x-bar&gt;\n    &lt;\/x-form&gt;\n&lt;\/submit-form&gt;\n\n&lt;x-form&gt;&lt;\/x-form&gt;\n&lt;x-bar&gt;&lt;\/x-bar&gt;\n<\/code><\/pre>\n<pre><code class=\"language-css line-numbers\">:root::part(some-input) { ... }\n<\/code><\/pre>\n<p>\u5f53\u7136\uff0c\u8fd9\u4e5f\u6709\u5f88\u591a\u95ee\u9898\uff0c\u81f3\u5c11\u5f00\u53d1\u8d77\u6765\u5f88\u3002\u3002\u55ef&#8230;\ud83e\udd14<\/p>\n<p>code enjoy!\ud83d\ude1c\ud83d\ude1c\ud83d\ude1c<\/p>\n<p>\u4f5c\u8005\uff1aindeex<\/p>\n<p>\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/indeex.club\/\">https:\/\/indeex.club<\/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>\u6837\u5f0f\u8986\u76d6 \u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7684\u95ee\u9898\u5728\u4e8e\u6837\u5f0f\u8986\u76d6\uff1a &lt;style type=&#8221;text\/css&#8221;&gt;<a href=\"https:\/\/blog.indeex.club\/index.php\/2020\/09\/01\/%e7%bb%84%e4%bb%b6%e7%bc%96%e7%a8%8b%e4%b8%89\/\" 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\/206"}],"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=206"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":207,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/206\/revisions\/207"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}