{"id":9613,"date":"2018-11-21T10:57:19","date_gmt":"2018-11-21T10:57:19","guid":{"rendered":"https:\/\/blog.fukata.org\/?p=9613"},"modified":"2018-11-21T16:23:49","modified_gmt":"2018-11-21T16:23:49","slug":"vue%e3%81%a7beforeunload%e3%82%92%e6%ad%a3%e3%81%97%e3%81%8f%e5%ae%9f%e8%a3%85%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/blog.fukata.org\/archives\/9613\/","title":{"rendered":"Vue.js\u3067beforeunload\u3092\u6b63\u3057\u304f\u5b9f\u88c5\u3059\u308b"},"content":{"rendered":"
<\/a><\/p>\n \u30c8\u30e9\u30cf<\/a>\u3092nuxtjs\u3067\u66f8\u304d\u63db\u3048\u3066\u591a\u5c11\u30d0\u30b0\u304c\u3042\u308b\u3082\u306e\u306e\u6982\u306d\u3046\u307e\u304f\u52d5\u3044\u3066\u3044\u308b\u3002\u8a18\u4e8b\u306e\u4f5c\u6210\u3001\u7de8\u96c6\u3092\u3059\u308b\u6642\u306b\u30da\u30fc\u30b8\u304b\u3089\u96e2\u8131\u3059\u308b\u969b\u306b\u8b66\u544a\u3092\u51fa\u3057\u3066\u3044\u308b\u304cVue\u3067\u5b9f\u88c5\u3059\u308b\u969b\u306b\u5c11\u3057\u8abf\u3079\u305f\u306e\u3067\u30e1\u30e2\u3057\u3066\u304a\u304f\u3002<\/p>\n <\/p>\n \u73fe\u72b6\u306f\u4e0b\u8a18\u306e\u30b3\u30fc\u30c9\u3092pages\u306b\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3057\u3066\u3044\u308b\u3002 \u4eca\u56de\u4f7f\u7528\u3057\u305f\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u306f\u4e0b\u8a18\u306e\u30da\u30fc\u30b8\u3092\u53c2\u8003\u306b\u3057\u305f\u3002<\/p>\n Navigation Guards | Vue Router<\/a><\/p>\n","protected":false},"excerpt":{"rendered":" \u30c8\u30e9\u30cf\u3092nuxtjs\u3067\u66f8\u304d\u63db\u3048\u3066\u591a\u5c11\u30d0\u30b0\u304c\u3042\u308b\u3082\u306e\u306e\u6982\u306d\u3046\u307e\u304f\u52d5\u3044\u3066\u3044\u308b\u3002\u8a18\u4e8b\u306e\u4f5c\u6210\u3001\u7de8\u96c6\u3092\u3059\u308b\u6642\u306b\u30da\u30fc\u30b8\u304b\u3089\u96e2\u8131\u3059\u308b\u969b\u306b\u8b66\u544a\u3092\u51fa\u3057\u3066\u3044\u308b\u304cVue\u3067\u5b9f\u88c5\u3059\u308b\u969b\u306b\u5c11\u3057\u8abf\u3079\u305f\u306e\u3067\u30e1\u30e2\u3057\u3066\u304a\u304f\u3002 … \u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":2,"featured_media":9223372036854775807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1980],"tags":[2071],"_links":{"self":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/9613"}],"collection":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/comments?post=9613"}],"version-history":[{"count":2,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/9613\/revisions"}],"predecessor-version":[{"id":9615,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/9613\/revisions\/9615"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media\/9223372036854775807"}],"wp:attachment":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media?parent=9613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/categories?post=9613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/tags?post=9613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}beforeRouteLeave<\/code> \u306e\u307f\u3060\u3068\u30ea\u30ed\u30fc\u30c9\u3084\u623b\u308b\u30dc\u30bf\u30f3\u306a\u3069\u306b\u5bfe\u5fdc\u51fa\u6765\u306a\u304b\u3063\u305f\u306e\u3067\u901a\u5e38\u306e
beforeunload<\/code> \u30a4\u30d9\u30f3\u30c8\u3082\u30d5\u30c3\u30af\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u3002<\/p>\n
created() {\n if (process.browser) {\n \/\/ \u30da\u30fc\u30b8\u3092\u6d88\u305d\u3046\u3068\u3057\u305f\u308a\u30ea\u30ed\u30fc\u30c9\u3057\u305f\u5834\u5408\u306a\u3069\u306b\u5bfe\u5fdc\n $(window)\n .off('beforeunload')\n .on('beforeunload', () => {\n if (!this.pageState.saving) {\n return '\u7de8\u96c6\u4e2d\u306e\u30c7\u30fc\u30bf\u304c\u3042\u308a\u307e\u3059\uff01'\n }\n })\n }\n },\n beforeRouteLeave(to, from, next) {\n \/\/ \u30d5\u30a9\u30fc\u30e0\u3092\u9001\u4fe1\u3057\u305f\u5834\u5408\u306a\u3069\u306b\u96e2\u8131\u3068\u307f\u306a\u3055\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u3002\n if (this.pageState.saving) {\n $(window).off('beforeunload')\n return next()\n }\n\n this.$swal({\n title: '\u7de8\u96c6\u4e2d\u306e\u30c7\u30fc\u30bf\u304c\u3042\u308a\u307e\u3059\uff01',\n text: '\u30da\u30fc\u30b8\u3092\u79fb\u52d5\u3059\u308b\u3068\u7de8\u96c6\u4e2d\u306e\u30c7\u30fc\u30bf\u304c\u5931\u304f\u306a\u308a\u307e\u3059\u3002',\n type: 'warning',\n showCancelButton: true,\n confirmButtonColor: '#3085d6',\n cancelButtonColor: '#d33',\n confirmButtonText: '\u306f\u3044\u3001\u79fb\u52d5\u3057\u307e\u3059',\n cancelButtonText: '\u30ad\u30e3\u30f3\u30bb\u30eb'\n }).then(result => {\n if (result.value) {\n $(window).off('beforeunload')\n next()\n } else {\n next(false)\n }\n })\n }\n}\n<\/code><\/pre>\n