{"id":5707,"date":"2024-09-04T18:01:01","date_gmt":"2024-09-04T10:01:01","guid":{"rendered":""},"modified":"2024-09-04T18:01:01","modified_gmt":"2024-09-04T10:01:01","slug":"2021vue\u9762\u8bd5\u9898_Vue\u9762\u8bd5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/5707.html","title":{"rendered":"2021vue\u9762\u8bd5\u9898_Vue\u9762\u8bd5"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

vue\u91cd\u7f6edata<\/h2>\n
Object.assign\uff08\uff09\u65b9\u6cd5\u7528\u4e8e\u5c06\u6240\u6709\u53ef\u679a\u4e3e\u5c5e\u6027\u7684\u503c\u4ece\u4e00\u4e2a\u6216\u591a\u4e2a\u6e90\u5bf9\u8c61\u590d\u5236\u5230\u76ee\u6807\u5bf9\u8c61 this.$data\u83b7\u53d6\u5f53\u524d\u72b6\u6001\u4e0b\u7684data this.$options.data<\/span>(<\/span>)<\/span>\u83b7\u53d6\u8be5\u7ec4\u4ef6\u521d\u59cb\u72b6\u6001\u4e0b\u7684data Object.assign<\/span>(<\/span>this.$data,<\/span>this.$options.data<\/span>(<\/span>)<\/span>)<\/span> <\/code><\/pre>\n

vue\u539f\u7406<\/h2>\n
vue \u53cc\u5411\u6570\u636e\u7ed1\u5b9a\u662f\u901a\u8fc7\u6570\u636e\u52ab\u6301\u7ed3\u5408\u53d1\u5e03\u8ba2\u9605\u6a21\u5f0f\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u7684\uff0c \u4e5f\u5c31\u662f\u8bf4\u6570\u636e\u548c\u89c6\u56fe\u540c\u6b65\uff0c\u6570\u636e\u53d1\u751f\u53d8\u5316\uff0c\u89c6\u56fe\u8ddf\u7740\u53d8\u5316\uff0c\u89c6\u56fe\u53d8\u5316\uff0c\u6570\u636e\u4e5f\u968f\u4e4b\u53d1\u751f\u6539\u53d8 <\/code><\/pre>\n

js\u7684\u53cc\u5411\u6570\u636e\u7ed1\u5b9a<\/h2>\n
<div id=\"app\"<\/span>> <input type=\"text\"<\/span> id=\"ipt\"<\/span>> <h3 id=\"txt\"<\/span>><\/h3> <\/div> <script> \/\/\u6a21\u62df\u6570\u636e\u6a21\u5757 var msg=\"hello world\"<\/span>;<\/span> \/\/\u5efa\u7acb\u5bf9\u8c61 var obj=<\/span>{ \n   <\/span>}<\/span>; Object.defineProperty(obj,'value',<\/span>{ \n   <\/span> \/\/\u8bbe\u7f6eget\u3001set\u8bbf\u95ee\u5668 get()<\/span>{ \n   <\/span> return this._value;<\/span> }<\/span>, set(val)<\/span>{ \n   <\/span> this._value=val;<\/span> ipt.value=val;<\/span> txt.innerText=val;<\/span> }<\/span> }<\/span>)<\/span>;<\/span> obj.value=msg;<\/span> \/\/\u76d1\u542c\u6587\u672c\u6846\u8f93\u5165 ipt.addEventListener(\"input\",function()<\/span>{ \n   <\/span> obj.value=this.value;<\/span> console.log<\/span>(<\/span>obj.value)<\/span>;<\/span> }<\/span>)<\/span> <\/script> <\/code><\/pre>\n

\u52a8\u6001\u8def\u7531<\/h2>\n
1\u3001\u5c06\u8def\u7531\u5206\u4e3a\u9759\u6001\u8def\u7531(<\/span>staticRouters)<\/span>\u3001\u52a8\u6001\u8def\u7531 2\u3001\u9759\u6001\u8def\u7531\u521d\u59cb\u5316\u65f6\u6b63\u5e38\u52a0\u8f7d 3\u3001\u7528\u6237\u767b\u9646\u540e\uff0c\u83b7\u53d6\u76f8\u5173\u52a8\u6001\u8def\u7531\u6570\u636e\uff0c 4\u3001\u7136\u540e\u5229\u7528vue\uff1aaddRoute\u8ffd\u52a0\u5230vue\u5b9e\u4f8b\u4e2d\u5373\u53ef\u3002 <\/code><\/pre>\n

\u8def\u7531\u89e3\u8026<\/h2>\n
1\u3001\u53ea\u6709\u52a8\u6001\u8def\u7531\u624d\u80fd\u7528\u8def\u7531\u89e3\u8026 export default<\/span> { \n   <\/span> methods:<\/span> { \n   <\/span> getParamsId()<\/span> { \n   <\/span> return this.$route.params.id }<\/span> }<\/span> }<\/span> 2\u3001\u5728\u5b9a\u4e49\u8def\u7531\u7684\u65f6\u5019\u901a\u8fc7\/:\u5c5e\u6027\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u4f20\u9012\u53c2\u6570\u7684\u5c5e\u6027 3\u3001\u9700\u8981\u5728\u5b9a\u4e49\u8def\u7531\u7684\u65f6\u5019\u8bbe\u7f6e\u5c5e\u6027props:true const router = new VueRouter(<\/span>{ \n   <\/span> routes: [<\/span>{ \n   <\/span> path<\/span>:<\/span> '\/user\/:id'<\/span>,<\/span> component<\/span>:<\/span> User,<\/span> props<\/span>:<\/span> true }<\/span>] }<\/span>) 4\u3001\u5728\u8fdb\u884c\u8def\u7531\u8df3\u8f6c\u7684\u65f6\u5019\u901a\u8fc7\/\u503c\u5f97\u65b9\u5f0f\u8fdb\u884c\u4f20\u9012\u6570\u636e 5\u3001\u5728\u9700\u8981\u63a5\u6536\u53c2\u6570\u7684\u7ec4\u4ef6\u4e2d\u901a\u8fc7props\u8fdb\u884c\u63a5\u6536\u5373\u53ef export default<\/span> { \n   <\/span> props: ['id'], methods:<\/span> { \n   <\/span> getParamsId()<\/span> { \n   <\/span> return this.id }<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

eventBus\u7f3a\u70b9<\/h2>\n
1\uff0c\u51fa\u73b0\u5f88\u591a\u4e2a\u7ec4\u4ef6\u4f20\u9012\u65f6\uff0c\u6ca1\u6cd5\u627e\u90fd\u5728\u54ea\u91cc\u5b9a\u4e49 2\uff0c\u751f\u547d\u5468\u671f\u7684\u95ee\u9898\uff08EventBus\u4e8b\u4ef6\u672a\u5728\u5b9e\u4f8b\u9500\u6bc1\u7684\u65f6\u5019\uff0c\u6ce8\u9500\u76d1\u542c\u4e8b\u4ef6\uff0c\u5bfc\u81f4\u89e6\u53d1\u4e0a\u4e00\u6b21\u76d1\u542c\u4e8b\u4ef6\uff0c\u9057\u7559\u6570\u636e\u4e5f\u662f\u4e0a\u4e00\u6b21\u4e8b\u4ef6\u4e2d\u7684\uff09 <\/code><\/pre>\n

$nextTick\u4f18\u70b9<\/h2>\n
\u5e38\u7528\u7684\u573a\u666f\u662f\u5728\u8fdb\u884c\u83b7\u53d6\u6570\u636e\u540e\uff0c\u9700\u8981\u5bf9\u65b0\u89c6\u56fe\u8fdb\u884c\u4e0b\u4e00\u6b65\u64cd\u4f5c\u6216\u8005\u5176\u4ed6\u64cd\u4f5c\u65f6\uff0c\u53d1\u73b0\u83b7\u53d6\u4e0d\u5230dom <\/code><\/pre>\n

vue\u4e2dnextTick()\u539f\u7406<\/h2>\n
\u7531\u4e8eVue DOM\u66f4\u65b0\u662f\u5f02\u6b65\u6267\u884c\u7684\uff0c\u5373\u4fee\u6539\u6570\u636e\u65f6\uff0c\u89c6\u56fe\u4e0d\u4f1a\u7acb\u5373\u66f4\u65b0\uff0c\u53ea\u8981\u76d1\u542c\u6570\u636e\u53d8\u5316\uff0cVue \u5c06\u5f00\u542f\u4e00\u4e2a\u961f\u5217\uff0c\u5e76\u7f13\u51b2\u5728\u540c\u4e00\u4e8b\u4ef6\u5faa\u73af\u4e2d\u53d1\u751f\u7684\u6240\u6709\u6570\u636e\u53d8\u66f4\uff0c\u5728\u7f13\u51b2\u65f6\u4f1a\u53bb\u9664\u91cd\u590d\u6570\u636e\uff0c\u4ece\u800c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u548cDOM\u64cd\u4f5c\u3002 \u7136\u540e\uff0c\u5728\u4e0b\u4e00\u4e2a\u7684\u4e8b\u4ef6\u5faa\u73af\u201ctick\u201d\u4e2d\uff0cVue \u5237\u65b0\u961f\u5217\u5e76\u6267\u884c\u5b9e\u9645 (<\/span>\u5df2\u53bb\u91cd\u7684)<\/span> \u5de5\u4f5c\u3002 \u7b49\u540c\u4e00\u6570\u636e\u5faa\u73af\u4e2d\u7684\u6240\u6709\u6570\u636e\u53d8\u5316\u5b8c\u6210\u4e4b\u540e\uff0c\u518d\u7edf\u4e00\u8fdb\u884c\u89c6\u56fe\u66f4\u65b0\u3002 \u4e3a\u4e86\u786e\u4fdd\u5f97\u5230\u66f4\u65b0\u540e\u7684DOM\uff0c\u6240\u4ee5\u8bbe\u7f6e\u4e86 Vue.nextTick<\/span>(<\/span>)<\/span>\u65b9\u6cd5\u3002 nextTick\u4e3b\u8981\u4f7f\u7528\u4e86\u5b8f\u4efb\u52a1\u548c\u5fae\u4efb\u52a1 Vue \u5728\u5185\u90e8\u5bf9\u5f02\u6b65\u961f\u5217\u5c1d\u8bd5\u4f7f\u7528\u539f\u751f\u7684 Promise.then\u3001MutationObserver \u548c setImmediate\uff0c\u5982\u679c\u6267\u884c\u73af\u5883\u4e0d\u652f\u6301\uff0c\u5219\u4f1a\u91c7\u7528 setTimeout<\/span>(<\/span>fn,<\/span> 0)<\/span>\u4ee3\u66ff\u3002 <\/code><\/pre>\n

vue\u4e2d11\u4e2a\u751f\u547d\u5468\u671f<\/h2>\n
beforeCreate<\/span>:<\/span>vue \u5b9e\u4f8b\u8fd8\u6ca1\u6709\u88ab\u5b8c\u5168\u521b\u5efa\u51fa\u6765\uff0c\u6b64\u65f6 data\uff0cmethods \u7b49\u5185\u90e8\u6ca1\u6709\u521d\u59cb\u5316\uff0c\u65e0\u6cd5\u83b7\u53d6\u54cd\u5e94\u6570\u636e\u3002 created<\/span>:<\/span>vue \u5b9e\u4f8b\u5df2\u7ecf\u521d\u59cb\u5316\u4e86\uff0c\u53ef\u4ee5\u5728\u8fd9\u91cc\u8c03\u7528\u6570\u636e\uff0c\u4e0d\u8fc7\u8fd8\u6ca1\u6e32\u67d3\u5230\u9875\u9762\u4e0a beforeMount<\/span>:<\/span>vue \u5df2\u7ecf\u5c06\u6a21\u677f\u5b57\u7b26\u4e32\u7f16\u8bd1\u6210\u5185\u5b58\u865a\u62dfDOM\uff0c\u6a21\u677f\u5df2\u7ecf\u7f16\u8bd1\u5b8c\u6210\uff0c\u5df2\u7ecf\u5b8c\u6210\u6e32\u67d3\u6811\uff0c\u8fd8\u6ca1\u6709\u6e32\u67d3\u5230\u9875\u9762\u4e0a mounted<\/span>:<\/span>\u6ce8\u610f mounted \u4e0d\u4f1a\u4fdd\u8bc1\u6240\u6709\u7684\u5b50\u7ec4\u4ef6\u4e5f\u90fd\u4e00\u8d77\u88ab\u6302\u8f7d\u3002\u5982\u679c\u4f60\u5e0c\u671b\u7b49\u5230\u6574\u4e2a\u89c6\u56fe\u90fd\u6e32\u67d3\u5b8c\u6bd5\uff0c\u53ef\u4ee5\u5728 mounted \u5185\u90e8\u4f7f\u7528 vm.$nextTick\uff1a beforeUpdate<\/span>:<\/span>\u5185\u5b58\u4e2d\u7684\u6570\u636e\u5df2\u7ecf\u6539\u53d8\uff0c\u9875\u9762\u4e0a\u7684\u8fd8\u6ca1\u66f4\u65b0 updated<\/span>:<\/span>\u9875\u9762\u4e0a\u6570\u636e\u548c\u5185\u5b58\u4e2d\u7684\u4e00\u81f4 activated\uff1a\u88ab keep-alive \u7f13\u5b58\u7684\u7ec4\u4ef6\u6fc0\u6d3b\u65f6\u8c03\u7528\u3002 deactivated\uff1a\u88ab keep-alive \u7f13\u5b58\u7684\u7ec4\u4ef6\u505c\u7528\u65f6\u8c03\u7528\u3002 beforeDestroy\uff1a\u5b9e\u4f8b\u9500\u6bc1\u4e4b\u524d\u8c03\u7528\u3002\u5728\u8fd9\u4e00\u6b65\uff0c\u5b9e\u4f8b\u4ecd\u7136\u5b8c\u5168\u53ef\u7528\u3002 destroyed\uff1a\u5b9e\u4f8b\u9500\u6bc1\u540e\u8c03\u7528\u3002\u8be5\u94a9\u5b50\u88ab\u8c03\u7528\u540e\uff0c\u5bf9\u5e94 Vue \u5b9e\u4f8b\u7684\u6240\u6709\u6307\u4ee4\u90fd\u88ab\u89e3\u7ed1\uff0c\u6240\u6709\u7684\u4e8b\u4ef6\u76d1\u542c\u5668\u88ab\u79fb\u9664\uff0c\u6240\u6709\u7684\u5b50\u5b9e\u4f8b\u4e5f\u90fd\u88ab\u9500\u6bc1\u3002 errorCaptured\uff1a\u5f53\u6355\u83b7\u4e00\u4e2a\u6765\u81ea\u5b50\u5b59\u7ec4\u4ef6\u7684\u9519\u8bef\u65f6\u88ab\u8c03\u7528\u3002\u6b64\u94a9\u5b50\u4f1a\u6536\u5230\u4e09\u4e2a\u53c2\u6570\uff1a\u9519\u8bef\u5bf9\u8c61\u3001\u53d1\u751f\u9519\u8bef\u7684\u7ec4\u4ef6\u5b9e\u4f8b\u4ee5\u53ca\u4e00\u4e2a\u5305\u542b\u9519\u8bef\u6765\u6e90\u4fe1\u606f\u7684\u5b57\u7b26\u4e32\u3002\u6b64\u94a9\u5b50\u53ef\u4ee5\u8fd4\u56de false \u4ee5\u963b\u6b62\u8be5\u9519\u8bef\u7ee7\u7eed\u5411\u4e0a\u4f20\u64ad\u3002 <\/code><\/pre>\n

vuex<\/h2>\n
VueX \u662f\u4e00\u4e2a\u4e13\u95e8\u4e3a Vue.js \u5e94\u7528\u8bbe\u8ba1\u7684\u72b6\u6001\u7ba1\u7406\u67b6\u6784\uff0c\u7edf\u4e00\u7ba1\u7406\u548c\u7ef4\u62a4\u5404\u4e2avue\u7ec4\u4ef6\u7684\u53ef\u53d8\u5316\u72b6\u6001(<\/span>\u4f60\u53ef\u4ee5\u7406\u89e3\u6210 vue \u7ec4\u4ef6\u91cc\u7684\u67d0\u4e9b data )<\/span>\u3002 Vuex\u6709\u4e94\u4e2a\u6838\u5fc3\u6982\u5ff5\uff0cstate,<\/span> getters,<\/span> mutations,<\/span> actions,<\/span> modules\u3002 state => \u57fa\u672c\u6570\u636e getters => \u4ece\u57fa\u672c\u6570\u636e\u6d3e\u751f\u7684\u6570\u636e mutations => \u63d0\u4ea4\u66f4\u6539\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u540c\u6b65\uff01 actions => \u50cf\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u5305\u88f9mutations\uff0c\u4f7f\u4e4b\u53ef\u4ee5\u5f02\u6b65\u3002 modules => \u6a21\u5757\u5316Vuex \u8f85\u52a9\u51fd\u6570\uff1amapState\u3001mapGetters\u3001mapMutations\u3001mapActions createStore \u6a21\u5757\u5316\uff1anamespece,<\/span>this.store.state.\u6a21\u5757\u540d\u79f0.\u5c5e\u6027 \u5176\u4e2dstate\u548cgetters\u7528\u6765\u4fdd\u5b58\u72b6\u6001\uff1bmutations\u548cactions\u7528\u6765\u6539\u53d8\u72b6\u6001\uff1b\u76d1\u542c\u72b6\u6001\u7528\u7684\u662fVue\u7ec4\u4ef6\u4e2d\u7684computed\u5c5e\u6027\uff1bmodule\u662f\u7528\u6765\u7ec4\u7ec7\u6574\u4e2a\u5e94\u7528\u7684\u72b6\u6001\u7ba1\u7406\u4ee3\u7801\uff0c\u4f7f\u72b6\u6001\u5212\u5206\u6a21\u5757\uff0c\u66f4\u6613\u4e8e\u7ba1\u7406\uff1b\u8f85\u52a9\u51fd\u6570\u7528\u6765\u5728\u76d1\u542c\u72b6\u6001\u65f6\u5019\u7b80\u5316\u4ee3\u7801\uff0ccreateStore\u5219\u7528\u6765\u521b\u5efa\u72b6\u6001\u7ba1\u7406\u5bf9\u8c61\u3002 mutations\u548cactions\u6709\u533a\u522b\uff0cmutations\u4e0d\u5e94\u8be5\u7528\u4e8e\u5f02\u6b65\u4fee\u6539\u72b6\u6001\uff0c\u56e0\u4e3a\u8fd9\u6837\u505a\u7684\u8bdd\uff0cVuex\u662f\u65e0\u6cd5\u77e5\u9053\u4fee\u6539state.count\u7684\u65f6\u673a\u7684\uff0c\u56e0\u4e3a\u5b83\u662f\u5728\u5f02\u6b65\u56de\u8c03\u91cc\u9762\u6307\u5b9a\u7684\uff0c\u56e0\u6b64Vuex\u65e0\u6cd5\u5728\u8c03\u8bd5\u5de5\u5177\u4e2d\u6253\u5370\u51fa\u6211\u4eec\u5b9e\u9645\u6539\u53d8state\u7684\u64cd\u4f5c\u3002 \u800cactions\u4e2d\u53ef\u4ee5\u5f02\u6b65\u66f4\u65b0\u72b6\u6001\u3002 getters\u548cstate\u7684\u5173\u7cfb\u7c7b\u4f3c\u4e8eVue\u7ec4\u4ef6data\u5c5e\u6027\u548ccomputed\u5c5e\u6027\u7684\u5173\u7cfb\uff0cgetters\u6839\u636estate\u6216\u8005\u5176\u4ed6getters\u8ba1\u7b97\u51fa\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u5f53\u5176\u4f9d\u8d56\u7684\u6570\u636e\u53d8\u5316\u65f6\u5019\uff0c\u5b83\u4e5f\u4f1a\u5b9e\u65f6\u66f4\u65b0\u3002 <\/code><\/pre>\n

vue\u4e2d\u7ec4\u4ef6\u901a\u4fe1<\/h2>\n
\u4e00\u3001props \/ $emit \uff08\u6700\u5e38\u7528\u7684\u7ec4\u5efa\u901a\u4fe1\u65b9\u5f0f\uff09 \u4e8c\u3001$children \/ $parent \u4e09\u3001provide\/ inject \u56db\u3001ref \/ refs \u4e94\u3001eventBus \u516d\u3001Vuex \u4e03\u3001$attrs\u4e0e $listeners \u516b\u3001localStorage \/ sessionStorage <\/code><\/pre>\n

vue\u7ec4\u4ef6data\u7528\u7bad\u5934\u51fd\u6570<\/h2>\n
\u53ef\u4ee5\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff0c\u4f46\u662f\u9700\u8981\u6ce8\u610fthis\u6307\u5411\u3002 \u5982\u679c\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff0cdata\u51fd\u6570\u4e2d\u7684this\u4e0d\u4f1a\u6307\u5411vue\u5b9e\u4f8b\uff0c\u5982\u679c\u9700\u8981\u8bbf\u95eevue\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7data\u51fd\u6570\u7684\u53c2\u6570\u6765\u5b9e\u73b0\u3002 data: vm => (<\/span>{ \n   <\/span> a<\/span>:<\/span> vm.myProp }<\/span>)<\/span> <\/code><\/pre>\n

vue\u7236\u5b50\u7ec4\u4ef6\u6302\u8f7d\u987a\u5e8f<\/h2>\n
\u52a0\u8f7d\u6e32\u67d3\u8fc7\u7a0b \u7236 beforeCreate -> \u7236 created -> \u7236 beforeMount -> \u5b50 beforeCreate -> \u5b50 created ->\u5b50 beforeMount -> \u5b50 mounted -> \u7236 mounted \u66f4\u65b0\u8fc7\u7a0b \u7236 beforeUpdate -> \u5b50 beforeUpdate -> \u5b50 updated -> \u7236 updated \u9500\u6bc1\u8fc7\u7a0b \u7236 beforeDestroy -> \u5b50 beforeDestroy -> \u5b50 destroyed -> \u7236 destroyed <\/code><\/pre>\n

route\u548crouter\u7684\u533a\u522b<\/h2>\n

route\u662f\u8def\u7531\u4fe1\u606f\u5bf9\u8c61\uff0c\u6bcf\u4e00\u4e2a\u8def\u7531\u90fd\u4f1a\u6709\u4e00\u4e2aroute\u5bf9\u8c61\uff0c\u662f\u4e00\u4e2a\u5c40\u90e8\u7684\u5bf9\u8c61,\u91cc\u9762\u4e3b\u8981\u5305\u542b\u8def\u7531\u7684\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f\uff0c\u6bd4\u5982name\u3001meta\u3001path\u3001hash\u3001query\u3001params\u3001fullPath\u3001matched\u3001redirectedFrom\u2026
\"2021vue\u9762\u8bd5\u9898_Vue\u9762\u8bd5
router\u662fVueRouter\u7684\u5b9e\u4f8b\uff0c\u901a\u8fc7Vue.use(VueRouter)\u548cVueRouter\u6784\u9020\u51fd\u6570\u5f97\u5230\u4e00\u4e2arouter\u7684\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u4e2d\u662f\u4e00\u4e2a\u5168\u5c40\u7684\u5bf9\u8c61\uff0c\u4ed6\u5305\u542b\u4e86\u6240\u6709\u7684\u8def\u7531\u5305\u542b\u4e86\u8bb8\u591a\u5173\u952e\u7684\u5bf9\u8c61\u548c\u5c5e\u6027<\/p>\n

    \n
  1. $router.go()<\/li>\n
  2. $router.push()<\/li>\n
  3. $router.replace()<\/li>\n<\/ol>\n

    Query\u548cparams\u7684\u533a\u522b<\/h2>\n
    this.$router.push(<\/span>{ \n   <\/span>path:\"\u5730\u5740\",query:<\/span>{ \n   <\/span>id<\/span>:<\/span>\"123\"<\/span>}<\/span>}<\/span>)<\/span>;<\/span> \u8fd9\u662f\u4f20\u9012\u53c2\u6570 this.$route.query.id\uff1b \u8fd9\u662f\u63a5\u53d7\u53c2\u6570 this.$router.push(<\/span>{ \n   <\/span>name:\"\u5730\u5740\",params:<\/span>{ \n   <\/span>id<\/span>:<\/span>\"123\"<\/span>}<\/span>}<\/span>)<\/span>;<\/span> \u8fd9\u662f\u4f20\u9012\u53c2\u6570 this.$route.params.id;<\/span> \u8fd9\u662f\u63a5\u53d7\u53c2\u6570 \u4e24\u8005\u4e2dquery\u5728\u5237\u65b0\u9875\u9762\u7684\u65f6\u5019\u53c2\u6570\u4e0d\u4f1a\u6d88\u5931 \u4f46params\u5728\u5237\u65b0\u9875\u9762\u7684\u65f6\u5019\u53c2\u6570\u4f1a\u6d88\u5931 \u53ef\u4ee5\u8003\u8651\u672c\u5730\u5b58\u50a8\u89e3\u51b3\u6b64\u95ee\u9898 query\u4f20\u8fc7\u6765\u7684\u53c2\u6570\u4f1a\u663e\u793a\u5230\u5730\u5740\u680f\u4e2d \u800cparams\u4f20\u8fc7\u6765\u7684\u53c2\u6570\u4e0d\u4f1a\u663e\u793a\u5230\u5730\u5740\u680f\u4e2d \u76f4\u767d\u7684\u6765\u8bf4 query\u76f8\u5f53\u4e8eget\u8bf7\u6c42\uff0c\u800cparams\u76f8\u5f53\u4e8epost\u8bf7\u6c42 <\/code><\/pre>\n

    v-if\u548cv-show\u7684\u533a\u522b<\/h2>\n
    \u884c\u4e3a\u4e0d\u540c\uff1av-if\u6307\u4ee4\u5728\u6ee1\u8db3\u6761\u4ef6\u65f6\u5019\u624d\u4f1a\u6e32\u67d3DOM\uff0cv-show\u4e00\u5f00\u59cb\u5c31\u6e32\u67d3DOM\uff0c\u6ee1\u8db3\u6761\u4ef6\u65f6\u5019\u624d\u8bbe\u7f6eCSS\u7684display\u5c5e\u6027\u8ba9\u5143\u7d20\u663e\u793a\u51fa\u6765\u3002 \u5e94\u7528\u573a\u666f\u4e0d\u540c\uff1a\u4e00\u822c\u6765\u8bf4\uff0cv-if \u6709\u66f4\u9ad8\u7684\u5207\u6362\u5f00\u9500\uff0c\u800c v-show \u6709\u66f4\u9ad8\u7684\u521d\u59cb\u6e32\u67d3\u5f00\u9500\u3002\u56e0\u6b64\uff0c\u5982\u679c\u9700\u8981\u975e\u5e38\u9891\u7e41\u5730\u5207\u6362\uff0c\u5219\u4f7f\u7528 v-show \u8f83\u597d\uff1b\u5982\u679c\u5728\u8fd0\u884c\u65f6\u6761\u4ef6\u5f88\u5c11\u6539\u53d8\uff0c\u5219\u4f7f\u7528 v-if \u8f83\u597d\u3002 <\/code><\/pre>\n

    v-for\u548cv-if\u540c\u65f6\u4f7f\u7528\u6709\u95ee\u9898<\/h2>\n
    \u6c38\u8fdc\u4e0d\u8981\u628a v-<\/span>if<\/span> \u548c v-<\/span>for<\/span> \u540c\u65f6\u7528\u5728\u540c\u4e00\u4e2a\u5143\u7d20\u4e0a\uff0c\u5e26\u6765\u6027\u80fd\u65b9\u9762\u7684\u6d6a\u8d39\uff08\u6bcf\u6b21\u6e32\u67d3\u90fd\u4f1a\u5148\u5faa\u73af\u518d\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\uff09 Vue3<\/span>\u4fee\u6539\u4e86v-<\/span>if<\/span>\u548cv-<\/span>for<\/span>\u7684\u4f18\u5148\u7ea7\uff0cv-<\/span>if<\/span>\u6ca1\u6709\u6743\u9650\u8bbf\u95eev-<\/span>for<\/span>\u7684\u53d8\u91cf\uff0c\u8fd9\u4e2a\u9700\u8981\u6ce8\u610f\u3002 <\/code><\/pre>\n

    \u865a\u62dfdom\u7684\u5b9e\u73b0\u539f\u7406\u4e0e\u4f18\u7f3a\u70b9<\/h2>\n
    \u865a\u62dfDOM\u672c\u8d28\u4e0a\u662fJavaScript<\/span>\u5bf9\u8c61,<\/span>\u662f\u5bf9\u771f\u5b9eDOM\u7684\u62bd\u8c61 \u72b6\u6001\u53d8\u66f4\u65f6\uff0c\u8bb0\u5f55\u65b0\u6811\u548c\u65e7\u6811\u7684\u5dee\u5f02\uff0c\u6700\u540e\u628a\u5dee\u5f02\u66f4\u65b0\u5230\u771f\u6b63\u7684dom\u4e2d.<\/span> \u865a\u62dfDOM\u7684\u4f5c\u7528\uff1a\u4f7f\u7528\u539f\u751fjs\u6216\u8005jquery\u5199\u9875\u9762\u7684\u65f6\u5019\u4f1a\u53d1\u73b0\u64cd\u4f5cDOM\u662f\u4e00\u4ef6\u975e\u5e38\u9ebb\u70e6\u7684\u4e8b\u60c5\uff0c\u5f80\u5f80\u662fDOM\u6807\u7b7e\u548cjs\u903b\u8f91\u540c\u65f6\u5199\u5728js\u6587\u4ef6\u91cc\uff0c\u6570\u636e\u4ea4\u4e92\u65f6\u4e0d\u65f6\u8fd8\u8981\u5199\u5f88\u591a\u7684input\u9690\u85cf\u57df\uff0c\u5982\u679c\u6ca1\u6709\u597d\u7684\u4ee3\u7801\u89c4\u8303\u7684\u8bdd\u4f1a\u663e\u5f97\u4ee3\u7801\u975e\u5e38\u5197\u4f59\u6df7\u4e71\uff0c\u8026\u5408\u6027\u9ad8\u5e76\u4e14\u96be\u4ee5\u7ef4\u62a4\u3002 \u53e6\u5916\u4e00\u65b9\u9762\u5728\u6d4f\u89c8\u5668\u91cc\u4e00\u904d\u53c8\u4e00\u904d\u7684\u6e32\u67d3DOM\u662f\u975e\u5e38\u975e\u5e38\u6d88\u8017\u6027\u80fd\u7684\uff0c\u5e38\u5e38\u4f1a\u51fa\u73b0\u9875\u9762\u5361\u6b7b\u7684\u60c5\u51b5\uff1b\u6240\u4ee5\u5c3d\u91cf\u51cf\u5c11\u5bf9DOM\u7684\u64cd\u4f5c\u6210\u4e3a\u4e86\u4f18\u5316\u524d\u7aef\u6027\u80fd\u7684\u5fc5\u8981\u624b\u6bb5\uff0cvdom\u5c31\u662f\u5c06DOM\u7684\u5bf9\u6bd4\u653e\u5728\u4e86js\u5c42\uff0c\u901a\u8fc7\u5bf9\u6bd4\u4e0d\u540c\u4e4b\u5904\u6765\u9009\u62e9\u65b0\u6e32\u67d3DOM\u8282\u70b9\uff0c\u4ece\u800c\u63d0\u9ad8\u6e32\u67d3\u6548\u7387\u3002 <\/code><\/pre>\n
    \u4f18\u70b9\uff1a \u4fdd\u8bc1\u6027\u80fd\u4e0b\u9650:<\/span> \u865a\u62dfDOM\u53ef\u4ee5\u7ecf\u8fc7diff\u627e\u51fa\u6700\u5c0f\u5dee\u5f02,<\/span>\u7136\u540e\u6279\u91cf\u8fdb\u884cpatch,<\/span>\u8fd9\u79cd\u64cd\u4f5c\u867d\u7136\u6bd4\u4e0d\u4e0a\u624b\u52a8\u4f18\u5316,<\/span>\u4f46\u662f\u6bd4\u8d77\u7c97\u66b4\u7684DOM\u64cd\u4f5c\u6027\u80fd\u8981\u597d\u5f88\u591a,<\/span>\u56e0\u6b64\u865a\u62dfDOM\u53ef\u4ee5\u4fdd\u8bc1\u6027\u80fd\u4e0b\u9650 \u65e0\u9700\u624b\u52a8\u64cd\u4f5cDOM:<\/span> \u865a\u62dfDOM\u7684diff\u548cpatch\u90fd\u662f\u5728\u4e00\u6b21\u66f4\u65b0\u4e2d\u81ea\u52a8\u8fdb\u884c\u7684,<\/span>\u6211\u4eec\u65e0\u9700\u624b\u52a8\u64cd\u4f5cDOM,<\/span>\u6781\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387 \u8de8\u5e73\u53f0:<\/span> \u865a\u62dfDOM\u672c\u8d28\u4e0a\u662fJavaScript<\/span>\u5bf9\u8c61,<\/span>\u800cDOM\u4e0e\u5e73\u53f0\u5f3a\u76f8\u5173,<\/span>\u76f8\u6bd4\u4e4b\u4e0b\u865a\u62dfDOM\u53ef\u4ee5\u8fdb\u884c\u66f4\u65b9\u4fbf\u5730\u8de8\u5e73\u53f0\u64cd\u4f5c,<\/span>\u4f8b\u5982\u670d\u52a1\u5668\u6e32\u67d3\u3001\u79fb\u52a8\u7aef\u5f00\u53d1\u7b49\u7b49 \u7f3a\u70b9\uff1a \u65e0\u6cd5\u8fdb\u884c\u6781\u81f4\u4f18\u5316:<\/span> \u5728\u4e00\u4e9b\u6027\u80fd\u8981\u6c42\u6781\u9ad8\u7684\u5e94\u7528\u4e2d\u865a\u62dfDOM\u65e0\u6cd5\u8fdb\u884c\u9488\u5bf9\u6027\u7684\u6781\u81f4\u4f18\u5316\uff0c\u6bd4\u5982VScode<\/span>\u91c7\u7528\u76f4\u63a5\u624b\u52a8\u64cd\u4f5cDOM\u7684\u65b9\u5f0f\u8fdb\u884c\u6781\u7aef\u7684\u6027\u80fd\u4f18 <\/code><\/pre>\n

    keep-alive\u5b9e\u73b0\u539f\u7406<\/h2>\n
    \u4ec0\u4e48\u662fkeep-<\/span>alive\uff1a keep-<\/span>alive\u662fVue<\/span>\u7684\u5185\u7f6e\u7ec4\u4ef6\uff0c\u5f53\u5b83\u5305\u88f9\u52a8\u6001\u7ec4\u4ef6\u65f6\uff0c\u4f1a\u7f13\u5b58\u4e0d\u6d3b\u52a8\u7684\u7ec4\u4ef6\u5b9e\u4f8b\uff0c\u800c\u4e0d\u662f\u9500\u6bc1\u5b83\u4eec\u3002 keep-<\/span>alive\u662f\u4e00\u4e2a\u62bd\u8c61\u7ec4\u4ef6\uff0c\u5b83\u81ea\u8eab\u4e0d\u4f1a\u6e32\u67d3\u4e00\u4e2aDOM\u5143\u7d20\uff0c\u4e5f\u4e0d\u4f1a\u51fa\u73b0\u5728\u7236\u7ec4\u4ef6\u4e2d\u3002 \u4f5c\u7528 \u5728\u7ec4\u4ef6\u5207\u6362\u8fc7\u7a0b\u4e2d \u628a\u5207\u6362\u51fa\u53bb\u7684\u7ec4\u4ef6\u4fdd\u7559\u5728\u5185\u5b58\u4e2d\uff0c\u9632\u6b62\u91cd\u590d\u6e32\u67d3DOM\uff0c\u51cf\u5c11\u52a0\u8f7d\u65f6\u95f4\u53ca\u6027\u80fd\u6d88\u8017\uff0c\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u6027 \u539f\u7406\uff1a \u5728 created\u94a9\u5b50\u51fd\u6570\u8c03\u7528\u65f6\u5c06\u9700\u8981\u7f13\u5b58\u7684 VNode<\/span> \u8282\u70b9\u4fdd\u5b58\u5728 this<\/span>.<\/span>cache \u4e2d\uff0f\u5728 render\uff08\u9875\u9762\u6e32\u67d3\uff09 \u65f6\uff0c\u5982\u679c VNode<\/span> \u7684 name \u7b26\u5408\u7f13\u5b58\u6761\u4ef6\uff08\u53ef\u4ee5\u7528 include \u4ee5\u53ca exclude \u63a7\u5236\uff09\uff0c\u5219\u4f1a\u4ece this<\/span>.<\/span>cache \u4e2d\u53d6\u51fa\u4e4b\u524d\u7f13\u5b58\u7684 VNode<\/span>\u5b9e\u4f8b\u8fdb\u884c\u6e32\u67d3\u3002 <\/code><\/pre>\n

    computed\u548cwatch\u7684\u533a\u522b<\/h2>\n

    \u5e94\u7528\u573a\u666f\u4e0d\u540c<\/p>\n

    computed\u7528\u5728\u6839\u636edata\u5c5e\u6027\u6216\u8005\u5176\u4ed6computed\u8ba1\u7b97\u5f97\u5230\u4e00\u4e2a\u65b0\u503c\u7684\u60c5\u51b5\uff0ccomputed\u7684\u503c\u4e00\u822c\u88ab\u7528\u5728\u6e32\u67d3\u4e2d\u3002 watch\u7528\u5728\u76d1\u542c\u6570\u636e\u53d8\u5316\uff0c\u7136\u540e\u505a\u4e00\u4e9b\u6709\u526f\u4f5c\u7528\u7684\u64cd\u4f5c\u7684\u573a\u666f\u3002 \u6267\u884c\u8fc7\u7a0b\u4e0d\u540c \u5728\u4f9d\u8d56\u7684data\u5c5e\u6027\u53d8\u5316\u540e\uff0ccomputed\u5e76\u4e0d\u4f1a\u91cd\u65b0\u8ba1\u7b97\u65b0\u7684\u503c\uff0c\u800c\u662f\u7b49\u5230\u8bbf\u95ee\u7684\u65f6\u5019\u518d\u5224\u65ad\uff0c\u5982\u679c\u4f9d\u8d56\u7684data\u6709\u6539\u52a8\u5219\u91cd\u65b0\u8ba1\u7b97\u5e76\u8fd4\u56de\u7ed3\u679c\uff0c\u5982\u679c\u4f9d\u8d56\u7684data\u6ca1\u6709\u6539\u52a8\uff0c\u5c31\u4e0d\u8ba1\u7b97\uff0c\u76f4\u63a5\u8fd4\u56de\u5f53\u524d\u7ed3\u679c\u3002 \u4f9d\u8d56\u7684\u6570\u636e\u53d8\u5316\u540e\u5c31\u4f1a\u6267\u884cwatch\u7684\u56de\u8c03\u3002 <\/code><\/pre>\n

    vue-router\u539f\u7406\u4ee5\u53ca\u4e24\u79cd\u6a21\u5f0f\u533a\u522b<\/h2>\n
    \u524d\u7aef\u8def\u7531\u6709\u4e24\u79cd\u6a21\u5f0f\uff0cHTML5\u548chash\uff0c\u8fd9\u4e24\u79cd\u6a21\u5f0f\u672c\u8d28\u662f\u4e0d\u540c\u7684\u5e95\u5c42\u6d4f\u89c8\u5668\u6280\u672f\uff0c\u4f46\u662f\u4e0a\u5c42Vue<\/span> Router<\/span>\u505a\u4e86\u7edf\u4e00\u5316\u7684\u5c01\u88c5\uff0c\u56e0\u6b64\u5728\u6211\u4eec\u5f00\u53d1\u7ec4\u4ef6\u548c\u914d\u7f6e\u8def\u7531\u65f6\u5019\u4f7f\u7528\u8fd9\u4e24\u79cd\u6a21\u5f0f\u7684\u533a\u522b\u5e76\u4e0d\u5927\u3002\u9ed8\u8ba4\u662fhash\u6a21\u5f0f\u3002 \u8fd9\u4e24\u79cd\u6a21\u5f0f\u6709\u51e0\u4e2a\u4e3b\u8981\u533a\u522b 1.<\/span> HTML5\u6a21\u5f0f\u7684\u8def\u7531\u6ca1\u6709\"#\"<\/span><\/span>\u5b57\u7b26\uff0c\u800c\u662f\u5728\u57df\u540d\u540e\u76f4\u63a5\u5199\u8def\u5f84\uff0c\u66f4\u52a0\u4f18\u96c5 2.<\/span> \u7531\u4e8e#\u540e\u9762\u7684\u5b57\u7b26\u4e0d\u4f1a\u53d1\u7ed9\u670d\u52a1\u5668\uff0c\u56e0\u6b64hash\u8def\u7531SEO\u6bd4\u8f83\u5dee 3.<\/span> HTML5\u9700\u8981\u670d\u52a1\u5668\u5728\u8bbf\u95ee\u4e0d\u540c\u7684\u8def\u5f84\u65f6\u5019\u90fd\u80fdfallback\u5230index.<\/span>html\uff0c\u56e0\u6b64\u76f8\u5bf9\u9ebb\u70e6\n\n\u524d\u7aef\u8def\u7531\u7684\u539f\u7406\u5173\u952e\u67092<\/span>\u70b9 1.<\/span> \u53ef\u4ee5\u4fee\u6539url\uff0c\u4f46\u4e0d\u4f1a\u5f15\u8d77\u5237\u65b0\uff0c\u4ece\u800c\u5728\u4e0d\u5237\u65b0\u7684\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u8df3\u8f6c\u8def\u7531\u3002 2.<\/span> \u76d1\u542curl\u6539\u53d8\uff0c\u6839\u636eurl\u6e32\u67d3\u5bf9\u5e94\u7ec4\u4ef6\u3002 hash\u6a21\u5f0f\u548chistory\u6a21\u5f0f\u7684\u539f\u7406\u90fd\u662f\u57fa\u4e8e\u8fd9\u4e24\u70b9\u3002hash\u662f\u901a\u8fc7\u6d4f\u89c8\u5668\u63d0\u4f9b\u7684location API\u4fee\u6539url\uff0c\u901a\u8fc7onhashchange\u65b9\u6cd5\u76d1\u542chash\u6539\u53d8\uff1bhistory\u901a\u8fc7\u6d4f\u89c8\u5668\u63d0\u4f9b\u7684history.<\/span>pushState\u6216\u8005history.<\/span>replacestate\u4fee\u6539url\uff0c\u901a\u8fc7popState\u4e8b\u4ef6\u76d1\u542curl\u6539\u53d8\u3002 <\/code><\/pre>\n

    Vue\u5982\u4f55\u7ed9\u4e00\u4e2a\u5bf9\u8c61\u6dfb\u52a0\u65b0\u7684\u5c5e\u6027<\/h2>\n

    Vue.set(object, propertyName, value)
    vm.$set((object, propertyName, value)<\/p>\n

    vue\u7684compile\u8fc7\u7a0b<\/h2>\n

    Vue\u5728\u5b9e\u4f8b\u5316\u7ec4\u4ef6\u65f6\u5019\u4f1a\u751f\u6210\u865a\u62dfDOM\uff0cVue\u5148\u5224\u65ad\u662f\u5426\u6709render\u51fd\u6570\uff0c\u5982\u679c\u6709\u7684\u8bdd\u8c03\u7528render\u751f\u6210\u865a\u62dfDOM\uff1b\u5982\u679c\u6ca1\u6709render\u51fd\u6570\uff0c\u5219\u83b7\u53d6template\u9009\u9879\uff0ctemplate\u9009\u9879\u53ef\u4ee5\u662f\u9009\u62e9\u5668\u3001\u6a21\u7248\u5b57\u7b26\u4e32\u3001dom\u5143\u7d20\uff0cVue\u6839\u636etemplate\u9009\u9879\u8fdb\u884c\u6a21\u677f\u7f16\u8bd1\uff1b\u5982\u679c\u6ca1\u6709template\uff0c\u5219\u83b7\u53d6el\u4ee5\u53ca\u5176\u5b50\u5185\u5bb9\u4f5c\u4e3a\u6a21\u7248\u3002<\/p>\n

    \u6a21\u677f\u7f16\u8bd1\u6709\u4e09\u4e2a\u6b65\u9aa4<\/p>\n

      \n
    1. \u5c06\u6a21\u677f\u89e3\u6790\u4e3aAST\u3002\uff08Abstract Syntax Tree\uff0c\u62bd\u8c61\u8bed\u6cd5\u6811\uff09\u3002<\/li>\n
    2. \u904d\u5386AST\u6807\u8bb0\u9759\u6001\u8282\u70b9\u3002<\/li>\n
    3. \u4f7f\u7528AST\u751f\u6210\u6e32\u67d3\u51fd\u6570\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"2021vue\u9762\u8bd5\u9898_Vue\u9762\u8bd52023Vue\u9762\u8bd5\u9898_vuedata\u7bad\u5934\u51fd\u6570","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5707"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=5707"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5707\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=5707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=5707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=5707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}