{"id":185802,"date":"2023-07-01T19:07:10","date_gmt":"2023-07-01T11:07:10","guid":{"rendered":"http:\/\/www.idc.net\/help\/185802\/"},"modified":"2023-07-01T19:07:10","modified_gmt":"2023-07-01T11:07:10","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91%e5%b0%8f%e7%a8%8b%e5%ba%8f%e6%95%99%e7%a8%8b%ef%bc%9a%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f-%e7%bb%84%e4%bb%b6%e6%a8%a1%e6%9d%bf%e5%92%8c%e6%a0%b7%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/185802\/","title":{"rendered":"\u540e\u6d6a\u4e91\u5c0f\u7a0b\u5e8f\u6559\u7a0b\uff1a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u7ec4\u4ef6\u6a21\u677f\u548c\u6837\u5f0f"},"content":{"rendered":"<h2>\u7ec4\u4ef6\u6a21\u677f\u548c\u6837\u5f0f<\/h2>\n<p>\u7c7b\u4f3c\u4e8e\u9875\u9762\uff0c\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u62e5\u6709\u81ea\u5df1\u7684&nbsp;wxml&nbsp;\u6a21\u677f\u548c&nbsp;wxss&nbsp;\u6837\u5f0f\u3002<\/p>\n<h3>\u7ec4\u4ef6\u6a21\u677f<\/h3>\n<p>\u7ec4\u4ef6\u6a21\u677f\u7684\u5199\u6cd5\u4e0e\u9875\u9762\u6a21\u677f\u76f8\u540c\u3002\u7ec4\u4ef6\u6a21\u677f\u4e0e\u7ec4\u4ef6\u6570\u636e\u7ed3\u5408\u540e\u751f\u6210\u7684\u8282\u70b9\u6811\uff0c\u5c06\u88ab\u63d2\u5165\u5230\u7ec4\u4ef6\u7684\u5f15\u7528\u4f4d\u7f6e\u4e0a\u3002<\/p>\n<p>\u5728\u7ec4\u4ef6\u6a21\u677f\u4e2d\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a&nbsp;&lt;slot&gt;&nbsp;\u8282\u70b9\uff0c\u7528\u4e8e\u627f\u8f7d\u7ec4\u4ef6\u5f15\u7528\u65f6\u63d0\u4f9b\u7684\u5b50\u8282\u70b9\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;!-- \u7ec4\u4ef6\u6a21\u677f --&gt;\n&lt;view class=\"wrapper\"&gt;\n  &lt;view&gt;\u8fd9\u91cc\u662f\u7ec4\u4ef6\u7684\u5185\u90e8\u8282\u70b9&lt;\/view&gt;\n  &lt;slot&gt;&lt;\/slot&gt;\n&lt;\/view&gt;\n<\/code><\/pre>\n<pre><code>&lt;!-- \u5f15\u7528\u7ec4\u4ef6\u7684\u9875\u9762\u6a21\u677f --&gt;\n&lt;view&gt;\n  &lt;component-tag-name&gt;\n    &lt;!-- \u8fd9\u90e8\u5206\u5185\u5bb9\u5c06\u88ab\u653e\u7f6e\u5728\u7ec4\u4ef6 &lt;slot&gt; \u7684\u4f4d\u7f6e\u4e0a --&gt;\n    &lt;view&gt;\u8fd9\u91cc\u662f\u63d2\u5165\u5230\u7ec4\u4ef6slot\u4e2d\u7684\u5185\u5bb9&lt;\/view&gt;\n  &lt;\/component-tag-name&gt;\n&lt;\/view&gt;\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u5728\u6a21\u677f\u4e2d\u5f15\u7528\u5230\u7684\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u53ca\u5176\u5bf9\u5e94\u7684\u8282\u70b9\u540d\u9700\u8981\u5728&nbsp;json&nbsp;\u6587\u4ef6\u4e2d\u663e\u5f0f\u5b9a\u4e49\uff0c\u5426\u5219\u4f1a\u88ab\u5f53\u4f5c\u4e00\u4e2a\u65e0\u610f\u4e49\u7684\u8282\u70b9\u3002\u9664\u6b64\u4ee5\u5916\uff0c\u8282\u70b9\u540d\u4e5f\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3a\u62bd\u8c61\u8282\u70b9\u3002<\/p>\n<h3>\u6a21\u677f\u6570\u636e\u7ed1\u5b9a<\/h3>\n<p>\u4e0e\u666e\u901a\u7684 WXML \u6a21\u677f\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u7ed1\u5b9a\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5411\u5b50\u7ec4\u4ef6\u7684\u5c5e\u6027\u4f20\u9012\u52a8\u6001\u6570\u636e\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;!-- \u5f15\u7528\u7ec4\u4ef6\u7684\u9875\u9762\u6a21\u677f --&gt;\n&lt;view&gt;\n  &lt;component-tag-name prop-a=\"{{dataFieldA}}\" prop-b=\"{{dataFieldB}}\"&gt;\n    &lt;!-- \u8fd9\u90e8\u5206\u5185\u5bb9\u5c06\u88ab\u653e\u7f6e\u5728\u7ec4\u4ef6 &lt;slot&gt; \u7684\u4f4d\u7f6e\u4e0a --&gt;\n    &lt;view&gt;\u8fd9\u91cc\u662f\u63d2\u5165\u5230\u7ec4\u4ef6slot\u4e2d\u7684\u5185\u5bb9&lt;\/view&gt;\n  &lt;\/component-tag-name&gt;\n&lt;\/view&gt;\n<\/code><\/pre>\n<p>\u5728\u4ee5\u4e0a\u4f8b\u5b50\u4e2d\uff0c\u7ec4\u4ef6\u7684\u5c5e\u6027&nbsp;propA&nbsp;\u548c&nbsp;propB&nbsp;\u5c06\u6536\u5230\u9875\u9762\u4f20\u9012\u7684\u6570\u636e\u3002\u9875\u9762\u53ef\u4ee5\u901a\u8fc7&nbsp;setData&nbsp;\u6765\u6539\u53d8\u7ed1\u5b9a\u7684\u6570\u636e\u5b57\u6bb5\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u8fd9\u6837\u7684\u6570\u636e\u7ed1\u5b9a\u53ea\u80fd\u4f20\u9012 JSON \u517c\u5bb9\u6570\u636e\u3002\u81ea\u57fa\u7840\u5e93\u7248\u672c&nbsp;2.0.9&nbsp;\u5f00\u59cb\uff0c\u8fd8\u53ef\u4ee5\u5728\u6570\u636e\u4e2d\u5305\u542b\u51fd\u6570\uff08\u4f46\u8fd9\u4e9b\u51fd\u6570\u4e0d\u80fd\u5728 WXML \u4e2d\u76f4\u63a5\u8c03\u7528\uff0c\u53ea\u80fd\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\uff09\u3002<\/p>\n<h3>\u7ec4\u4ef6 wxml \u7684 slot<\/h3>\n<p>\u5728\u7ec4\u4ef6\u7684 wxml \u4e2d\u53ef\u4ee5\u5305\u542b&nbsp;slot&nbsp;\u8282\u70b9\uff0c\u7528\u4e8e\u627f\u8f7d\u7ec4\u4ef6\u4f7f\u7528\u8005\u63d0\u4f9b\u7684 wxml \u7ed3\u6784\u3002<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a\u7ec4\u4ef6\u7684 wxml \u4e2d\u53ea\u80fd\u6709\u4e00\u4e2a slot \u3002\u9700\u8981\u4f7f\u7528\u591a slot \u65f6\uff0c\u53ef\u4ee5\u5728\u7ec4\u4ef6 js \u4e2d\u58f0\u660e\u542f\u7528\u3002<\/p>\n<pre><code>Component({\n  options: {\n    multipleSlots: true \/\/ \u5728\u7ec4\u4ef6\u5b9a\u4e49\u65f6\u7684\u9009\u9879\u4e2d\u542f\u7528\u591aslot\u652f\u6301\n  },\n  properties: { \/* ... *\/ },\n  methods: { \/* ... *\/ }\n})\n<\/code><\/pre>\n<p>\u6b64\u65f6\uff0c\u53ef\u4ee5\u5728\u8fd9\u4e2a\u7ec4\u4ef6\u7684 wxml \u4e2d\u4f7f\u7528\u591a\u4e2a slot \uff0c\u4ee5\u4e0d\u540c\u7684&nbsp;name&nbsp;\u6765\u533a\u5206\u3002<\/p>\n<pre><code>&lt;!-- \u7ec4\u4ef6\u6a21\u677f --&gt;\n&lt;view class=\"wrapper\"&gt;\n  &lt;slot name=\"before\"&gt;&lt;\/slot&gt;\n  &lt;view&gt;\u8fd9\u91cc\u662f\u7ec4\u4ef6\u7684\u5185\u90e8\u7ec6\u8282&lt;\/view&gt;\n  &lt;slot name=\"after\"&gt;&lt;\/slot&gt;\n&lt;\/view&gt;\n<\/code><\/pre>\n<p>\u4f7f\u7528\u65f6\uff0c\u7528&nbsp;slot&nbsp;\u5c5e\u6027\u6765\u5c06\u8282\u70b9\u63d2\u5165\u5230\u4e0d\u540c\u7684 slot \u4e0a\u3002<\/p>\n<pre><code>&lt;!-- \u5f15\u7528\u7ec4\u4ef6\u7684\u9875\u9762\u6a21\u677f --&gt;\n&lt;view&gt;\n  &lt;component-tag-name&gt;\n    &lt;!-- \u8fd9\u90e8\u5206\u5185\u5bb9\u5c06\u88ab\u653e\u7f6e\u5728\u7ec4\u4ef6 &lt;slot name=\"before\"&gt; \u7684\u4f4d\u7f6e\u4e0a --&gt;\n    &lt;view slot=\"before\"&gt;\u8fd9\u91cc\u662f\u63d2\u5165\u5230\u7ec4\u4ef6slot name=\"before\"\u4e2d\u7684\u5185\u5bb9&lt;\/view&gt;\n    &lt;!-- \u8fd9\u90e8\u5206\u5185\u5bb9\u5c06\u88ab\u653e\u7f6e\u5728\u7ec4\u4ef6 &lt;slot name=\"after\"&gt; \u7684\u4f4d\u7f6e\u4e0a --&gt;\n    &lt;view slot=\"after\"&gt;\u8fd9\u91cc\u662f\u63d2\u5165\u5230\u7ec4\u4ef6slot name=\"after\"\u4e2d\u7684\u5185\u5bb9&lt;\/view&gt;\n  &lt;\/component-tag-name&gt;\n&lt;\/view&gt;\n<\/code><\/pre>\n<h3>\u7ec4\u4ef6\u6837\u5f0f<\/h3>\n<p>\u7ec4\u4ef6\u5bf9\u5e94&nbsp;wxss&nbsp;\u6587\u4ef6\u7684\u6837\u5f0f\uff0c\u53ea\u5bf9\u7ec4\u4ef6wxml\u5185\u7684\u8282\u70b9\u751f\u6548\u3002\u7f16\u5199\u7ec4\u4ef6\u6837\u5f0f\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u7ec4\u4ef6\u548c\u5f15\u7528\u7ec4\u4ef6\u7684\u9875\u9762\u4e0d\u80fd\u4f7f\u7528id\u9009\u62e9\u5668\uff08#a\uff09\u3001\u5c5e\u6027\u9009\u62e9\u5668\uff08[a]\uff09\u548c\u6807\u7b7e\u540d\u9009\u62e9\u5668\uff0c\u8bf7\u6539\u7528class\u9009\u62e9\u5668\u3002<\/li>\n<li>\u7ec4\u4ef6\u548c\u5f15\u7528\u7ec4\u4ef6\u7684\u9875\u9762\u4e2d\u4f7f\u7528\u540e\u4ee3\u9009\u62e9\u5668\uff08.a .b\uff09\u5728\u4e00\u4e9b\u6781\u7aef\u60c5\u51b5\u4e0b\u4f1a\u6709\u975e\u9884\u671f\u7684\u8868\u73b0\uff0c\u5982\u9047\uff0c\u8bf7\u907f\u514d\u4f7f\u7528\u3002<\/li>\n<li>\u5b50\u5143\u7d20\u9009\u62e9\u5668\uff08.a&gt;.b\uff09\u53ea\u80fd\u7528\u4e8e&nbsp;view&nbsp;\u7ec4\u4ef6\u4e0e\u5176\u5b50\u8282\u70b9\u4e4b\u95f4\uff0c\u7528\u4e8e\u5176\u4ed6\u7ec4\u4ef6\u53ef\u80fd\u5bfc\u81f4\u975e\u9884\u671f\u7684\u60c5\u51b5\u3002<\/li>\n<li>\u7ee7\u627f\u6837\u5f0f\uff0c\u5982&nbsp;font&nbsp;\u3001&nbsp;color&nbsp;\uff0c\u4f1a\u4ece\u7ec4\u4ef6\u5916\u7ee7\u627f\u5230\u7ec4\u4ef6\u5185\u3002<\/li>\n<li>\u9664\u7ee7\u627f\u6837\u5f0f\u5916\uff0c&nbsp;app.wxss&nbsp;\u4e2d\u7684\u6837\u5f0f\u3001\u7ec4\u4ef6\u6240\u5728\u9875\u9762\u7684\u7684\u6837\u5f0f\u5bf9\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u65e0\u6548\uff08\u9664\u975e\u66f4\u6539\u7ec4\u4ef6\u6837\u5f0f\u9694\u79bb\u9009\u9879\uff09\u3002<\/li>\n<\/ul>\n<pre><code>#a { } \/* \u5728\u7ec4\u4ef6\u4e2d\u4e0d\u80fd\u4f7f\u7528 *\/\n[a] { } \/* \u5728\u7ec4\u4ef6\u4e2d\u4e0d\u80fd\u4f7f\u7528 *\/\nbutton { } \/* \u5728\u7ec4\u4ef6\u4e2d\u4e0d\u80fd\u4f7f\u7528 *\/\n.a &gt; .b { } \/* \u9664\u975e .a \u662f view \u7ec4\u4ef6\u8282\u70b9\uff0c\u5426\u5219\u4e0d\u4e00\u5b9a\u4f1a\u751f\u6548 *\/\n<\/code><\/pre>\n<p>\u9664\u6b64\u4ee5\u5916\uff0c\u7ec4\u4ef6\u53ef\u4ee5\u6307\u5b9a\u5b83\u6240\u5728\u8282\u70b9\u7684\u9ed8\u8ba4\u6837\u5f0f\uff0c\u4f7f\u7528&nbsp;:host&nbsp;\u9009\u62e9\u5668\uff08\u9700\u8981\u5305\u542b\u57fa\u7840\u5e93&nbsp;1.7.2&nbsp;\u6216\u66f4\u9ad8\u7248\u672c\u7684\u5f00\u53d1\u8005\u5de5\u5177\u652f\u6301\uff09\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code>\/* \u7ec4\u4ef6 custom-component.wxss *\/\n:host {\n  color: yellow;\n}\n<\/code><\/pre>\n<pre><code>&lt;!-- \u9875\u9762\u7684 WXML --&gt;\n&lt;custom-component&gt;\u8fd9\u6bb5\u6587\u672c\u662f\u9ec4\u8272\u7684&lt;\/custom-component&gt;\n<\/code><\/pre>\n<h3>\u7ec4\u4ef6\u6837\u5f0f\u9694\u79bb<\/h3>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7684\u6837\u5f0f\u53ea\u53d7\u5230\u81ea\u5b9a\u4e49\u7ec4\u4ef6 wxss \u7684\u5f71\u54cd\u3002\u9664\u975e\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>app.wxss&nbsp;\u6216\u9875\u9762\u7684&nbsp;wxss&nbsp;\u4e2d\u4f7f\u7528\u4e86\u6807\u7b7e\u540d\u9009\u62e9\u5668\uff08\u6216\u4e00\u4e9b\u5176\u4ed6\u7279\u6b8a\u9009\u62e9\u5668\uff09\u6765\u76f4\u63a5\u6307\u5b9a\u6837\u5f0f\uff0c\u8fd9\u4e9b\u9009\u62e9\u5668\u4f1a\u5f71\u54cd\u5230\u9875\u9762\u548c\u5168\u90e8\u7ec4\u4ef6\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\u8fd9\u662f\u4e0d\u63a8\u8350\u7684\u505a\u6cd5\u3002<\/li>\n<li>\u6307\u5b9a\u7279\u6b8a\u7684\u6837\u5f0f\u9694\u79bb\u9009\u9879&nbsp;styleIsolation&nbsp;\u3002<\/li>\n<\/ul>\n<pre><code>Component({\n  options: {\n    styleIsolation: 'isolated'\n  }\n})<\/code><\/pre>\n<p>styleIsolation&nbsp;\u9009\u9879\u4ece\u57fa\u7840\u5e93\u7248\u672c&nbsp;2.6.5&nbsp;\u5f00\u59cb\u652f\u6301\u3002\u5b83\u652f\u6301\u4ee5\u4e0b\u53d6\u503c\uff1a<\/p>\n<ul>\n<li>isolated&nbsp;\u8868\u793a\u542f\u7528\u6837\u5f0f\u9694\u79bb\uff0c\u5728\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u5185\u5916\uff0c\u4f7f\u7528 class \u6307\u5b9a\u7684\u6837\u5f0f\u5c06\u4e0d\u4f1a\u76f8\u4e92\u5f71\u54cd\uff08\u4e00\u822c\u60c5\u51b5\u4e0b\u7684\u9ed8\u8ba4\u503c\uff09\uff1b<\/li>\n<li>apply-shared&nbsp;\u8868\u793a\u9875\u9762 wxss \u6837\u5f0f\u5c06\u5f71\u54cd\u5230\u81ea\u5b9a\u4e49\u7ec4\u4ef6\uff0c\u4f46\u81ea\u5b9a\u4e49\u7ec4\u4ef6 wxss \u4e2d\u6307\u5b9a\u7684\u6837\u5f0f\u4e0d\u4f1a\u5f71\u54cd\u9875\u9762\uff1b<\/li>\n<li>shared&nbsp;\u8868\u793a\u9875\u9762 wxss \u6837\u5f0f\u5c06\u5f71\u54cd\u5230\u81ea\u5b9a\u4e49\u7ec4\u4ef6\uff0c\u81ea\u5b9a\u4e49\u7ec4\u4ef6 wxss \u4e2d\u6307\u5b9a\u7684\u6837\u5f0f\u4e5f\u4f1a\u5f71\u54cd\u9875\u9762\u548c\u5176\u4ed6\u8bbe\u7f6e\u4e86&nbsp;apply-shared&nbsp;\u6216&nbsp;shared&nbsp;\u7684\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u3002\uff08\u8fd9\u4e2a\u9009\u9879\u5728\u63d2\u4ef6\u4e2d\u4e0d\u53ef\u7528\u3002\uff09<\/li>\n<\/ul>\n<p>\u4f7f\u7528\u540e\u4e24\u8005\u65f6\uff0c\u8bf7\u52a1\u5fc5\u6ce8\u610f\u7ec4\u4ef6\u95f4\u6837\u5f0f\u7684\u76f8\u4e92\u5f71\u54cd\u3002<\/p>\n<p>\u5982\u679c\u8fd9\u4e2a&nbsp;Component \u6784\u9020\u5668\u7528\u4e8e\u6784\u9020\u9875\u9762&nbsp;\uff0c\u5219\u9ed8\u8ba4\u503c\u4e3a&nbsp;shared&nbsp;\uff0c\u4e14\u8fd8\u6709\u4ee5\u4e0b\u51e0\u4e2a\u989d\u5916\u7684\u6837\u5f0f\u9694\u79bb\u9009\u9879\u53ef\u7528\uff1a<\/p>\n<ul>\n<li>page-isolated&nbsp;\u8868\u793a\u5728\u8fd9\u4e2a\u9875\u9762\u7981\u7528 app.wxss \uff0c\u540c\u65f6\uff0c\u9875\u9762\u7684 wxss \u4e0d\u4f1a\u5f71\u54cd\u5230\u5176\u4ed6\u81ea\u5b9a\u4e49\u7ec4\u4ef6\uff1b<\/li>\n<li>page-apply-shared&nbsp;\u8868\u793a\u5728\u8fd9\u4e2a\u9875\u9762\u7981\u7528 app.wxss \uff0c\u540c\u65f6\uff0c\u9875\u9762 wxss \u6837\u5f0f\u4e0d\u4f1a\u5f71\u54cd\u5230\u5176\u4ed6\u81ea\u5b9a\u4e49\u7ec4\u4ef6\uff0c\u4f46\u8bbe\u4e3a&nbsp;shared&nbsp;\u7684\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u4f1a\u5f71\u54cd\u5230\u9875\u9762\uff1b<\/li>\n<li>page-shared&nbsp;\u8868\u793a\u5728\u8fd9\u4e2a\u9875\u9762\u7981\u7528 app.wxss \uff0c\u540c\u65f6\uff0c\u9875\u9762 wxss \u6837\u5f0f\u4f1a\u5f71\u54cd\u5230\u5176\u4ed6\u8bbe\u4e3a&nbsp;apply-shared&nbsp;\u6216&nbsp;shared&nbsp;\u7684\u81ea\u5b9a\u4e49\u7ec4\u4ef6\uff0c\u4e5f\u4f1a\u53d7\u5230\u8bbe\u4e3a&nbsp;shared&nbsp;\u7684\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7684\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<p>\u4ece\u5c0f\u7a0b\u5e8f\u57fa\u7840\u5e93\u7248\u672c&nbsp;2.10.1&nbsp;\u5f00\u59cb\uff0c\u4e5f\u53ef\u4ee5\u5728\u9875\u9762\u6216\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7684 json \u6587\u4ef6\u4e2d\u914d\u7f6e&nbsp;styleIsolation&nbsp;\uff08\u8fd9\u6837\u5c31\u4e0d\u9700\u5728 js \u6587\u4ef6\u7684&nbsp;options&nbsp;\u4e2d\u518d\u914d\u7f6e\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>{\n  \"styleIsolation\": \"isolated\"\n}\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u5c0f\u7a0b\u5e8f\u57fa\u7840\u5e93\u7248\u672c&nbsp;2.2.3&nbsp;\u4ee5\u4e0a\u652f\u6301&nbsp;addGlobalClass&nbsp;\u9009\u9879\uff0c\u5373\u5728&nbsp;Component&nbsp;\u7684&nbsp;options&nbsp;\u4e2d\u8bbe\u7f6e&nbsp;addGlobalClass: true&nbsp;\u3002 \u8fd9\u4e2a\u9009\u9879\u7b49\u4ef7\u4e8e\u8bbe\u7f6e&nbsp;styleIsolation: apply-shared&nbsp;\uff0c\u4f46\u8bbe\u7f6e\u4e86&nbsp;styleIsolation&nbsp;\u9009\u9879\u540e\u8fd9\u4e2a\u9009\u9879\u4f1a\u5931\u6548\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code>\/* \u7ec4\u4ef6 custom-component.js *\/\nComponent({\n  options: {\n    addGlobalClass: true,\n  }\n})\n<\/code><\/pre>\n<pre><code>&lt;!-- \u7ec4\u4ef6 custom-component.wxml --&gt;\n&lt;text class=\"red-text\"&gt;\u8fd9\u6bb5\u6587\u672c\u7684\u989c\u8272\u7531 `app.wxss` \u548c\u9875\u9762 `wxss` \u4e2d\u7684\u6837\u5f0f\u5b9a\u4e49\u6765\u51b3\u5b9a&lt;\/text&gt;\n<\/code><\/pre>\n<pre><code>\/* app.wxss *\/\n.red-text {\n  color: red;\n}\n<\/code><\/pre>\n<h3>\u5916\u90e8\u6837\u5f0f\u7c7b<\/h3>\n<blockquote><p>\n  \u57fa\u7840\u5e93 1.9.90 \u5f00\u59cb\u652f\u6301\uff0c\u4f4e\u7248\u672c\u9700\u505a \u517c\u5bb9\u5904\u7406\u3002\n<\/p><\/blockquote>\n<p>\u6709\u65f6\uff0c\u7ec4\u4ef6\u5e0c\u671b\u63a5\u53d7\u5916\u90e8\u4f20\u5165\u7684\u6837\u5f0f\u7c7b\u3002\u6b64\u65f6\u53ef\u4ee5\u5728&nbsp;Component&nbsp;\u4e2d\u7528&nbsp;externalClasses&nbsp;\u5b9a\u4e49\u6bb5\u5b9a\u4e49\u82e5\u5e72\u4e2a\u5916\u90e8\u6837\u5f0f\u7c7b\u3002<\/p>\n<p>\u8fd9\u4e2a\u7279\u6027\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u7c7b\u4f3c\u4e8e&nbsp;view&nbsp;\u7ec4\u4ef6\u7684&nbsp;hover-class&nbsp;\u5c5e\u6027\uff1a\u9875\u9762\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u6837\u5f0f\u7c7b\uff0c\u8d4b\u4e88&nbsp;view&nbsp;\u7684&nbsp;hover-class&nbsp;\uff0c\u8fd9\u4e2a\u6837\u5f0f\u7c7b\u672c\u8eab\u5199\u5728\u9875\u9762\u4e2d\u800c\u975e&nbsp;view&nbsp;\u7ec4\u4ef6\u7684\u5b9e\u73b0\u4e2d\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u5728\u540c\u4e00\u4e2a\u8282\u70b9\u4e0a\u4f7f\u7528\u666e\u901a\u6837\u5f0f\u7c7b\u548c\u5916\u90e8\u6837\u5f0f\u7c7b\u65f6\uff0c\u4e24\u4e2a\u7c7b\u7684\u4f18\u5148\u7ea7\u662f\u672a\u5b9a\u4e49\u7684\uff0c\u56e0\u6b64\u6700\u597d\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code>\/* \u7ec4\u4ef6 custom-component.js *\/\nComponent({\n  externalClasses: ['my-class']\n})\n<\/code><\/pre>\n<pre><code>&lt;!-- \u7ec4\u4ef6 custom-component.wxml --&gt;\n&lt;custom-component class=\"my-class\"&gt;\u8fd9\u6bb5\u6587\u672c\u7684\u989c\u8272\u7531\u7ec4\u4ef6\u5916\u7684 class \u51b3\u5b9a&lt;\/custom-component&gt;\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7ec4\u4ef6\u7684\u4f7f\u7528\u8005\u53ef\u4ee5\u6307\u5b9a\u8fd9\u4e2a\u6837\u5f0f\u7c7b\u5bf9\u5e94\u7684 class \uff0c\u5c31\u50cf\u4f7f\u7528\u666e\u901a\u5c5e\u6027\u4e00\u6837\u3002\u5728&nbsp;2.7.1&nbsp;\u4e4b\u540e\uff0c\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u5bf9\u5e94\u7684 class \u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;!-- \u9875\u9762\u7684 WXML --&gt;\n&lt;custom-component my-class=\"red-text\" \/&gt;\n&lt;custom-component my-class=\"large-text\" \/&gt;\n&lt;!-- \u4ee5\u4e0b\u5199\u6cd5\u9700\u8981\u57fa\u7840\u5e93\u7248\u672c 2.7.1 \u4ee5\u4e0a --&gt;\n&lt;custom-component my-class=\"red-text large-text\" \/&gt;\n<\/code><\/pre>\n<pre><code>.red-text {\n  color: red;\n}\n.large-text {\n  font-size: 1.5em;\n}\n<\/code><\/pre>\n<h3>\u5f15\u7528\u9875\u9762\u6216\u7236\u7ec4\u4ef6\u7684\u6837\u5f0f<\/h3>\n<blockquote><p>\n  \u57fa\u7840\u5e93 2.9.2 \u5f00\u59cb\u652f\u6301\uff0c\u4f4e\u7248\u672c\u9700\u505a \u517c\u5bb9\u5904\u7406\u3002\n<\/p><\/blockquote>\n<p>\u5373\u4f7f\u542f\u7528\u4e86\u6837\u5f0f\u9694\u79bb&nbsp;isolated&nbsp;\uff0c\u7ec4\u4ef6\u4ecd\u7136\u53ef\u4ee5\u5728\u5c40\u90e8\u5f15\u7528\u7ec4\u4ef6\u6240\u5728\u9875\u9762\u7684\u6837\u5f0f\u6216\u7236\u7ec4\u4ef6\u7684\u6837\u5f0f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u5728\u9875\u9762 wxss \u4e2d\u5b9a\u4e49\u4e86\uff1a<\/p>\n<pre><code>.blue-text {\n  color: blue;\n}\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u7ec4\u4ef6\u4e2d\u53ef\u4ee5\u4f7f\u7528&nbsp;~&nbsp;\u6765\u5f15\u7528\u8fd9\u4e2a\u7c7b\u7684\u6837\u5f0f\uff1a<\/p>\n<pre><code>&lt;view class=\"~blue-text\"&gt; \u8fd9\u6bb5\u6587\u672c\u662f\u84dd\u8272\u7684 &lt;\/view&gt;\n<\/code><\/pre>\n<p>\u5982\u679c\u5728\u4e00\u4e2a\u7ec4\u4ef6\u7684\u7236\u7ec4\u4ef6 wxss \u4e2d\u5b9a\u4e49\u4e86\uff1a<\/p>\n<pre><code>.red-text {\n  color: red;\n}\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u7ec4\u4ef6\u4e2d\u53ef\u4ee5\u4f7f\u7528&nbsp;^&nbsp;\u6765\u5f15\u7528\u8fd9\u4e2a\u7c7b\u7684\u6837\u5f0f\uff1a<\/p>\n<pre><code>&lt;view class=\"^red-text\"&gt; \u8fd9\u6bb5\u6587\u672c\u662f\u7ea2\u8272\u7684 &lt;\/view&gt;\n<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u8fde\u7eed\u4f7f\u7528\u591a\u4e2a&nbsp;^&nbsp;\u6765\u5f15\u7528\u7956\u5148\u7ec4\u4ef6\u4e2d\u7684\u6837\u5f0f\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u5982\u679c\u7ec4\u4ef6\u662f\u6bd4\u8f83\u72ec\u7acb\u3001\u901a\u7528\u7684\u7ec4\u4ef6\uff0c\u8bf7\u4f18\u5148\u4f7f\u7528\u5916\u90e8\u6837\u5f0f\u7c7b\u7684\u65b9\u5f0f\uff0c\u800c\u975e\u76f4\u63a5\u5f15\u7528\u7236\u7ec4\u4ef6\u6216\u9875\u9762\u7684\u6837\u5f0f\u3002<\/p>\n<h3>\u865a\u62df\u5316\u7ec4\u4ef6\u8282\u70b9<\/h3>\n<blockquote><p>\n  \u57fa\u7840\u5e93 2.11.2 \u5f00\u59cb\u652f\u6301\uff0c\u4f4e\u7248\u672c\u9700\u505a \u517c\u5bb9\u5904\u7406\u3002\n<\/p><\/blockquote>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u672c\u8eab\u7684\u90a3\u4e2a\u8282\u70b9\u662f\u4e00\u4e2a\u201c\u666e\u901a\u201d\u7684\u8282\u70b9\uff0c\u4f7f\u7528\u65f6\u53ef\u4ee5\u5728\u8fd9\u4e2a\u8282\u70b9\u4e0a\u8bbe\u7f6e&nbsp;class&nbsp;style&nbsp;\u3001\u52a8\u753b\u3001 flex \u5e03\u5c40\u7b49\uff0c\u5c31\u5982\u540c\u666e\u901a\u7684 view \u7ec4\u4ef6\u8282\u70b9\u4e00\u6837\u3002<\/p>\n<pre><code>&lt;!-- \u9875\u9762\u7684 WXML --&gt;\n&lt;view style=\"display: flex\"&gt;\n  &lt;!-- \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u4e00\u4e2a\u666e\u901a\u7684\u8282\u70b9 --&gt;\n  &lt;custom-component style=\"color: blue; flex: 1\"&gt;\u84dd\u8272\u3001\u6ee1\u5bbd\u7684&lt;\/custom-component&gt;\n&lt;\/view&gt;\n<\/code><\/pre>\n<p>\u4f46\u6709\u4e9b\u65f6\u5019\uff0c\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u5e76\u4e0d\u5e0c\u671b\u8fd9\u4e2a\u8282\u70b9\u672c\u8eab\u53ef\u4ee5\u8bbe\u7f6e\u6837\u5f0f\u3001\u54cd\u5e94 flex \u5e03\u5c40\u7b49\uff0c\u800c\u662f\u5e0c\u671b\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u5185\u90e8\u7684\u7b2c\u4e00\u5c42\u8282\u70b9\u80fd\u591f\u54cd\u5e94 flex \u5e03\u5c40\u6216\u8005\u6837\u5f0f\u7531\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u672c\u8eab\u5b8c\u5168\u51b3\u5b9a\u3002<\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u8bbe\u7f6e\u4e3a\u201c\u865a\u62df\u7684\u201d\uff1a<\/p>\n<pre><code>Component({\n  options: {\n    virtualHost: true\n  },\n  properties: {\n    style: { \/\/ \u5b9a\u4e49 style \u5c5e\u6027\u53ef\u4ee5\u62ff\u5230 style \u5c5e\u6027\u4e0a\u8bbe\u7f6e\u7684\u503c\n      type: String,\n    }\n  },\n  externalClass: ['class'], \/\/ \u53ef\u4ee5\u5c06 class \u8bbe\u4e3a externalClass\n})\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u53ef\u4ee5\u5c06 flex \u653e\u5165\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u5185\uff1a<\/p>\n<pre><code>&lt;!-- \u9875\u9762\u7684 WXML --&gt;\n&lt;view style=\"display: flex\"&gt;\n  &lt;!-- \u5982\u679c\u8bbe\u7f6e\u4e86 virtualHost \uff0c\u8282\u70b9\u4e0a\u7684\u6837\u5f0f\u5c06\u5931\u6548 --&gt;\n  &lt;custom-component style=\"color: blue\"&gt;\u4e0d\u662f\u84dd\u8272\u7684&lt;\/custom-component&gt;\n&lt;\/view&gt;\n<\/code><\/pre>\n<pre><code>&lt;!-- custom-component.wxml --&gt;\n&lt;view style=\"flex: 1\"&gt;\n  \u6ee1\u5bbd\u7684\n  &lt;slot&gt;&lt;\/slot&gt;\n&lt;\/view&gt;\n<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u8282\u70b9\u4e0a\u7684&nbsp;class&nbsp;style&nbsp;\u548c\u52a8\u753b\u5c06\u4e0d\u518d\u751f\u6548\uff0c\u4f46\u4ecd\u53ef\u4ee5\uff1a<\/p>\n<ul>\n<li>\u5c06 style \u5b9a\u4e49\u6210&nbsp;properties&nbsp;\u5c5e\u6027\u6765\u83b7\u53d6 style \u4e0a\u8bbe\u7f6e\u7684\u503c\uff1b<\/li>\n<li>\u5c06 class \u5b9a\u4e49\u6210&nbsp;externalClasses&nbsp;\u5916\u90e8\u6837\u5f0f\u7c7b\u4f7f\u5f97\u81ea\u5b9a\u4e49\u7ec4\u4ef6 wxml \u53ef\u4ee5\u4f7f\u7528 class \u503c\u3002<\/li>\n<\/ul>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ec4\u4ef6\u6a21\u677f\u548c\u6837\u5f0f \u7c7b\u4f3c\u4e8e\u9875\u9762\uff0c\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u62e5\u6709\u81ea\u5df1\u7684&nbsp;wxml&nbsp;\u6a21\u677f\u548c&nbsp;wxss&#038;n [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182398],"tags":[],"class_list":["post-185802","post","type-post","status-publish","format-standard","hentry","category-mini-program"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/185802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=185802"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/185802\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=185802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=185802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=185802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}