adonuxt-template
adonuxt-template copied to clipboard
How to access the database
Greetings,
What is the best way to access the database? I believe using vuex store would be the best way to go but i'm not sure how to access the database within the store.
I would use adonis to do stuff with database.
Then create some api routes that vue/nuxt will call for data. And then you can play with your data. I think that is the only (at least right) way to do it.
http://adonisjs.com/docs/3.2/adonis-blog-part3
for an example a way to get Locale
app\Controllers\Http\LangController.js
'use strict'
const Antl = use('Antl')
class LangController {
async get ({ response }) {
response.json({
lang: Antl.getLocale()
})
}
}
module.exports = LangController
start/routes.js
const Route = use('Route')
Route.get('lang', 'LangController.get')
Route.any('*', 'NuxtController.render')
Then in vue:
<script>
import axios from '~/plugins/axios'
export default {
methods: {
changeLanguage (lang) {
axios().get('/lang').then((response) => {
console.log(response)
}).catch((error) => {
console.log(error)
})
}
}
}
</script>