Memo
vue Nuxt 共通関数 (inject)
Home
›
Memo
›
Nuxt 共通関数 (inject)
本稿について
本稿はサイト運営者が学んだことなどを記したメモの内容です。
全てが正確な情報とは限りません。ご注意ください。また修正するべき点は適時修正していきます
pluginのディレクトリ下に共通関数を作成する
import axios from 'axios'
const getServer = (path, data) => {
return new Promise((resolve, reject) => {
axios.get(`http://localhost:3000${path}`).then((response) => {
resolve(response)
}).catch((error) => {
reject(error)
})
})
}
const postServer = () => {
}
export default ({}, inject) => {
inject('getServer', getServer);
inject('postServer', postServer);
nuxt.config.jsに設定
plugins: [
'@/plugins/requestServer'
],
以下のような感じで各コンポーネントから利用する
this.$getServer
[参考]
https://qiita.com/itouuuuuuuuu/items/bcad408842c7723e7ca3
injectの中で storeを使う場合はこれを参考にするといいかも(後日試す)
https://github.com/nuxt-community/axios-module/issues/298
関連ページ
コンポーネントの削除
#vue
2020-06-14 18:25:52
イベントハンドリング
#vue
2020-08-31 17:30:54
Nuxt 環境変数
#vue
2020-07-29 10:57:04
Nuxt Firebase AuthでSNSログイン
#vue
2021-03-11 20:56:17
Vue(Javascript)で配列をコピーする
#vue
2020-11-06 21:02:45
Back