Skip to content

Commit ead2f89

Browse files
committed
more store
1 parent a6472cc commit ead2f89

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

README.md

+99
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,105 @@ And delete all files and code related to the original component. Our Angular app
12381238
See `tag-07-es6-components`.
12391239

12401240

1241+
Bonus #2: free Vue components from Angular services
1242+
----
1243+
Now we have got a store we can move on and strip `searchService` of unnecessary parts.
1244+
For example, the `store.currentDetail` property and the `selectItem(id)` method are exclusively used by the `detail` Vue component. Let's move them from the Angular service to the Vuex store.
1245+
1246+
Comment (or delete) the following lines:
1247+
1248+
**ngVueBridgeCode/services/searchService.js**
1249+
```javascript
1250+
export class SearchService {
1251+
constructor($timeout, VuexStore) {
1252+
/* ... */
1253+
this.store = {
1254+
searching: false,
1255+
searchParam: '',
1256+
searchResults: []
1257+
//, currentDetail: null
1258+
};
1259+
/* ... */
1260+
}
1261+
1262+
/*
1263+
selectItem(id) {
1264+
this.store.currentDetail = this.store.searchResults.find(function (r) {
1265+
return r.id === id;
1266+
});
1267+
}
1268+
*/
1269+
1270+
query (searchParam) {
1271+
/* ... */
1272+
// this.store.currentDetail = null;
1273+
/* ... */
1274+
}
1275+
};
1276+
```
1277+
1278+
And modify the store:
1279+
1280+
**vueCode/store.js**
1281+
```javascript
1282+
export default new Vuex.Store({
1283+
state: {
1284+
/* ... */
1285+
selectedDetailId: null
1286+
},
1287+
actions: {
1288+
getResults: (store, searchParam) => {
1289+
return new Promise((resolve, reject) => {
1290+
/* ... */
1291+
1292+
setTimeout(() => {
1293+
store.commit('setSelectedDetail', null);
1294+
store.commit('setResults', results);
1295+
resolve(results);
1296+
}, 2000);
1297+
});
1298+
},
1299+
/* ... */
1300+
selectItem: (store, id) => {
1301+
store.commit('setSelectedDetail', id);
1302+
}
1303+
},
1304+
mutations: {
1305+
/* ... */
1306+
setSelectedDetail: (state, id) => {
1307+
state.selectedDetailId = id;
1308+
}
1309+
},
1310+
getters: {
1311+
/* ... */
1312+
currentDetail: state => state.results.find(r => r.id === state.selectedDetailId)
1313+
}
1314+
});
1315+
```
1316+
1317+
Lastly, rewrite the component to use the store in place of the service:
1318+
1319+
**vueCode/components/Detail/index.vue**
1320+
```javascript
1321+
export default {
1322+
/* ... */
1323+
computed: {
1324+
currentDetail () {
1325+
return this.$store.getters['currentDetail'];
1326+
}
1327+
},
1328+
methods: {
1329+
getDetail (detailId) {
1330+
this.$store.dispatch('selectItem', Number(detailId));
1331+
},
1332+
/* ... */
1333+
},
1334+
/* ... */
1335+
};
1336+
```
1337+
1338+
Sure, you can probably do the same with the `store.searchResults` property. Our Angular app is reducing to its bare bones.
1339+
Refer to `tag-08-more-store`.
12411340

12421341

12431342

0 commit comments

Comments
 (0)