Load notes from local json file.

This commit is contained in:
Lukáš Kucharczyk 2020-09-08 19:02:21 +02:00
parent 6aa5105c48
commit f123001228
5 changed files with 28 additions and 8 deletions

8
db.json Normal file
View File

@ -0,0 +1,8 @@
{
"notes": [
{
"id": 1,
"text": "This note is from the db.json file!"
}
]
}

11
package-lock.json generated
View File

@ -2616,6 +2616,14 @@
"integrity": "sha512-zg7Hz2k5lI8kb7U32998pRRFin7zJlkfezGJjUc2heaD4Pw2wObakCDVzkKztTm/Ln7eiVvYsjqak0Ed4LkMDA==", "integrity": "sha512-zg7Hz2k5lI8kb7U32998pRRFin7zJlkfezGJjUc2heaD4Pw2wObakCDVzkKztTm/Ln7eiVvYsjqak0Ed4LkMDA==",
"dev": true "dev": true
}, },
"axios": {
"version": "0.20.0",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.20.0.tgz",
"integrity": "sha512-ANA4rr2BDcmmAQLOKft2fufrtuvlqR+cXNNinUmvfeSNCOF98PZL+7M/v1zIdGo7OLjEA9J2gXJL+j4zGsl0bA==",
"requires": {
"follow-redirects": "^1.10.0"
}
},
"babel-eslint": { "babel-eslint": {
"version": "10.1.0", "version": "10.1.0",
"resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-10.1.0.tgz", "resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-10.1.0.tgz",
@ -5621,8 +5629,7 @@
"follow-redirects": { "follow-redirects": {
"version": "1.13.0", "version": "1.13.0",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz",
"integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA==", "integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA=="
"dev": true
}, },
"for-in": { "for-in": {
"version": "1.0.2", "version": "1.0.2",

View File

@ -8,6 +8,7 @@
"lint": "vue-cli-service lint" "lint": "vue-cli-service lint"
}, },
"dependencies": { "dependencies": {
"axios": "^0.20.0",
"core-js": "^3.6.5", "core-js": "^3.6.5",
"vue": "^3.0.0-rc.10", "vue": "^3.0.0-rc.10",
"vuex": "^4.0.0-0" "vuex": "^4.0.0-0"

View File

@ -82,6 +82,7 @@ export default {
break break
} }
}) })
this.$store.commit('FETCH_NOTES')
} }
} }
</script> </script>

View File

@ -1,4 +1,5 @@
import { createStore } from 'vuex' import { createStore } from 'vuex'
import axios from 'axios'
export default createStore({ export default createStore({
state() { state() {
@ -8,12 +9,14 @@ export default createStore({
}, },
mutations: { mutations: {
FETCH_NOTES(state) { FETCH_NOTES(state) {
state.notes = [ axios
{ .get('http://localhost:3000/notes')
id: 1, .then(response => {
text: 'Note loaded via mutation from Vuex!' state.notes = response.data
} })
] .catch(error => {
console.log('There was an error:', error.response)
})
}, },
ADD_NOTE(state, note) { ADD_NOTE(state, note) {
state.notes.push(note) state.notes.push(note)