简体   繁体   English

如何检测页面在vue上刷新?

[英]How to detect page is refreshed on vue?

I try like this:我尝试这样:

created() {
      window.addEventListener('beforeunload', function(event) {
         event.returnValue = 'Write something'
         this.$router.push('/')
      })
    },

the message like this:像这样的消息:

在此处输入图像描述

If I click cancel, it will cancel如果我点击取消,它将取消

If i click reload, it will reload the page.如果我单击重新加载,它将重新加载页面。 it reload to detail page它重新加载到详细信息页面

I want when the user clicks the reload button it will reload the page.我希望当用户单击重新加载按钮时它会重新加载页面。 but reloads the page to the home page但将页面重新加载到主页

how can i do it?我该怎么做?

Update :更新

My router like this:我的路由器是这样的:

import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
...

Vue.use(Router)

export default new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home
    },
    ...
  ]
})

My environment: dev我的环境:开发

My project is using vuetify我的项目正在使用 vuetify

You can add created hook of Vue life cycle to the main.js where new Vue instance is written.您可以将 Vue 生命周期的created钩子添加到编写新 Vue 实例的 main.js 中。

It is the right place, when you refresh the application it again reinitializes the Complete Vue application.这是正确的地方,当您刷新应用程序时,它会再次重新初始化 Complete Vue 应用程序。

Still if you use go back or go forward in browser or如果您在浏览器中使用 go 后退或 go 前进或

this.$router.go(-1)

It is not consider as refresh, the state of vue application is preserved不考虑刷新,vue应用的state被保留

Use like this, here page3 is the browser current page and if you want ton refresh, it prompts a confirm ro reload this site.像这样使用,这里 page3 是浏览器当前页面,如果你想刷新,它会提示确认 ro reload this site。 If Yes, it reloads to home(page1) else it stays in same page如果是,它会重新加载到主页(page1),否则它会留在同一页面

In main.js在 main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'

Vue.config.productionTip = false


new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',
  created() {
    if (performance.navigation.type == 1) {
      if(this.$route.path == '/page3') {
        if (confirm('Reload site?. Change you made may not be saved.')) {
          this.$router.push({path: '/page1'})
        } else {
          console.log('reload page now');
      }
    }          
   }
  }
})

Update code: As per the question, How to detect page from reload, the above logic works fine更新代码:根据问题,如何从重新加载中检测页面,上述逻辑工作正常

But if a user wants to prevent the page from reload and asks for confirmation from user before refresh or reload page, below code is the answer.但是如果用户想要阻止页面重新加载并在刷新或重新加载页面之前要求用户确认,下面的代码就是答案。

The below logic is, if the user is in page3 and trying to refresh or reload the page, it prompts for user confirmation and redirect to home page(page1), if user cancels the refresh, it will not reload and reserve the state of the page下面的逻辑是,如果用户在page3并试图刷新或重新加载页面,它会提示用户确认并重定向到主页(page1),如果用户取消刷新,它不会重新加载并保留state页

In main.js在 main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
Vue.config.productionTip = false


new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',
  created() {
    var self = this;
    window.onbeforeunload = function (e) {
      if(self.$route.path  == "/page3") {
        e = e || window.event;
        //old browsers
        if (e) {e.returnValue = 'Changes you made may not be saved';}
        //safari, chrome(chrome ignores text)
        return 'Changes you made may not be saved';
      } else { 
        return null;
      }
    };
    if (performance.navigation.type == 1) {
      if(this.$route.path == '/page3') {
          this.$router.push({path: '/page1'})
        } else {
          console.log('reload page without redirect');
      }
    }
  }
})

Its because you probably have a SPA.这是因为你可能有一个 SPA。 That means that JavaScript is building up your website with your templates, components etc. Whenever you refresh your page it will always refresh your SPA and lead you back to beginning.这意味着 JavaScript 正在使用您的模板、组件等构建您的网站。每当您刷新页面时,它总是会刷新您的 SPA 并引导您重新开始。 If you want to stay on the same site you should take a look about vue.js router如果你想留在同一个网站上,你应该看看 vue.js 路由器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM