1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

vue routerのナビゲーションガード機能を利用して外部サイトへリダイレクトする方法 メモ

Posted at
  • 検査目的で外部サイトへのリダイレクト機能を持つモックアプリをVue.jsで作る必要があり、その実現方法を調べた。

実現方法

  • ナビゲーションガード機能のルートガードbeforeEnterwindow.locationを組み合わせる。

ナビゲーションガード

  • 画面遷移(ナビゲーション)を制御するためのフック機能。

  • ルートガードメソッドbeforeEnter

    • ルートの遷移前に実行されるメソッド。
    • ここでwindow.locationで遷移先を指定して、リダイレクトさせる。

window.location

  • 現在表示しているWebページの位置情報を表すプロパティ。
  • 新しい値を代入することで、その値(ページ)に遷移させることができる。

実装

  • router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home
  },
	... ,
  {
    path: '/external_site',
    name: 'external',
    beforeEnter() {
      window.location = "https://www.example.com/top"
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

参考情報

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?