{"id":404,"date":"2022-04-19T16:08:00","date_gmt":"2022-04-19T08:08:00","guid":{"rendered":"http:\/\/gjweb.top\/?p=404"},"modified":"2022-04-19T16:08:01","modified_gmt":"2022-04-19T08:08:01","slug":"07-provideinject-%e6%a8%a1%e6%9d%bfref-%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=404","title":{"rendered":"07.provide,Inject \u6a21\u677fref \u7684\u7528\u6cd5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"provide-inject\">provide,Inject<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>const app = Vue.createApp({<br> &nbsp; &nbsp;setup(){<br> &nbsp; &nbsp; &nbsp;const {provide,ref,readonly} = Vue;<br> &nbsp; &nbsp; &nbsp;const name = ref(\"hello\");<br> &nbsp; &nbsp; &nbsp;provide(\"name\",readonly(name));<br> &nbsp; &nbsp; &nbsp;const handleClick = (newValue)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;name.value = newValue<br> &nbsp; &nbsp;  };<br> &nbsp; &nbsp; &nbsp;provide(\"changeName\",handleClick)<br> &nbsp; &nbsp; &nbsp;return {}<br> &nbsp;  },<br> &nbsp; &nbsp;template:`<br> &nbsp; &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;child&gt;&lt;\/child&gt;<br> &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp;`<br>  })<br> &nbsp;app.component(\"child\",{<br> &nbsp; &nbsp;setup(){<br> &nbsp; &nbsp; &nbsp;const {inject} = Vue;<br> &nbsp; &nbsp; &nbsp;const name = inject(\"name\");<br> &nbsp; &nbsp; &nbsp;const changeName = inject(\"changeName\")<br> &nbsp; &nbsp; &nbsp;const handleClick = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;changeName(\"hello world\")<br> &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;return{<br> &nbsp; &nbsp; &nbsp; &nbsp;name,<br> &nbsp; &nbsp; &nbsp; &nbsp;handleClick<br> &nbsp; &nbsp;  }<br> &nbsp;  },<br> &nbsp; &nbsp;template:`<br> &nbsp; &nbsp; &nbsp;&lt;div @click=\"handleClick\"&gt;{{name}}&lt;\/div&gt;<br> &nbsp; &nbsp;`<br>  })<br> &nbsp;const vm = app.mount('#root');<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>provide,Inject<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-404","post","type-post","status-publish","format-standard","hentry","category-composition"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=404"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/404\/revisions"}],"predecessor-version":[{"id":405,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/404\/revisions\/405"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}