{"id":67,"date":"2022-05-29T12:51:03","date_gmt":"2022-05-29T04:51:03","guid":{"rendered":"http:\/\/www.33weixin.com\/index\/?p=67"},"modified":"2022-05-29T12:51:43","modified_gmt":"2022-05-29T04:51:43","slug":"%e8%87%aa%e5%ae%9a%e4%b9%89%e6%8c%87%e4%bb%a4","status":"publish","type":"post","link":"http:\/\/www.33weixin.com\/index\/index.php\/2022\/05\/29\/%e8%87%aa%e5%ae%9a%e4%b9%89%e6%8c%87%e4%bb%a4\/","title":{"rendered":"\u81ea\u5b9a\u4e49\u6307\u4ee4"},"content":{"rendered":"<h2 id=\"\u7b80\u4ecb\">\u7b80\u4ecb<\/h2>\n<p>\u9664\u4e86\u9ed8\u8ba4\u8bbe\u7f6e\u7684\u6838\u5fc3\u6307\u4ee4(\u00a0<code>v-model<\/code>\u00a0\u548c\u00a0<code>v-show<\/code>\u00a0),Vue \u4e5f\u5141\u8bb8\u6ce8\u518c\u81ea\u5b9a\u4e49\u6307\u4ee4\u3002\u6ce8\u610f\uff0c\u5728 Vue2.0 \u91cc\u9762\uff0c\u4ee3\u7801\u590d\u7528\u7684\u4e3b\u8981\u5f62\u5f0f\u548c\u62bd\u8c61\u662f\u7ec4\u4ef6\u2014\u2014\u7136\u800c\uff0c\u6709\u7684\u60c5\u51b5\u4e0b,\u4f60\u4ecd\u7136\u9700\u8981\u5bf9\u7eaf DOM \u5143\u7d20\u8fdb\u884c\u5e95\u5c42\u64cd\u4f5c,\u8fd9\u65f6\u5019\u5c31\u4f1a\u7528\u5230\u81ea\u5b9a\u4e49\u6307\u4ee4\u3002\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u5c06\u805a\u7126\u4e00\u4e2a input \u5143\u7d20\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n<div id=\"simplest-directive-example\" class=\"demo\"><input type=\"text\" \/><\/div>\n<p>\u5f53\u9875\u9762\u52a0\u8f7d\u65f6\uff0c\u5143\u7d20\u5c06\u83b7\u5f97\u7126\u70b9\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4f60\u8bbf\u95ee\u540e\u8fd8\u6ca1\u70b9\u51fb\u4efb\u4f55\u5185\u5bb9\uff0cinput \u5c31\u83b7\u5f97\u4e86\u7126\u70b9\u3002\u73b0\u5728\u8ba9\u6211\u4eec\u5b8c\u5584\u8fd9\u4e2a\u6307\u4ee4\uff1a<\/p>\n<pre><code class=\"lang-js\"><span class=\"hljs-comment\">\/\/ \u6ce8\u518c\u4e00\u4e2a\u5168\u5c40\u81ea\u5b9a\u4e49\u6307\u4ee4 v-focus<\/span>\nVue.directive(<span class=\"hljs-string\">'focus'<\/span>, {\n  <span class=\"hljs-comment\">\/\/ \u5f53\u7ed1\u5b9a\u5143\u7d20\u63d2\u5165\u5230 DOM \u4e2d\u3002<\/span>\n  inserted: <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\">el<\/span>) <\/span>{\n    <span class=\"hljs-comment\">\/\/ \u805a\u7126\u5143\u7d20<\/span>\n    el.focus()\n  }\n})\n<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u6ce8\u518c\u5c40\u90e8\u6307\u4ee4\uff0c\u7ec4\u4ef6\u4e2d\u63a5\u53d7\u4e00\u4e2a\u00a0<code>directives<\/code>\u00a0\u7684\u9009\u9879\uff1a<\/p>\n<pre><code class=\"lang-js\">directives: {\n  focus: {\n    <span class=\"hljs-comment\">\/\/ \u6307\u4ee4\u7684\u5b9a\u4e49---<\/span>\n\n  }\n}\n<\/code><\/pre>\n<p>\u7136\u540e\u4f60\u53ef\u4ee5\u5728\u6a21\u677f\u4e2d\u4efb\u4f55\u5143\u7d20\u4e0a\u4f7f\u7528\u65b0\u7684\u00a0<code>v-focus<\/code>\u00a0\u5c5e\u6027\uff1a<\/p>\n<pre><code class=\"lang-html\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">v-focus<\/span>&gt;<\/span>\n<\/code><\/pre>\n<h2 id=\"\u94a9\u5b50\u51fd\u6570\">\u94a9\u5b50\u51fd\u6570<\/h2>\n<p>\u6307\u4ee4\u5b9a\u4e49\u51fd\u6570\u63d0\u4f9b\u4e86\u51e0\u4e2a\u94a9\u5b50\u51fd\u6570\uff08\u53ef\u9009\uff09\uff1a<\/p>\n<ul>\n<li><code>bind<\/code>: \u53ea\u8c03\u7528\u4e00\u6b21\uff0c\u6307\u4ee4\u7b2c\u4e00\u6b21\u7ed1\u5b9a\u5230\u5143\u7d20\u65f6\u8c03\u7528\uff0c\u7528\u8fd9\u4e2a\u94a9\u5b50\u51fd\u6570\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u5728\u7ed1\u5b9a\u65f6\u6267\u884c\u4e00\u6b21\u7684\u521d\u59cb\u5316\u52a8\u4f5c\u3002<\/li>\n<li><code>inserted<\/code>: \u88ab\u7ed1\u5b9a\u5143\u7d20\u63d2\u5165\u7236\u8282\u70b9\u65f6\u8c03\u7528\uff08\u7236\u8282\u70b9\u5b58\u5728\u5373\u53ef\u8c03\u7528\uff0c\u4e0d\u5fc5\u5b58\u5728\u4e8e document \u4e2d\uff09\u3002<\/li>\n<li><code>update<\/code>: \u88ab\u7ed1\u5b9a\u5143\u7d20\u6240\u5728\u7684\u6a21\u677f\u66f4\u65b0\u65f6\u8c03\u7528\uff0c\u800c\u4e0d\u8bba\u7ed1\u5b9a\u503c\u662f\u5426\u53d8\u5316\u3002\u901a\u8fc7\u6bd4\u8f83\u66f4\u65b0\u524d\u540e\u7684\u7ed1\u5b9a\u503c\uff0c\u53ef\u4ee5\u5ffd\u7565\u4e0d\u5fc5\u8981\u7684\u6a21\u677f\u66f4\u65b0\uff08\u8be6\u7ec6\u7684\u94a9\u5b50\u51fd\u6570\u53c2\u6570\u89c1\u4e0b\uff09\u3002<\/li>\n<li><code>componentUpdated<\/code>: \u88ab\u7ed1\u5b9a\u5143\u7d20\u6240\u5728\u6a21\u677f\u5b8c\u6210\u4e00\u6b21\u66f4\u65b0\u5468\u671f\u65f6\u8c03\u7528\u3002<\/li>\n<li><code>unbind<\/code>: \u53ea\u8c03\u7528\u4e00\u6b21\uff0c \u6307\u4ee4\u4e0e\u5143\u7d20\u89e3\u7ed1\u65f6\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u94a9\u5b50\u51fd\u6570\u7684\u53c2\u6570 (\u5305\u62ec\u00a0<code>el<\/code>\uff0c<code>binding<\/code>\uff0c<code>vnode<\/code>\uff0c<code>oldVnode<\/code>) \u3002<\/p>\n<h2 id=\"\u94a9\u5b50\u51fd\u6570\u53c2\u6570\">\u94a9\u5b50\u51fd\u6570\u53c2\u6570<\/h2>\n<p>\u94a9\u5b50\u51fd\u6570\u88ab\u8d4b\u4e88\u4e86\u4ee5\u4e0b\u53c2\u6570\uff1a<\/p>\n<ul>\n<li><strong>el<\/strong>: \u6307\u4ee4\u6240\u7ed1\u5b9a\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u7528\u6765\u76f4\u63a5\u64cd\u4f5c DOM \u3002<\/li>\n<li><strong>binding<\/strong>: \u4e00\u4e2a\u5bf9\u8c61\uff0c\u5305\u542b\u4ee5\u4e0b\u5c5e\u6027\uff1a\n<ul>\n<li><strong>name<\/strong>: \u6307\u4ee4\u540d\uff0c\u4e0d\u5305\u62ec\u00a0<code>v-<\/code>\u00a0\u524d\u7f00\u3002<\/li>\n<li><strong>value<\/strong>: \u6307\u4ee4\u7684\u7ed1\u5b9a\u503c\uff0c \u4f8b\u5982\uff1a\u00a0<code>v-my-directive=\"1 + 1\"<\/code>, value \u7684\u503c\u662f\u00a0<code>2<\/code>\u3002<\/li>\n<li><strong>oldValue<\/strong>: \u6307\u4ee4\u7ed1\u5b9a\u7684\u524d\u4e00\u4e2a\u503c\uff0c\u4ec5\u5728\u00a0<code>update<\/code>\u00a0\u548c\u00a0<code>componentUpdated<\/code>\u00a0\u94a9\u5b50\u4e2d\u53ef\u7528\u3002\u65e0\u8bba\u503c\u662f\u5426\u6539\u53d8\u90fd\u53ef\u7528\u3002<\/li>\n<li><strong>expression<\/strong>: \u7ed1\u5b9a\u503c\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002 \u4f8b\u5982\u00a0<code>v-my-directive=\"1 + 1\"<\/code>\u00a0\uff0c expression \u7684\u503c\u662f\u00a0<code>\"1 + 1\"<\/code>\u3002<\/li>\n<li><strong>arg<\/strong>: \u4f20\u7ed9\u6307\u4ee4\u7684\u53c2\u6570\u3002\u4f8b\u5982\u00a0<code>v-my-directive:foo<\/code>\uff0c arg \u7684\u503c\u662f\u00a0<code>\"foo\"<\/code>\u3002<\/li>\n<li><strong>modifiers<\/strong>: \u4e00\u4e2a\u5305\u542b\u4fee\u9970\u7b26\u7684\u5bf9\u8c61\u3002 \u4f8b\u5982\uff1a\u00a0<code>v-my-directive.foo.bar<\/code>, \u4fee\u9970\u7b26\u5bf9\u8c61 modifiers \u7684\u503c\u662f\u00a0<code>{ foo: true, bar: true }<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>vnode<\/strong>: Vue \u7f16\u8bd1\u751f\u6210\u7684\u865a\u62df\u8282\u70b9\uff0c\u67e5\u9605\u00a0<a href=\"http:\/\/caibaojian.com\/vue\/api#VNode%E6%8E%A5%E5%8F%A3\">VNode API<\/a>\u00a0\u4e86\u89e3\u66f4\u591a\u8be6\u60c5\u3002<\/li>\n<li><strong>oldVnode<\/strong>: \u4e0a\u4e00\u4e2a\u865a\u62df\u8282\u70b9\uff0c\u4ec5\u5728\u00a0<code>update<\/code>\u00a0\u548c\u00a0<code>componentUpdated<\/code>\u00a0\u94a9\u5b50\u4e2d\u53ef\u7528\u3002<\/li>\n<\/ul>\n<p class=\"tip\">\u9664\u4e86 `el` \u4e4b\u5916\uff0c\u5176\u5b83\u53c2\u6570\u90fd\u5e94\u8be5\u662f\u53ea\u8bfb\u7684\uff0c\u5c3d\u91cf\u4e0d\u8981\u4fee\u6539\u4ed6\u4eec\u3002\u5982\u679c\u9700\u8981\u5728\u94a9\u5b50\u4e4b\u95f4\u5171\u4eab\u6570\u636e\uff0c\u5efa\u8bae\u901a\u8fc7\u5143\u7d20\u7684 [dataset](https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/HTMLElement\/dataset) \u6765\u8fdb\u884c\u3002<\/p>\n<p>\u4e00\u4e2a\u4f7f\u7528\u4e86\u8fd9\u4e9b\u53c2\u6570\u7684\u81ea\u5b9a\u4e49\u94a9\u5b50\u6837\u4f8b\uff1a<\/p>\n<pre><code class=\"lang-html\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"hook-arguments-example\"<\/span> <span class=\"hljs-attr\">v-demo:hello.a.b<\/span>=<span class=\"hljs-string\">\"message\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/code><\/pre>\n<pre><code class=\"lang-js\">Vue.directive(<span class=\"hljs-string\">'demo'<\/span>, {\n  bind: <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\">el, binding, vnode<\/span>) <\/span>{\n    <span class=\"hljs-keyword\">var<\/span> s = <span class=\"hljs-built_in\">JSON<\/span>.stringify\n    el.innerHTML =\n      <span class=\"hljs-string\">'name: '<\/span>       + s(binding.name) + <span class=\"hljs-string\">'&lt;br&gt;'<\/span> +\n      <span class=\"hljs-string\">'value: '<\/span>      + s(binding.value) + <span class=\"hljs-string\">'&lt;br&gt;'<\/span> +\n      <span class=\"hljs-string\">'expression: '<\/span> + s(binding.expression) + <span class=\"hljs-string\">'&lt;br&gt;'<\/span> +\n      <span class=\"hljs-string\">'argument: '<\/span>   + s(binding.arg) + <span class=\"hljs-string\">'&lt;br&gt;'<\/span> +\n      <span class=\"hljs-string\">'modifiers: '<\/span>  + s(binding.modifiers) + <span class=\"hljs-string\">'&lt;br&gt;'<\/span> +\n      <span class=\"hljs-string\">'vnode keys: '<\/span> + <span class=\"hljs-built_in\">Object<\/span>.keys(vnode).join(<span class=\"hljs-string\">', '<\/span>)\n  }\n})\n\n<span class=\"hljs-keyword\">new<\/span> Vue({\n  el: <span class=\"hljs-string\">'#hook-arguments-example'<\/span>,\n  data: {\n    message: <span class=\"hljs-string\">'hello!'<\/span>\n  }\n})\n<\/code><\/pre>\n<div id=\"hook-arguments-example\" class=\"demo\">name: &#8220;demo&#8221;<br \/>\nvalue: &#8220;hello!&#8221;<br \/>\nexpression: &#8220;message&#8221;<br \/>\nargument: &#8220;hello&#8221;<br \/>\nmodifiers: {&#8220;a&#8221;:true,&#8221;b&#8221;:true}<br \/>\nvnode keys: tag, data, children, text, elm, ns, context, functionalContext, key, componentOptions, child, parent, raw, isStatic, isRootInsert, isComment, isCloned, isOnce<\/div>\n<h2 id=\"\u51fd\u6570\u7b80\u5199\">\u51fd\u6570\u7b80\u5199<\/h2>\n<p>\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u60f3\u5728\u00a0<code>bind<\/code>\u00a0\u548c\u00a0<code>update<\/code>\u00a0\u94a9\u5b50\u4e0a\u505a\u91cd\u590d\u52a8\u4f5c\uff0c\u5e76\u4e14\u4e0d\u60f3\u5173\u5fc3\u5176\u5b83\u7684\u94a9\u5b50\u51fd\u6570\u3002\u53ef\u4ee5\u8fd9\u6837\u5199:<\/p>\n<pre><code class=\"lang-js\">Vue.directive(<span class=\"hljs-string\">'color-swatch'<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\">el, binding<\/span>) <\/span>{\n  el.style.backgroundColor = binding.value\n})\n<\/code><\/pre>\n<h2 id=\"\u5bf9\u8c61\u5b57\u9762\u91cf\">\u5bf9\u8c61\u5b57\u9762\u91cf<\/h2>\n<p>\u5982\u679c\u6307\u4ee4\u9700\u8981\u591a\u4e2a\u503c\uff0c\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a JavaScript \u5bf9\u8c61\u5b57\u9762\u91cf\u3002\u8bb0\u4f4f\uff0c\u6307\u4ee4\u51fd\u6570\u80fd\u591f\u63a5\u53d7\u6240\u6709\u5408\u6cd5\u7c7b\u578b\u7684 Javascript \u8868\u8fbe\u5f0f\u3002<\/p>\n<pre><code class=\"lang-html\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">v-demo<\/span>=<span class=\"hljs-string\">\"{ color: 'white', text: 'hello!' }\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/code><\/pre>\n<pre><code class=\"lang-js\">Vue.directive(<span class=\"hljs-string\">'demo'<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\">el, binding<\/span>) <\/span>{\n  <span class=\"hljs-built_in\">console<\/span>.log(binding.value.color) <span class=\"hljs-comment\">\/\/ =&gt; \"white\"<\/span>\n  <span class=\"hljs-built_in\">console<\/span>.log(binding.value.text)  <span class=\"hljs-comment\">\/\/ =&gt; \"hello!\"<\/span>\n})\n<\/code><\/pre>\n<hr \/>\n<blockquote><p>\u539f\u6587\uff1a<a href=\"http:\/\/vuejs.org\/guide\/custom-directive.html\" target=\"_blank\" rel=\"noopener\">http:\/\/vuejs.org\/guide\/custom-directive.html<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb \u9664\u4e86\u9ed8\u8ba4\u8bbe\u7f6e\u7684\u6838\u5fc3\u6307\u4ee4(\u00a0v-model\u00a0\u548c\u00a0v-show\u00a0),Vue \u4e5f\u5141\u8bb8\u6ce8\u518c\u81ea\u5b9a\u4e49\u6307\u4ee4\u3002\u6ce8\u610f\uff0c\u5728 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/www.33weixin.com\/index\/index.php\/wp-json\/wp\/v2\/posts\/67"}],"collection":[{"href":"http:\/\/www.33weixin.com\/index\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.33weixin.com\/index\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.33weixin.com\/index\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.33weixin.com\/index\/index.php\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":1,"href":"http:\/\/www.33weixin.com\/index\/index.php\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":68,"href":"http:\/\/www.33weixin.com\/index\/index.php\/wp-json\/wp\/v2\/posts\/67\/revisions\/68"}],"wp:attachment":[{"href":"http:\/\/www.33weixin.com\/index\/index.php\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.33weixin.com\/index\/index.php\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.33weixin.com\/index\/index.php\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}