LoginSignup
5
2

More than 3 years have passed since last update.

Vue.jsで環境変数を設定する

Last updated at Posted at 2020-02-26

やりたいこと

Vue.jsでお天気アプリを作成したのでgithubにあげたらAPIKeyも一緒にあがってしまったので隠したい

手順

プロジェクト直下にenvファイルをつくる

スクリーンショット 2020-02-26 15.22.44.png

変数を定義する

.env
# openweathermap
VUE_APP_API_URL_WEATHER = https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?id=1850147&units=metric&appid=APIキー

# forecast
VUE_APP_API_URL_FORECAST = https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/APIキー/35.6895,139.6917?lang=ja&units=si

呼ぶ

process.env.変数名 で呼び出せる

getWeather() {
    this.$axios
        .get(process.env.VUE_APP_API_URL_WEATHER, {})
        .then(result => {
          this.weather = Weather.create(result);
        })
        .catch(error => {
          console.log(error);
        });
    },

確認

スクリーンショット 2020-02-26 15.41.52.png

ちゃんと表示されました🐰

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2