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
でページ遷移させることが可能です。