LoginSignup
1
0

More than 3 years have passed since last update.

vue(Nuxt.js)で環境ごとにURLのリンク先を変える方法

Last updated at Posted at 2020-07-14

vue(Nuxt.js)で環境ごとにURLのリンク先を変える方法です。

<template>
  <div>
    <button @click="toLink">リンク先へ</button>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'

export default Vue.extend({
  methods: {
    toLink() {
      let url = ''
      switch (window.location.hostname) {
        case 'https://dev.demo.com':
          url = 'https://dev.demo.jp'
          break
        case 'https://stg.demo.com':
          url = 'https://stg.demo.jp'
          break
        case 'https://demo.com':
          url = 'https://demo.jp'
          break
        default:
          url = 'https://localhost:3000'
          break
      }
      window.location.href = url
    }
  }
})
</script>

window.location.hostnameで現在ページのURLのホスト名を取得しています。
それをswitchして、環境ごとにリンク先のURLを変えています。

window.location.hrefでページ遷移させることが可能です。

1
0
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
1
0