{"id":5977,"date":"2024-04-17T23:26:13","date_gmt":"2024-04-17T15:26:13","guid":{"rendered":"http:\/\/zyg.luchers.cn\/?p=5977"},"modified":"2024-04-17T23:26:13","modified_gmt":"2024-04-17T15:26:13","slug":"%e4%b8%89%e3%80%81%e5%85%b6%e5%ae%83-composition-api","status":"publish","type":"post","link":"http:\/\/zyg.luchers.cn\/index.php\/2024\/04\/17\/%e4%b8%89%e3%80%81%e5%85%b6%e5%ae%83-composition-api\/","title":{"rendered":"\u4e09\u3001\u5176\u5b83 Composition API"},"content":{"rendered":"<h1>\u4e09\u3001\u5176\u5b83 Composition API<\/h1>\n<h2>1.shallowReactive \u4e0e shallowRef<\/h2>\n<ul>\n<li>\n<p>shallowReactive\uff1a\u53ea\u5904\u7406\u5bf9\u8c61\u6700\u5916\u5c42\u5c5e\u6027\u7684\u54cd\u5e94\u5f0f\uff08\u6d45\u54cd\u5e94\u5f0f\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>shallowRef\uff1a\u53ea\u5904\u7406\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u54cd\u5e94\u5f0f, \u4e0d\u8fdb\u884c\u5bf9\u8c61\u7684\u54cd\u5e94\u5f0f\u5904\u7406\u3002<\/p>\n<\/li>\n<li>\n<p>\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528?<\/p>\n<ul>\n<li>\u5982\u679c\u6709\u4e00\u4e2a\u5bf9\u8c61\u6570\u636e\uff0c\u7ed3\u6784\u6bd4\u8f83\u6df1, \u4f46\u53d8\u5316\u65f6\u53ea\u662f\u5916\u5c42\u5c5e\u6027\u53d8\u5316 ===&gt; shallowReactive\u3002<\/li>\n<li>\u5982\u679c\u6709\u4e00\u4e2a\u5bf9\u8c61\u6570\u636e\uff0c\u540e\u7eed\u529f\u80fd\u4e0d\u4f1a\u4fee\u6539\u8be5\u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\uff0c\u800c\u662f\u751f\u65b0\u7684\u5bf9\u8c61\u6765\u66ff\u6362 ===&gt; shallowRef\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>2.readonly \u4e0e shallowReadonly<\/h2>\n<ul>\n<li>readonly: \u8ba9\u4e00\u4e2a\u54cd\u5e94\u5f0f\u6570\u636e\u53d8\u4e3a\u53ea\u8bfb\u7684\uff08\u6df1\u53ea\u8bfb\uff09\u3002<\/li>\n<li>shallowReadonly\uff1a\u8ba9\u4e00\u4e2a\u54cd\u5e94\u5f0f\u6570\u636e\u53d8\u4e3a\u53ea\u8bfb\u7684\uff08\u6d45\u53ea\u8bfb\uff09\u3002<\/li>\n<li>\u5e94\u7528\u573a\u666f: \u4e0d\u5e0c\u671b\u6570\u636e\u88ab\u4fee\u6539\u65f6\u3002<\/li>\n<\/ul>\n<h2>3.toRaw \u4e0e markRaw<\/h2>\n<ul>\n<li>toRaw\uff1a\n<ul>\n<li>\u4f5c\u7528\uff1a\u5c06\u4e00\u4e2a\u7531<code><code>reactive<\/code><\/code>\u751f\u6210\u7684<strong style=\"color:orange\">\u54cd\u5e94\u5f0f\u5bf9\u8c61<\/strong>\u8f6c\u4e3a<strong style=\"color:orange\">\u666e\u901a\u5bf9\u8c61<\/strong>\u3002<\/li>\n<li>\u4f7f\u7528\u573a\u666f\uff1a\u7528\u4e8e\u8bfb\u53d6\u54cd\u5e94\u5f0f\u5bf9\u8c61\u5bf9\u5e94\u7684\u666e\u901a\u5bf9\u8c61\uff0c\u5bf9\u8fd9\u4e2a\u666e\u901a\u5bf9\u8c61\u7684\u6240\u6709\u64cd\u4f5c\uff0c\u4e0d\u4f1a\u5f15\u8d77\u9875\u9762\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<\/li>\n<li>markRaw\uff1a\n<ul>\n<li>\u4f5c\u7528\uff1a\u6807\u8bb0\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f7f\u5176\u6c38\u8fdc\u4e0d\u4f1a\u518d\u6210\u4e3a\u54cd\u5e94\u5f0f\u5bf9\u8c61\u3002<\/li>\n<li>\u5e94\u7528\u573a\u666f:\n<ol>\n<li>\u6709\u4e9b\u503c\u4e0d\u5e94\u88ab\u8bbe\u7f6e\u4e3a\u54cd\u5e94\u5f0f\u7684\uff0c\u4f8b\u5982\u590d\u6742\u7684\u7b2c\u4e09\u65b9\u7c7b\u5e93\u7b49\u3002<\/li>\n<li>\u5f53\u6e32\u67d3\u5177\u6709\u4e0d\u53ef\u53d8\u6570\u636e\u6e90\u7684\u5927\u5217\u8868\u65f6\uff0c\u8df3\u8fc7\u54cd\u5e94\u5f0f\u8f6c\u6362\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>4.customRef<\/h2>\n<ul>\n<li>\n<p>\u4f5c\u7528\uff1a\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 ref\uff0c\u5e76\u5bf9\u5176\u4f9d\u8d56\u9879\u8ddf\u8e2a\u548c\u66f4\u65b0\u89e6\u53d1\u8fdb\u884c\u663e\u5f0f\u63a7\u5236\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u73b0\u9632\u6296\u6548\u679c\uff1a<\/p>\n<pre><code class=\"language-vue\"><template>\n<input type=\"text\" v-model=\"keyword\">\n<h3>{{keyword}}<\/h3>\n<\/template>\n\n<script>\nimport {ref,customRef} from 'vue'\nexport default {\n    name:'Demo',\n    setup(){\n        \/\/ let keyword = ref('hello') \/\/\u4f7f\u7528Vue\u51c6\u5907\u597d\u7684\u5185\u7f6eref\n        \/\/\u81ea\u5b9a\u4e49\u4e00\u4e2amyRef\n        function myRef(value,delay){\n            let timer\n            \/\/\u901a\u8fc7customRef\u53bb\u5b9e\u73b0\u81ea\u5b9a\u4e49\n            return customRef((track,trigger)=>{\n                return{\n                    get(){\n                        track() \/\/\u544a\u8bc9Vue\u8fd9\u4e2avalue\u503c\u662f\u9700\u8981\u88ab\u201c\u8ffd\u8e2a\u201d\u7684\n                        return value\n                    },\n                    set(newValue){\n                        clearTimeout(timer)\n                        timer = setTimeout(()=>{\n                            value = newValue\n                            trigger() \/\/\u544a\u8bc9Vue\u53bb\u66f4\u65b0\u754c\u9762\n                        },delay)\n                    }\n                }\n            })\n        }\n        let keyword = myRef('hello',500) \/\/\u4f7f\u7528\u7a0b\u5e8f\u5458\u81ea\u5b9a\u4e49\u7684ref\n        return {\n            keyword\n        }\n    }\n}\n<\/script><\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>5.provide \u4e0e inject<\/h2>\n<ul>\n<li>\n<p>\u4f5c\u7528\uff1a\u5b9e\u73b0<strong style=\"color:#DD5145\">\u7956\u4e0e\u540e\u4ee3\u7ec4\u4ef6\u95f4<\/strong>\u901a\u4fe1<\/p>\n<\/li>\n<li>\n<p>\u5957\u8def\uff1a\u7236\u7ec4\u4ef6\u6709\u4e00\u4e2a <code>provide<\/code> \u9009\u9879\u6765\u63d0\u4f9b\u6570\u636e\uff0c\u540e\u4ee3\u7ec4\u4ef6\u6709\u4e00\u4e2a <code>inject<\/code> \u9009\u9879\u6765\u5f00\u59cb\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u5177\u4f53\u5199\u6cd5\uff1a<\/p>\n<ol>\n<li>\n<p>\u7956\u7ec4\u4ef6\u4e2d\uff1a<\/p>\n<pre><code class=\"language-js\">setup(){\n......\n let car = reactive({name:'\u5954\u9a70',price:'40\u4e07'})\n provide('car',car)\n ......\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>\u540e\u4ee3\u7ec4\u4ef6\u4e2d\uff1a<\/p>\n<pre><code class=\"language-js\">setup(props,context){\n......\n const car = inject('car')\n return {car}\n......\n}<\/code><\/pre>\n<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h2>6.\u54cd\u5e94\u5f0f\u6570\u636e\u7684\u5224\u65ad<\/h2>\n<ul>\n<li>isRef: \u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u4e3a\u4e00\u4e2a ref \u5bf9\u8c61<\/li>\n<li>isReactive: \u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u7531 <code>reactive<\/code> \u521b\u5efa\u7684\u54cd\u5e94\u5f0f\u4ee3\u7406<\/li>\n<li>isReadonly: \u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u7531 <code>readonly<\/code> \u521b\u5efa\u7684\u53ea\u8bfb\u4ee3\u7406<\/li>\n<li>isProxy: \u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u7531 <code>reactive<\/code> \u6216\u8005 <code>readonly<\/code> \u65b9\u6cd5\u521b\u5efa\u7684\u4ee3\u7406<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e09\u3001\u5176\u5b83 Composition API 1.shallowReactive \u4e0e shallowRef sha [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-5977","post","type-post","status-publish","format-standard","hentry","category-vue3"],"_links":{"self":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/comments?post=5977"}],"version-history":[{"count":1,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5977\/revisions"}],"predecessor-version":[{"id":5978,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5977\/revisions\/5978"}],"wp:attachment":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/media?parent=5977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/categories?post=5977"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/tags?post=5977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}