聞きたいこと
WebアプリのフロントをVue.jsで実装しているのですが、コンポーネント全体で使用する変数(定数)の定義場所に困っています。
現状AjaxでAPIサーバーからデータを取得する際に、APIサーバーのホスト名を変数にしていますが、computed
やmethods
の各メソッド内ですべて宣言・代入をしています。
<script> : export default { : methods: { getRowCount (items) { return items.length }, async onChange() { let host if (process.env.NODE_ENV === "development") { host = "localhost" } else { host = "APIサーバー" } this.sessions = await axios .get(`http://${host}/api/hub/${this.selected}/session`) .then(response => response.data) }, async deleteSession (sessionName) { let host if (process.env.NODE_ENV === "development") { host = "localhost" } else { host = "APIサーバー" } await axios .delete(`http://${host}/api/xxx/`, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }) }, }, watch: { months: function (newVal) { this.onChange(newVal); } } } </script>
共通変数を使用している箇所が多くなればなるほど、変更が必要になった際に大変になるので、どこかで一元管理したいと思っています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。