{"id":100,"date":"2018-12-12T11:10:02","date_gmt":"2018-12-12T03:10:02","guid":{"rendered":"http:\/\/adeqing.com\/?p=100"},"modified":"2018-12-12T11:14:07","modified_gmt":"2018-12-12T03:14:07","slug":"vue","status":"publish","type":"post","link":"https:\/\/adeqing.com\/index.php\/2018\/12\/12\/develop\/vue\/100\/","title":{"rendered":"vue \u6570\u7ec4,\u5bf9\u8c61\u66f4\u65b0\u68c0\u6d4b"},"content":{"rendered":"<h3 id=\"\u6570\u7ec4\u66f4\u65b0\u68c0\u6d4b\"><a class=\"headerlink\" title=\"\u6570\u7ec4\u66f4\u65b0\u68c0\u6d4b\" href=\"https:\/\/cn.vuejs.org\/v2\/guide\/list.html#%E6%95%B0%E7%BB%84%E6%9B%B4%E6%96%B0%E6%A3%80%E6%B5%8B\" data-scroll=\"\">\u6570\u7ec4\u66f4\u65b0\u68c0\u6d4b<\/a><\/h3>\n<p>\u7531\u4e8e JavaScript \u7684\u9650\u5236\uff0cVue \u4e0d\u80fd\u68c0\u6d4b\u4ee5\u4e0b\u53d8\u52a8\u7684\u6570\u7ec4\uff1a<\/p>\n<ol>\n<li>\u5f53\u4f60\u5229\u7528\u7d22\u5f15\u76f4\u63a5\u8bbe\u7f6e\u4e00\u4e2a\u9879\u65f6\uff0c\u4f8b\u5982\uff1a<code>vm.items[indexOfItem] = newValue<\/code><\/li>\n<li>\u5f53\u4f60\u4fee\u6539\u6570\u7ec4\u7684\u957f\u5ea6\u65f6\uff0c\u4f8b\u5982\uff1a<code>vm.items.length = newLength<\/code><\/li>\n<\/ol>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<figure class=\"highlight js\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"keyword\">var<\/span> vm = <span class=\"keyword\">new<\/span> Vue({<\/span>\r\n<span class=\"line\">  data: {<\/span>\r\n<span class=\"line\">    items: [<span class=\"string\">'a'<\/span>, <span class=\"string\">'b'<\/span>, <span class=\"string\">'c'<\/span>]<\/span>\r\n<span class=\"line\">  }<\/span>\r\n<span class=\"line\">})<\/span>\r\n<span class=\"line\">vm.items[<span class=\"number\">1<\/span>] = <span class=\"string\">'x'<\/span> <span class=\"comment\">\/\/ \u4e0d\u662f\u54cd\u5e94\u6027\u7684<\/span><\/span>\r\n<span class=\"line\">vm.items.length = <span class=\"number\">2<\/span> <span class=\"comment\">\/\/ \u4e0d\u662f\u54cd\u5e94\u6027\u7684<\/span><\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>\u4e3a\u4e86\u89e3\u51b3\u7b2c\u4e00\u7c7b\u95ee\u9898\uff0c\u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f\u90fd\u53ef\u4ee5\u5b9e\u73b0\u548c\u00a0<code>vm.items[indexOfItem] = newValue<\/code>\u00a0\u76f8\u540c\u7684\u6548\u679c\uff0c\u540c\u65f6\u4e5f\u5c06\u89e6\u53d1\u72b6\u6001\u66f4\u65b0\uff1a<\/p>\n<figure class=\"highlight js\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"comment\">\/\/ Vue.set<\/span><\/span>\r\n<span class=\"line\">Vue.set(vm.items, indexOfItem, newValue)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"highlight js\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"comment\">\/\/ Array.prototype.splice<\/span><\/span>\r\n<span class=\"line\">vm.items.splice(indexOfItem, <span class=\"number\">1<\/span>, newValue)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u00a0<a href=\"https:\/\/vuejs.org\/v2\/api\/#vm-set\" target=\"_blank\" rel=\"noopener\"><code>vm.$set<\/code><\/a>\u00a0\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u662f\u5168\u5c40\u65b9\u6cd5\u00a0<code>Vue.set<\/code>\u00a0\u7684\u4e00\u4e2a\u522b\u540d\uff1a<\/p>\n<figure class=\"highlight js\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\">vm.$set(vm.items, indexOfItem, newValue)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>\u4e3a\u4e86\u89e3\u51b3\u7b2c\u4e8c\u7c7b\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u00a0<code>splice<\/code>\uff1a<\/p>\n<figure class=\"highlight js\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\">vm.items.splice(newLength)<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h3 id=\"\u5bf9\u8c61\u66f4\u6539\u68c0\u6d4b\u6ce8\u610f\u4e8b\u9879\"><strong><a title=\"\u5bf9\u8c61\u66f4\u6539\u68c0\u6d4b\u6ce8\u610f\u4e8b\u9879\" href=\"https:\/\/cn.vuejs.org\/v2\/guide\/list.html#%E5%AF%B9%E8%B1%A1%E6%9B%B4%E6%94%B9%E6%A3%80%E6%B5%8B%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\" data-scroll=\"\">\u5bf9\u8c61\u66f4\u6539\u68c0\u6d4b\u6ce8\u610f\u4e8b\u9879<\/a><\/strong><\/h3>\n<p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u4e3a\u5df2\u6709\u5bf9\u8c61\u8d4b\u4e88\u591a\u4e2a\u65b0\u5c5e\u6027\uff0c\u6bd4\u5982\u4f7f\u7528\u00a0<code>Object.assign()<\/code>\u00a0\u6216\u00a0<code>_.extend()<\/code>\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u5e94\u8be5\u7528\u4e24\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u60f3\u6dfb\u52a0\u65b0\u7684\u54cd\u5e94\u5f0f\u5c5e\u6027\uff0c\u4e0d\u8981\u50cf\u8fd9\u6837\uff1a<\/p>\n<figure class=\"highlight js\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\"><span class=\"built_in\">Object<\/span>.assign(vm.userProfile, {<\/span>\r\n<span class=\"line\">  age: <span class=\"number\">27<\/span>,<\/span>\r\n<span class=\"line\">  favoriteColor: <span class=\"string\">'Vue Green'<\/span><\/span>\r\n<span class=\"line\">})<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>\u4f60\u5e94\u8be5\u8fd9\u6837\u505a\uff1a<\/p>\n<figure class=\"highlight js\">\n<table>\n<tbody>\n<tr>\n<td class=\"code\">\n<pre><span class=\"line\">vm.userProfile = <span class=\"built_in\">Object<\/span>.assign({}, vm.userProfile, {<\/span>\r\n<span class=\"line\">  age: <span class=\"number\">27<\/span>,<\/span>\r\n<span class=\"line\">  favoriteColor: <span class=\"string\">'Vue Green'<\/span><\/span>\r\n<span class=\"line\">})<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u7ec4\u66f4\u65b0\u68c0\u6d4b \u7531\u4e8e JavaScript \u7684\u9650\u5236\uff0cVue \u4e0d\u80fd\u68c0\u6d4b\u4ee5\u4e0b\u53d8\u52a8\u7684\u6570\u7ec4\uff1a \u5f53\u4f60\u5229\u7528\u7d22\u5f15\u76f4\u63a5\u8bbe\u7f6e\u4e00\u4e2a\u9879 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/adeqing.com\/index.php\/wp-json\/wp\/v2\/posts\/100"}],"collection":[{"href":"https:\/\/adeqing.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adeqing.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adeqing.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/adeqing.com\/index.php\/wp-json\/wp\/v2\/comments?post=100"}],"version-history":[{"count":3,"href":"https:\/\/adeqing.com\/index.php\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/adeqing.com\/index.php\/wp-json\/wp\/v2\/posts\/100\/revisions\/104"}],"wp:attachment":[{"href":"https:\/\/adeqing.com\/index.php\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adeqing.com\/index.php\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adeqing.com\/index.php\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}