{"id":693,"date":"2023-05-10T21:50:01","date_gmt":"2023-05-10T13:50:01","guid":{"rendered":"http:\/\/gjweb.top\/?p=693"},"modified":"2023-05-10T21:50:03","modified_gmt":"2023-05-10T13:50:03","slug":"04-%e8%ae%a1%e7%ae%97%e5%b1%9e%e6%80%a7%e5%92%8c%e7%9b%91%e5%90%ac%e5%b1%9e%e6%80%a7","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=693","title":{"rendered":"04.\u8ba1\u7b97\u5c5e\u6027\u548c\u76d1\u542c\u5c5e\u6027"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"\u8ba1\u7b97\u5c5e\u6027\">\u8ba1\u7b97\u5c5e\u6027<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u65b9\u5f0f\u4e00\">\u65b9\u5f0f\u4e00<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script setup lang=\"ts\"&gt;<br>import {computed ,ref} from \"vue\"<br>const name = ref(\"\u4f60\u597d\")<br>const helloName = computed(()=&gt;name.value+\"vue3\"); \/\/ \u4f60\u597d vue3<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u65b9\u5f0f\u4e8c\">\u65b9\u5f0f\u4e8c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;<br> &nbsp;&lt;h1&gt;this is hello world&lt;\/h1&gt;<br> &nbsp;&lt;h2&gt;{{ newName}}&lt;\/h2&gt; <br> &nbsp;&lt;button @click=\"newName='vue3'\"&gt;click&lt;\/button&gt;<br>&lt;\/template&gt;<br>&lt;script setup lang=\"ts\"&gt;<br>import {computed ,ref} from \"vue\"<br>const name = ref(\"\u4f60\u597d\");<br>const newName = computed({<br> &nbsp;get(){<br> &nbsp; &nbsp;return name.value<br>  },<br> &nbsp;set(value:string){<br> &nbsp; &nbsp;name.value = \"hello\"+value \/\/ \u4f60\u597d vue3<br>  }<br>})<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"watch-\u76d1\u542c\">watch \u76d1\u542c<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u76d1\u542c\u57fa\u672c\u7c7b\u578b\">\u76d1\u542c\u57fa\u672c\u7c7b\u578b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script setup lang=\"ts\"&gt;<br>import {watch,ref} from \"vue\"<br>const name = ref(\"zhangsan\")<br>const age = ref(18)<br>watch(name,(newValue,oldvalue)=&gt;{})<br>\/\/\u540c\u65f6\u76d1\u542c\u591a\u4e2a\u53d8\u91cf<br>watch(&#91;name,age],(newVal,oldVal)=&gt;{})<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u76d1\u542c\u5bf9\u8c61\">\u76d1\u542c\u5bf9\u8c61<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;<br> &nbsp;&lt;h1&gt;this is hello world&lt;\/h1&gt;<br> &nbsp;&lt;h2&gt;{{ data.name }}&lt;\/h2&gt;<br> &nbsp;&lt;button @click=\"handleClick\"&gt;click&lt;\/button&gt;<br>&lt;\/template&gt;<br>&lt;script setup lang=\"ts\"&gt;<br>import {reactive ,watch} from \"vue\"<br>const data = reactive({<br> &nbsp;name:\"zhangsan\"<br>})<br>const handleClick = ()=&gt;{<br> &nbsp;data.name = \"lisi\"<br>}<br>watch(data,(newValue,oldValue)=&gt;{<br> &nbsp;console.log(newValue);<br> &nbsp;console.log(oldValue);<br>})<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5982\u679c\u4f7f\u7528 reactive \u76d1\u542c\u5bf9\u8c61\u65e0\u9700\u4f7f\u7528 deep \u6a21\u5f0f<\/li><li>watch \u76d1\u542c\u5bf9\u8c61 newValue \u548c oldValue \u7684\u503c\u90fd\u662f\u6700\u65b0\u7684<\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u5982\u679c\u76d1\u542c\u4e00\u4e2a\u5bf9\u8c61\u7684\u5177\u4f53\u5c5e\u6027,\u9700\u8981\u4f7f\u7528\u56de\u8c03\u51fd\u6570<\/p><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script setup lang=\"ts\"&gt;<br>import {reactive ,watch} from \"vue\"<br>const data = reactive({<br> &nbsp;name:\"zhangsan\"<br>})<br>const handleClick = ()=&gt;{<br> &nbsp;data.name = \"lisi\"<br>}<br>watch(()=&gt;data.name,(newValue,oldValue)=&gt;{})<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u6df1\u5ea6\u914d\u7f6e\">\u6df1\u5ea6\u914d\u7f6e<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>deep:true \u6df1\u5ea6\u76d1\u542c<\/li><li>flush:&#8217;pre&#8217; \/\/ pre\u66f4\u65b0\u524d\u8c03\u7528 sync \u540c\u6b65\u8c03\u7528 post \u66f4\u65b0\u540e\u8c03\u7528<\/li><li>immediate:true \/\/ \u7acb\u5373\u6267\u884c<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"watch\u9ad8\u7ea7\u4fa6\u542c\u5668\">watch\u9ad8\u7ea7\u4fa6\u542c\u5668<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u793a\u4f8b\">\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;<br> &nbsp;&lt;h1&gt;this is hello world&lt;\/h1&gt;<br> &nbsp;&lt;h2&gt;{{ name }}&lt;\/h2&gt;<br> &nbsp;&lt;button @click=\"handleClick\"&gt;click&lt;\/button&gt;<br> &nbsp;&lt;button @click=\"handleStop\"&gt;\u505c\u6b62\u76d1\u542c&lt;\/button&gt;<br>&lt;\/template&gt;<br>&lt;script setup lang=\"ts\"&gt;<br>import {watchEffect,ref} from \"vue\"<br>const name = ref(\"zhangsan\")<br>const handleClick = ()=&gt;{<br> &nbsp;name.value = \"lisi\"<br>}<br>const handleStop = ()=&gt;{<br> &nbsp;stopWatch()<br>}<br>const stopWatch = watchEffect((oninvalidate)=&gt;{<br> &nbsp;oninvalidate(()=&gt;console.log(\"\u66f4\u65b0\u524d\u8c03\u7528\"))<br> &nbsp;console.log(name.value);<br>})<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u6df1\u5ea6\u914d\u7f6e-\u7b2c\u4e8c\u4e2a\u53c2\u6570\">\u6df1\u5ea6\u914d\u7f6e [ \u7b2c\u4e8c\u4e2a\u53c2\u6570 ]<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script setup lang=\"ts\"&gt;<br>import {watchEffect,ref} from \"vue\"<br>watchEffect((oninvalidate)=&gt;{<br> &nbsp;oninvalidate(()=&gt;console.log(\"\u66f4\u65b0\u524d\u8c03\u7528\"))<br> &nbsp;console.log(name.value);<br>},{<br> &nbsp; &nbsp;flush:'pre' , \/\/ pre\u66f4\u65b0\u524d\u8c03\u7528 sync \u540c\u6b65\u8c03\u7528 post \u66f4\u65b0\u540e\u8c03\u7528<br> &nbsp; &nbsp;onTrigger(e){debugger;} \/\/ \u7528\u4e8e\u8c03\u8bd5\u7684 debugger \u63a7\u5236\u53f0<br>})<br>&lt;\/script&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8ba1\u7b97\u5c5e\u6027 \u65b9\u5f0f\u4e00 \u65b9\u5f0f\u4e8c watch \u76d1\u542c \u76d1\u542c\u57fa\u672c\u7c7b\u578b \u76d1\u542c\u5bf9\u8c61 \u5982\u679c\u4f7f\u7528 reactive \u76d1\u542c\u5bf9\u8c61\u65e0\u9700\u4f7f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-693","post","type-post","status-publish","format-standard","hentry","category-vue3"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/693","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=693"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/693\/revisions"}],"predecessor-version":[{"id":694,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/693\/revisions\/694"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}