[{"data":1,"prerenderedAt":1543},["ShallowReactive",2],{"blog-creating-a-post-code-lookup-with-vue":3},{"id":4,"title":5,"body":6,"date":1533,"description":1534,"extension":1535,"meta":1536,"navigation":1538,"path":1539,"seo":1540,"stem":1541,"__hash__":1542},"blog\u002Fblog\u002Fcreating-a-post-code-lookup-with-vue.md","Creating a postcode lookup with Vue.js",{"type":7,"value":8,"toc":1524},"minimark",[9,14,18,38,42,45,143,152,532,535,539,619,651,655,802,825,985,996,1000,1003,1139,1142,1165,1168,1172,1187,1251,1254,1273,1276,1297,1304,1334,1340,1463,1466,1470,1473,1513,1520],[10,11,13],"h2",{"id":12},"introduction","Introduction",[15,16,17],"p",{},"For this tutorial we're going to create a postcode look-up component using Vue.js that will allow the user to:",[19,20,21,32,35],"ul",{},[22,23,24,25,31],"li",{},"Enter a postcode and search for a postal address (we will be using ",[26,27,28],"a",{"href":28,"rel":29},"https:\u002F\u002Fgetaddress.io\u002F",[30],"nofollow",").",[22,33,34],{},"If a match is found show a list of relevant addresses in a dropdown.",[22,36,37],{},"When the user selects a dropdown value it will auto populate the address fields.",[10,39,41],{"id":40},"setting-up-parent-view","Setting up parent view",[15,43,44],{},"First off we're going to add a couple of address properties to the data object on the parent that will be updated when I use the postcode look-up component:",[46,47,52],"pre",{"className":48,"code":49,"language":50,"meta":51,"style":51},"language-js shiki shiki-themes github-dark","data() {\n  return {\n    house: '',\n    building: '',\n    street: '',\n    suburb: '',\n    city: '',\n  }\n}        \n","js","",[53,54,55,68,78,91,101,111,121,131,137],"code",{"__ignoreMap":51},[56,57,60,64],"span",{"class":58,"line":59},"line",1,[56,61,63],{"class":62},"svObZ","data",[56,65,67],{"class":66},"s95oV","() {\n",[56,69,71,75],{"class":58,"line":70},2,[56,72,74],{"class":73},"snl16","  return",[56,76,77],{"class":66}," {\n",[56,79,81,84,88],{"class":58,"line":80},3,[56,82,83],{"class":66},"    house: ",[56,85,87],{"class":86},"sU2Wk","''",[56,89,90],{"class":66},",\n",[56,92,94,97,99],{"class":58,"line":93},4,[56,95,96],{"class":66},"    building: ",[56,98,87],{"class":86},[56,100,90],{"class":66},[56,102,104,107,109],{"class":58,"line":103},5,[56,105,106],{"class":66},"    street: ",[56,108,87],{"class":86},[56,110,90],{"class":66},[56,112,114,117,119],{"class":58,"line":113},6,[56,115,116],{"class":66},"    suburb: ",[56,118,87],{"class":86},[56,120,90],{"class":66},[56,122,124,127,129],{"class":58,"line":123},7,[56,125,126],{"class":66},"    city: ",[56,128,87],{"class":86},[56,130,90],{"class":66},[56,132,134],{"class":58,"line":133},8,[56,135,136],{"class":66},"  }\n",[56,138,140],{"class":58,"line":139},9,[56,141,142],{"class":66},"}\n",[15,144,145,146,151],{},"We're also going to use ",[26,147,150],{"href":148,"target":149},"https:\u002F\u002Fvuejs.org\u002Fv2\u002Fguide\u002Fforms.html","_blank","v-model"," on each one of the inputs in order to create a two way binding between the model above and the template below:",[46,153,155],{"className":48,"code":154,"language":50,"meta":51,"style":51},"\u003Cfieldset>\n  \u003Clegend>Your address\u003C\u002Flegend>\n  \u003Cdiv>\n      \u003Clabel for=\"house\">House\u002FFlat Number\u003C\u002Flabel>\n      \u003Cinput id=\"house\" v-model=\"house\" placeholder=\"House\u002FFlat Number\">\n  \u003C\u002Fdiv>\n  \u003Cdiv>\n      \u003Clabel for=\"building\">Building\u003C\u002Flabel>\n      \u003Cinput id=\"building\" v-model=\"building\" placeholder=\"Building\">\n  \u003C\u002Fdiv>\n  \u003Cdiv>\n      \u003Clabel for=\"street\">Street\u003C\u002Flabel>\n      \u003Cinput id=\"street\" v-model=\"street\" placeholder=\"Street\">\n  \u003C\u002Fdiv>\n  \u003Cdiv>\n      \u003Clabel for=\"suburb\">Suburb\u003C\u002Flabel>\n      \u003Cinput id=\"suburb\" v-model=\"suburb\" placeholder=\"Suburb\">\n  \u003C\u002Fdiv>\n  \u003Cdiv>\n      \u003Clabel for=\"city\">City\u003C\u002Flabel>\n      \u003Cinput id=\"city\" v-model=\"city\" placeholder=\"City\">\n  \u003C\u002Fdiv>\n\u003C\u002Ffieldset>\n",[53,156,157,169,184,193,217,248,257,265,285,312,321,330,351,379,388,397,418,446,455,464,485,513,522],{"__ignoreMap":51},[56,158,159,162,166],{"class":58,"line":59},[56,160,161],{"class":66},"\u003C",[56,163,165],{"class":164},"s4JwU","fieldset",[56,167,168],{"class":66},">\n",[56,170,171,174,177,180,182],{"class":58,"line":70},[56,172,173],{"class":66},"  \u003C",[56,175,176],{"class":164},"legend",[56,178,179],{"class":66},">Your address\u003C\u002F",[56,181,176],{"class":164},[56,183,168],{"class":66},[56,185,186,188,191],{"class":58,"line":80},[56,187,173],{"class":66},[56,189,190],{"class":164},"div",[56,192,168],{"class":66},[56,194,195,198,201,204,207,210,213,215],{"class":58,"line":93},[56,196,197],{"class":66},"      \u003C",[56,199,200],{"class":164},"label",[56,202,203],{"class":62}," for",[56,205,206],{"class":73},"=",[56,208,209],{"class":86},"\"house\"",[56,211,212],{"class":66},">House\u002FFlat Number\u003C\u002F",[56,214,200],{"class":164},[56,216,168],{"class":66},[56,218,219,221,224,227,229,231,234,236,238,241,243,246],{"class":58,"line":103},[56,220,197],{"class":66},[56,222,223],{"class":164},"input",[56,225,226],{"class":62}," id",[56,228,206],{"class":73},[56,230,209],{"class":86},[56,232,233],{"class":62}," v-model",[56,235,206],{"class":73},[56,237,209],{"class":86},[56,239,240],{"class":62}," placeholder",[56,242,206],{"class":73},[56,244,245],{"class":86},"\"House\u002FFlat Number\"",[56,247,168],{"class":66},[56,249,250,253,255],{"class":58,"line":113},[56,251,252],{"class":66},"  \u003C\u002F",[56,254,190],{"class":164},[56,256,168],{"class":66},[56,258,259,261,263],{"class":58,"line":123},[56,260,173],{"class":66},[56,262,190],{"class":164},[56,264,168],{"class":66},[56,266,267,269,271,273,275,278,281,283],{"class":58,"line":133},[56,268,197],{"class":66},[56,270,200],{"class":164},[56,272,203],{"class":62},[56,274,206],{"class":73},[56,276,277],{"class":86},"\"building\"",[56,279,280],{"class":66},">Building\u003C\u002F",[56,282,200],{"class":164},[56,284,168],{"class":66},[56,286,287,289,291,293,295,297,299,301,303,305,307,310],{"class":58,"line":139},[56,288,197],{"class":66},[56,290,223],{"class":164},[56,292,226],{"class":62},[56,294,206],{"class":73},[56,296,277],{"class":86},[56,298,233],{"class":62},[56,300,206],{"class":73},[56,302,277],{"class":86},[56,304,240],{"class":62},[56,306,206],{"class":73},[56,308,309],{"class":86},"\"Building\"",[56,311,168],{"class":66},[56,313,315,317,319],{"class":58,"line":314},10,[56,316,252],{"class":66},[56,318,190],{"class":164},[56,320,168],{"class":66},[56,322,324,326,328],{"class":58,"line":323},11,[56,325,173],{"class":66},[56,327,190],{"class":164},[56,329,168],{"class":66},[56,331,333,335,337,339,341,344,347,349],{"class":58,"line":332},12,[56,334,197],{"class":66},[56,336,200],{"class":164},[56,338,203],{"class":62},[56,340,206],{"class":73},[56,342,343],{"class":86},"\"street\"",[56,345,346],{"class":66},">Street\u003C\u002F",[56,348,200],{"class":164},[56,350,168],{"class":66},[56,352,354,356,358,360,362,364,366,368,370,372,374,377],{"class":58,"line":353},13,[56,355,197],{"class":66},[56,357,223],{"class":164},[56,359,226],{"class":62},[56,361,206],{"class":73},[56,363,343],{"class":86},[56,365,233],{"class":62},[56,367,206],{"class":73},[56,369,343],{"class":86},[56,371,240],{"class":62},[56,373,206],{"class":73},[56,375,376],{"class":86},"\"Street\"",[56,378,168],{"class":66},[56,380,382,384,386],{"class":58,"line":381},14,[56,383,252],{"class":66},[56,385,190],{"class":164},[56,387,168],{"class":66},[56,389,391,393,395],{"class":58,"line":390},15,[56,392,173],{"class":66},[56,394,190],{"class":164},[56,396,168],{"class":66},[56,398,400,402,404,406,408,411,414,416],{"class":58,"line":399},16,[56,401,197],{"class":66},[56,403,200],{"class":164},[56,405,203],{"class":62},[56,407,206],{"class":73},[56,409,410],{"class":86},"\"suburb\"",[56,412,413],{"class":66},">Suburb\u003C\u002F",[56,415,200],{"class":164},[56,417,168],{"class":66},[56,419,421,423,425,427,429,431,433,435,437,439,441,444],{"class":58,"line":420},17,[56,422,197],{"class":66},[56,424,223],{"class":164},[56,426,226],{"class":62},[56,428,206],{"class":73},[56,430,410],{"class":86},[56,432,233],{"class":62},[56,434,206],{"class":73},[56,436,410],{"class":86},[56,438,240],{"class":62},[56,440,206],{"class":73},[56,442,443],{"class":86},"\"Suburb\"",[56,445,168],{"class":66},[56,447,449,451,453],{"class":58,"line":448},18,[56,450,252],{"class":66},[56,452,190],{"class":164},[56,454,168],{"class":66},[56,456,458,460,462],{"class":58,"line":457},19,[56,459,173],{"class":66},[56,461,190],{"class":164},[56,463,168],{"class":66},[56,465,467,469,471,473,475,478,481,483],{"class":58,"line":466},20,[56,468,197],{"class":66},[56,470,200],{"class":164},[56,472,203],{"class":62},[56,474,206],{"class":73},[56,476,477],{"class":86},"\"city\"",[56,479,480],{"class":66},">City\u003C\u002F",[56,482,200],{"class":164},[56,484,168],{"class":66},[56,486,488,490,492,494,496,498,500,502,504,506,508,511],{"class":58,"line":487},21,[56,489,197],{"class":66},[56,491,223],{"class":164},[56,493,226],{"class":62},[56,495,206],{"class":73},[56,497,477],{"class":86},[56,499,233],{"class":62},[56,501,206],{"class":73},[56,503,477],{"class":86},[56,505,240],{"class":62},[56,507,206],{"class":73},[56,509,510],{"class":86},"\"City\"",[56,512,168],{"class":66},[56,514,516,518,520],{"class":58,"line":515},22,[56,517,252],{"class":66},[56,519,190],{"class":164},[56,521,168],{"class":66},[56,523,525,528,530],{"class":58,"line":524},23,[56,526,527],{"class":66},"\u003C\u002F",[56,529,165],{"class":164},[56,531,168],{"class":66},[15,533,534],{},"Now that we have our parent view set up, it's time to set up our postcode look-up component.",[10,536,538],{"id":537},"creating-our-postcodelookup-component","Creating our PostCodeLookUp component",[46,540,542],{"className":48,"code":541,"language":50,"meta":51,"style":51},"    data() {\n      return {\n        isSearching: false,\n        postcode: '',\n        addressList: [],\n        selectedAddress: '',\n        postCodeSearched: false,\n        noResults: false\n      }\n    }\n",[53,543,544,551,558,569,578,583,592,601,609,614],{"__ignoreMap":51},[56,545,546,549],{"class":58,"line":59},[56,547,548],{"class":62},"    data",[56,550,67],{"class":66},[56,552,553,556],{"class":58,"line":70},[56,554,555],{"class":73},"      return",[56,557,77],{"class":66},[56,559,560,563,567],{"class":58,"line":80},[56,561,562],{"class":66},"        isSearching: ",[56,564,566],{"class":565},"sDLfK","false",[56,568,90],{"class":66},[56,570,571,574,576],{"class":58,"line":93},[56,572,573],{"class":66},"        postcode: ",[56,575,87],{"class":86},[56,577,90],{"class":66},[56,579,580],{"class":58,"line":103},[56,581,582],{"class":66},"        addressList: [],\n",[56,584,585,588,590],{"class":58,"line":113},[56,586,587],{"class":66},"        selectedAddress: ",[56,589,87],{"class":86},[56,591,90],{"class":66},[56,593,594,597,599],{"class":58,"line":123},[56,595,596],{"class":66},"        postCodeSearched: ",[56,598,566],{"class":565},[56,600,90],{"class":66},[56,602,603,606],{"class":58,"line":133},[56,604,605],{"class":66},"        noResults: ",[56,607,608],{"class":565},"false\n",[56,610,611],{"class":58,"line":139},[56,612,613],{"class":66},"      }\n",[56,615,616],{"class":58,"line":314},[56,617,618],{"class":66},"    }\n",[19,620,621,627,633,639,645],{},[22,622,623,626],{},[53,624,625],{},"postcode",": this is in order to keep track of the postcode that the user enters.",[22,628,629,632],{},[53,630,631],{},"addressList",": an empty array, this will be populated with a list of addresses and displayed in the dropdown after the user has searched a valid postcode.",[22,634,635,638],{},[53,636,637],{},"selectedAddress",": this will keep track of the the address that the user selects in the dropdown.",[22,640,641,644],{},[53,642,643],{},"postCodeSearched",": this boolean is used to conditionally show the dropdown after the user has searched a postcode (initially set to false).",[22,646,647,650],{},[53,648,649],{},"noResults",": boolean that will be used to show a error message if no results are returned from the api.",[10,652,654],{"id":653},"creating-the-search","Creating the search",[46,656,658],{"className":48,"code":657,"language":50,"meta":51,"style":51},"    \u003Cfieldset>\n        \u003Clegend>\n          Address Lookup\n        \u003C\u002Flegend>\n        \u003Cdiv>\n          \u003Clabel for=\"postcodeLookup\">Enter postcode\u003C\u002Flabel>\n          \u003Cinput id=\"postcodeLookup\" v-model=\"postcode\" type=\"text\" placeholder=\"Enter postcode\">\n          \u003Cbutton v-on:click=\"addressLookUp\">Search Postcode\u003C\u002Fbutton>\n        \u003C\u002Fdiv>\n    \u003C\u002Ffieldset>\n",[53,659,660,669,678,683,692,700,721,757,785,793],{"__ignoreMap":51},[56,661,662,665,667],{"class":58,"line":59},[56,663,664],{"class":66},"    \u003C",[56,666,165],{"class":164},[56,668,168],{"class":66},[56,670,671,674,676],{"class":58,"line":70},[56,672,673],{"class":66},"        \u003C",[56,675,176],{"class":164},[56,677,168],{"class":66},[56,679,680],{"class":58,"line":80},[56,681,682],{"class":66},"          Address Lookup\n",[56,684,685,688,690],{"class":58,"line":93},[56,686,687],{"class":66},"        \u003C\u002F",[56,689,176],{"class":164},[56,691,168],{"class":66},[56,693,694,696,698],{"class":58,"line":103},[56,695,673],{"class":66},[56,697,190],{"class":164},[56,699,168],{"class":66},[56,701,702,705,707,709,711,714,717,719],{"class":58,"line":113},[56,703,704],{"class":66},"          \u003C",[56,706,200],{"class":164},[56,708,203],{"class":62},[56,710,206],{"class":73},[56,712,713],{"class":86},"\"postcodeLookup\"",[56,715,716],{"class":66},">Enter postcode\u003C\u002F",[56,718,200],{"class":164},[56,720,168],{"class":66},[56,722,723,725,727,729,731,733,735,737,740,743,745,748,750,752,755],{"class":58,"line":123},[56,724,704],{"class":66},[56,726,223],{"class":164},[56,728,226],{"class":62},[56,730,206],{"class":73},[56,732,713],{"class":86},[56,734,233],{"class":62},[56,736,206],{"class":73},[56,738,739],{"class":86},"\"postcode\"",[56,741,742],{"class":62}," type",[56,744,206],{"class":73},[56,746,747],{"class":86},"\"text\"",[56,749,240],{"class":62},[56,751,206],{"class":73},[56,753,754],{"class":86},"\"Enter postcode\"",[56,756,168],{"class":66},[56,758,759,761,764,767,770,773,775,778,781,783],{"class":58,"line":133},[56,760,704],{"class":66},[56,762,763],{"class":164},"button",[56,765,766],{"class":62}," v-on",[56,768,769],{"class":66},":",[56,771,772],{"class":62},"click",[56,774,206],{"class":73},[56,776,777],{"class":86},"\"addressLookUp\"",[56,779,780],{"class":66},">Search Postcode\u003C\u002F",[56,782,763],{"class":164},[56,784,168],{"class":66},[56,786,787,789,791],{"class":58,"line":139},[56,788,687],{"class":66},[56,790,190],{"class":164},[56,792,168],{"class":66},[56,794,795,798,800],{"class":58,"line":314},[56,796,797],{"class":66},"    \u003C\u002F",[56,799,165],{"class":164},[56,801,168],{"class":66},[19,803,804,811,819,822],{},[22,805,806,807,810],{},"Firstly, we are going to create a function called ",[53,808,809],{},"addressLookUp"," and add it the methods object within our component.",[22,812,813,814,818],{},"Within this function we are going to use ",[26,815,817],{"href":816,"target":149},"https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Faxios","axios"," (a promise based HTTP client - I am importing it directly into the component using es6 modules) to make a request from the getAddress api with our postcode model value passed in.",[22,820,821],{},"If there is a successful response we are going to bind our postcode list to the array of addresses returned from the response, and also set our postcodeSearched to true.",[22,823,824],{},"If there are no results we will catch this error and return a message to the user.",[46,826,828],{"className":48,"code":827,"language":50,"meta":51,"style":51},"methods: {\n      addressLookUp() {\n        axios.get(`https:\u002F\u002Fapi.getAddress.io\u002Ffind\u002F${this.postcode}?api-key={api-key-here}`)\n          .then(response => { \n                this.addressList = response.data.addresses\n                this.postCodeSearched = true\n                this.noResults = false\n          })\n          .catch(error => {\n            this.noResults = true\n            console.log(error);\n          })\n      }\n}\n",[53,829,830,838,845,873,893,906,918,930,935,951,962,973,977,981],{"__ignoreMap":51},[56,831,832,835],{"class":58,"line":59},[56,833,834],{"class":62},"methods",[56,836,837],{"class":66},": {\n",[56,839,840,843],{"class":58,"line":70},[56,841,842],{"class":62},"      addressLookUp",[56,844,67],{"class":66},[56,846,847,850,853,856,859,862,865,867,870],{"class":58,"line":80},[56,848,849],{"class":66},"        axios.",[56,851,852],{"class":62},"get",[56,854,855],{"class":66},"(",[56,857,858],{"class":86},"`https:\u002F\u002Fapi.getAddress.io\u002Ffind\u002F${",[56,860,861],{"class":565},"this",[56,863,864],{"class":86},".",[56,866,625],{"class":66},[56,868,869],{"class":86},"}?api-key={api-key-here}`",[56,871,872],{"class":66},")\n",[56,874,875,878,881,883,887,890],{"class":58,"line":93},[56,876,877],{"class":66},"          .",[56,879,880],{"class":62},"then",[56,882,855],{"class":66},[56,884,886],{"class":885},"s9osk","response",[56,888,889],{"class":73}," =>",[56,891,892],{"class":66}," { \n",[56,894,895,898,901,903],{"class":58,"line":103},[56,896,897],{"class":565},"                this",[56,899,900],{"class":66},".addressList ",[56,902,206],{"class":73},[56,904,905],{"class":66}," response.data.addresses\n",[56,907,908,910,913,915],{"class":58,"line":113},[56,909,897],{"class":565},[56,911,912],{"class":66},".postCodeSearched ",[56,914,206],{"class":73},[56,916,917],{"class":565}," true\n",[56,919,920,922,925,927],{"class":58,"line":123},[56,921,897],{"class":565},[56,923,924],{"class":66},".noResults ",[56,926,206],{"class":73},[56,928,929],{"class":565}," false\n",[56,931,932],{"class":58,"line":133},[56,933,934],{"class":66},"          })\n",[56,936,937,939,942,944,947,949],{"class":58,"line":139},[56,938,877],{"class":66},[56,940,941],{"class":62},"catch",[56,943,855],{"class":66},[56,945,946],{"class":885},"error",[56,948,889],{"class":73},[56,950,77],{"class":66},[56,952,953,956,958,960],{"class":58,"line":314},[56,954,955],{"class":565},"            this",[56,957,924],{"class":66},[56,959,206],{"class":73},[56,961,917],{"class":565},[56,963,964,967,970],{"class":58,"line":323},[56,965,966],{"class":66},"            console.",[56,968,969],{"class":62},"log",[56,971,972],{"class":66},"(error);\n",[56,974,975],{"class":58,"line":332},[56,976,934],{"class":66},[56,978,979],{"class":58,"line":353},[56,980,613],{"class":66},[56,982,983],{"class":58,"line":381},[56,984,142],{"class":66},[15,986,987,991,992,995],{},[988,989,990],"strong",{},"Note:"," you will need to pass an ",[26,993,994],{"href":28,"target":149},"api-key"," into the URL above.",[10,997,999],{"id":998},"displaying-the-results-of-the-search","Displaying the results of the search",[15,1001,1002],{},"Now that we have a list of addresses returned, we need somewhere to display it:",[46,1004,1006],{"className":48,"code":1005,"language":50,"meta":51,"style":51},"    \u003Cfieldset v-if=\"postCodeSearched\">\n      \u003Clegend>Select your address\u003C\u002Flegend>\n      \u003Cselect v-model=\"selectedAddress\" v-on:change=\"emitAddress\">\n        \u003Coption value='' selected>Please select\u003C\u002Foption>\n        \u003Coption :value=\"item\" v-for=\"(item, index) in addressList\" :key=\"index\">{{item}}\u003C\u002Foption>\n      \u003C\u002Fselect>\n    \u003C\u002Ffieldset>\n    \u003Cp v-if=\"noResults\">Sorry, we were unable to find that post-code\u003C\u002Fp>\n",[53,1007,1008,1024,1037,1065,1089,1110,1115,1120],{"__ignoreMap":51},[56,1009,1010,1012,1014,1017,1019,1022],{"class":58,"line":59},[56,1011,664],{"class":66},[56,1013,165],{"class":164},[56,1015,1016],{"class":62}," v-if",[56,1018,206],{"class":73},[56,1020,1021],{"class":86},"\"postCodeSearched\"",[56,1023,168],{"class":66},[56,1025,1026,1028,1030,1033,1035],{"class":58,"line":70},[56,1027,197],{"class":66},[56,1029,176],{"class":164},[56,1031,1032],{"class":66},">Select your address\u003C\u002F",[56,1034,176],{"class":164},[56,1036,168],{"class":66},[56,1038,1039,1041,1044,1046,1048,1051,1053,1055,1058,1060,1063],{"class":58,"line":80},[56,1040,197],{"class":66},[56,1042,1043],{"class":164},"select",[56,1045,233],{"class":62},[56,1047,206],{"class":73},[56,1049,1050],{"class":86},"\"selectedAddress\"",[56,1052,766],{"class":62},[56,1054,769],{"class":66},[56,1056,1057],{"class":62},"change",[56,1059,206],{"class":73},[56,1061,1062],{"class":86},"\"emitAddress\"",[56,1064,168],{"class":66},[56,1066,1067,1069,1072,1075,1077,1079,1082,1085,1087],{"class":58,"line":93},[56,1068,673],{"class":66},[56,1070,1071],{"class":164},"option",[56,1073,1074],{"class":62}," value",[56,1076,206],{"class":73},[56,1078,87],{"class":86},[56,1080,1081],{"class":62}," selected",[56,1083,1084],{"class":66},">Please select\u003C\u002F",[56,1086,1071],{"class":164},[56,1088,168],{"class":66},[56,1090,1091,1093,1095,1099,1102,1104,1107],{"class":58,"line":103},[56,1092,673],{"class":66},[56,1094,1071],{"class":164},[56,1096,1098],{"class":1097},"s6RL2"," :value=\"item\"",[56,1100,1101],{"class":62}," v-for",[56,1103,206],{"class":73},[56,1105,1106],{"class":86},"\"(item, index) in addressList\"",[56,1108,1109],{"class":1097}," :key=\"index\">{{item}}\u003C\u002Foption>\n",[56,1111,1112],{"class":58,"line":113},[56,1113,1114],{"class":1097},"      \u003C\u002Fselect>\n",[56,1116,1117],{"class":58,"line":123},[56,1118,1119],{"class":1097},"    \u003C\u002Ffieldset>\n",[56,1121,1122,1125,1127,1129,1132,1135,1137],{"class":58,"line":133},[56,1123,1124],{"class":1097},"    \u003Cp",[56,1126,1016],{"class":62},[56,1128,206],{"class":73},[56,1130,1131],{"class":86},"\"noResults\"",[56,1133,1134],{"class":66},">Sorry, we were unable to find that post-code\u003C\u002F",[56,1136,15],{"class":164},[56,1138,168],{"class":66},[15,1140,1141],{},"Let's breakdown the above:",[19,1143,1144,1150,1156,1162],{},[22,1145,1146,1147,1149],{},"We have set up a v-if and bound it to the postCodeSearched property, in the ",[53,1148,809],{}," function we created above this will be set to true if a list of addresses is returned.",[22,1151,1152,1153,1155],{},"We have bound the select to ",[53,1154,637],{}," so that we can keep track of the currently selected address.",[22,1157,1158,1159,1161],{},"We have used the v-for directive to display the list of addresses returned from the ",[53,1160,809],{}," function within our select.",[22,1163,1164],{},"If there are no results we return a helpful message",[15,1166,1167],{},"Great, now we just need to update the address fields when the user selects an address in the dropdown.",[10,1169,1171],{"id":1170},"updating-the-address-fields-on-our-parent-view","Updating the address fields on our parent view",[15,1173,1174,1175,1178,1179,1183,1184,1186],{},"In the component above you will notice that I have attached a ",[53,1176,1177],{},"emitAddress"," function to the on change event of the select. This will ",[26,1180,1182],{"href":1181,"target":149},"https:\u002F\u002Fvuejs.org\u002Fv2\u002Fguide\u002Fcomponents-custom-events.html","emit"," the ",[53,1185,637],{}," property to the parent view the component sits on:",[46,1188,1190],{"className":48,"code":1189,"language":50,"meta":51,"style":51},"  emitAddress() {\n      if(this.selectedAddress != '') {\n          this.$emit('addressChanged', this.selectedAddress)\n      }\n  }\n",[53,1191,1192,1199,1220,1243,1247],{"__ignoreMap":51},[56,1193,1194,1197],{"class":58,"line":59},[56,1195,1196],{"class":62},"  emitAddress",[56,1198,67],{"class":66},[56,1200,1201,1204,1206,1208,1211,1214,1217],{"class":58,"line":70},[56,1202,1203],{"class":73},"      if",[56,1205,855],{"class":66},[56,1207,861],{"class":565},[56,1209,1210],{"class":66},".selectedAddress ",[56,1212,1213],{"class":73},"!=",[56,1215,1216],{"class":86}," ''",[56,1218,1219],{"class":66},") {\n",[56,1221,1222,1225,1227,1230,1232,1235,1238,1240],{"class":58,"line":80},[56,1223,1224],{"class":565},"          this",[56,1226,864],{"class":66},[56,1228,1229],{"class":62},"$emit",[56,1231,855],{"class":66},[56,1233,1234],{"class":86},"'addressChanged'",[56,1236,1237],{"class":66},", ",[56,1239,861],{"class":565},[56,1241,1242],{"class":66},".selectedAddress)\n",[56,1244,1245],{"class":58,"line":93},[56,1246,613],{"class":66},[56,1248,1249],{"class":58,"line":103},[56,1250,136],{"class":66},[15,1252,1253],{},"Now back on our parent view we want to firstly import our component:",[46,1255,1257],{"className":48,"code":1256,"language":50,"meta":51,"style":51},"import PostCodeLookUp from '.\u002Fcomponents\u002FPostCodeLookUp'\n",[53,1258,1259],{"__ignoreMap":51},[56,1260,1261,1264,1267,1270],{"class":58,"line":59},[56,1262,1263],{"class":73},"import",[56,1265,1266],{"class":66}," PostCodeLookUp ",[56,1268,1269],{"class":73},"from",[56,1271,1272],{"class":86}," '.\u002Fcomponents\u002FPostCodeLookUp'\n",[15,1274,1275],{},"And then register the component:",[46,1277,1279],{"className":48,"code":1278,"language":50,"meta":51,"style":51},"  components: {\n    PostCodeLookUp\n  }\n",[53,1280,1281,1288,1293],{"__ignoreMap":51},[56,1282,1283,1286],{"class":58,"line":59},[56,1284,1285],{"class":62},"  components",[56,1287,837],{"class":66},[56,1289,1290],{"class":58,"line":70},[56,1291,1292],{"class":66},"    PostCodeLookUp\n",[56,1294,1295],{"class":58,"line":80},[56,1296,136],{"class":66},[15,1298,1299,1300,1303],{},"On the component itself we are going to capture our custom event (",[53,1301,1302],{},"addressChanged",") that we emitted in the previous step and call a function that will update our address:",[46,1305,1307],{"className":48,"code":1306,"language":50,"meta":51,"style":51},"\u003Cpost-code-look-up v-on:addressChanged=\"updateAddress\">\u003C\u002Fpost-code-look-up>\n",[53,1308,1309],{"__ignoreMap":51},[56,1310,1311,1313,1316,1318,1320,1322,1324,1327,1330,1332],{"class":58,"line":59},[56,1312,161],{"class":66},[56,1314,1315],{"class":565},"post-code-look-up",[56,1317,766],{"class":62},[56,1319,769],{"class":66},[56,1321,1302],{"class":62},[56,1323,206],{"class":73},[56,1325,1326],{"class":86},"\"updateAddress\"",[56,1328,1329],{"class":66},">\u003C\u002F",[56,1331,1315],{"class":565},[56,1333,168],{"class":66},[15,1335,1336,1337,1339],{},"In terms of the function it will take the payLoad (in this case the ",[53,1338,637],{}," from our component), splitting it and assigning parts of the address string to particular address fields:",[46,1341,1343],{"className":48,"code":1342,"language":50,"meta":51,"style":51},"updateAddress(payLoad) {   \n  let addressArray = payLoad.split(\",\")\n  this.house = addressArray[0]\n  this.building = addressArray[1]\n  this.street = addressArray[2]\n  this.suburb = addressArray[4]\n  this.city = addressArray[5]\n}\n",[53,1344,1345,1353,1376,1395,1411,1427,1443,1459],{"__ignoreMap":51},[56,1346,1347,1350],{"class":58,"line":59},[56,1348,1349],{"class":62},"updateAddress",[56,1351,1352],{"class":66},"(payLoad) {   \n",[56,1354,1355,1358,1361,1363,1366,1369,1371,1374],{"class":58,"line":70},[56,1356,1357],{"class":73},"  let",[56,1359,1360],{"class":66}," addressArray ",[56,1362,206],{"class":73},[56,1364,1365],{"class":66}," payLoad.",[56,1367,1368],{"class":62},"split",[56,1370,855],{"class":66},[56,1372,1373],{"class":86},"\",\"",[56,1375,872],{"class":66},[56,1377,1378,1381,1384,1386,1389,1392],{"class":58,"line":80},[56,1379,1380],{"class":565},"  this",[56,1382,1383],{"class":66},".house ",[56,1385,206],{"class":73},[56,1387,1388],{"class":66}," addressArray[",[56,1390,1391],{"class":565},"0",[56,1393,1394],{"class":66},"]\n",[56,1396,1397,1399,1402,1404,1406,1409],{"class":58,"line":93},[56,1398,1380],{"class":565},[56,1400,1401],{"class":66},".building ",[56,1403,206],{"class":73},[56,1405,1388],{"class":66},[56,1407,1408],{"class":565},"1",[56,1410,1394],{"class":66},[56,1412,1413,1415,1418,1420,1422,1425],{"class":58,"line":103},[56,1414,1380],{"class":565},[56,1416,1417],{"class":66},".street ",[56,1419,206],{"class":73},[56,1421,1388],{"class":66},[56,1423,1424],{"class":565},"2",[56,1426,1394],{"class":66},[56,1428,1429,1431,1434,1436,1438,1441],{"class":58,"line":113},[56,1430,1380],{"class":565},[56,1432,1433],{"class":66},".suburb ",[56,1435,206],{"class":73},[56,1437,1388],{"class":66},[56,1439,1440],{"class":565},"4",[56,1442,1394],{"class":66},[56,1444,1445,1447,1450,1452,1454,1457],{"class":58,"line":123},[56,1446,1380],{"class":565},[56,1448,1449],{"class":66},".city ",[56,1451,206],{"class":73},[56,1453,1388],{"class":66},[56,1455,1456],{"class":565},"5",[56,1458,1394],{"class":66},[56,1460,1461],{"class":58,"line":133},[56,1462,142],{"class":66},[15,1464,1465],{},"And there we have it, our component is now succesfully updating our address fields :)",[10,1467,1469],{"id":1468},"conclusion","Conclusion",[15,1471,1472],{},"We have succesfully created a post code look up component that utilizes several key Vue.js features including:",[1474,1475,1476,1481,1487,1493,1504,1507,1510],"ol",{},[22,1477,1478,1480],{},[53,1479,150],{}," to create two way binding for our address fields, postcode and selected address.",[22,1482,1483,1486],{},[53,1484,1485],{},"v-if"," directive to conditionally show particular parts of our template.",[22,1488,1489,1492],{},[53,1490,1491],{},"v-for"," directive for looping through our address list.",[22,1494,1495,1496,1499,1500,1503],{},"Setting up event listeners using ",[53,1497,1498],{},"v-on:change"," and ",[53,1501,1502],{},"v-on:click"," and calling custom functions.",[22,1505,1506],{},"Using axios to return data from an api endpoint.",[22,1508,1509],{},"Emitting custom events from child to parent.",[22,1511,1512],{},"Creating a isolated component that can be imported onto different parent views.",[15,1514,1515,1516,864],{},"All code used in this tutorial can be found on this ",[26,1517,1519],{"href":1518,"target":149},"https:\u002F\u002Fgithub.com\u002Fcjloff\u002Fvue-post-code-look-up","demo",[1521,1522,1523],"style",{},"html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}html pre.shiki code .s95oV, html code.shiki .s95oV{--shiki-default:#E1E4E8}html pre.shiki code .snl16, html code.shiki .snl16{--shiki-default:#F97583}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .s4JwU, html code.shiki .s4JwU{--shiki-default:#85E89D}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}html pre.shiki code .s9osk, html code.shiki .s9osk{--shiki-default:#FFAB70}html pre.shiki code .s6RL2, html code.shiki .s6RL2{--shiki-default:#FDAEB7;--shiki-default-font-style:italic}",{"title":51,"searchDepth":70,"depth":70,"links":1525},[1526,1527,1528,1529,1530,1531,1532],{"id":12,"depth":70,"text":13},{"id":40,"depth":70,"text":41},{"id":537,"depth":70,"text":538},{"id":653,"depth":70,"text":654},{"id":998,"depth":70,"text":999},{"id":1170,"depth":70,"text":1171},{"id":1468,"depth":70,"text":1469},"2019-08-28","For this tutorial we’re going to create a postcode look-up component using Vue.js that will allow the user to: Enter a postcode and search…","md",{"url":1537},"creating-a-post-code-lookup-with-vue",true,"\u002Fblog\u002Fcreating-a-post-code-lookup-with-vue",{"title":5,"description":1534},"blog\u002Fcreating-a-post-code-lookup-with-vue","WHXeJm-pfHil4fR441ahkg3OYJqPozTbyLLPjdjUfF8",1794763528558]