简体   繁体   English

Nuxt - 如何找到以前的路线?

[英]Nuxt - How to find the previous route?

I am able to use this.$router.go(-1) to redirect a user to the previous route.我可以使用this.$router.go(-1)将用户重定向到之前的路由。 However, I am not able to understand how I get get the information about the previous route before redirecting.但是,我无法理解如何在重定向之前获取有关先前路线的信息。

Basically, by first reading what the previous route was, I want to make sure that the previous route was from the same domain and only then redirect to it, otherwise do something else.基本上,通过首先阅读前一条路由是什么,我想确保前一条路由来自同一个域,然后才重定向到它,否则做其他事情。

in nuxt static methods where you have access to nuxt context ex: asyncData or middlewares :在您可以访问 nuxt 上下文的 nuxt 静态方法中, ex: asyncData or middlewares

asyncData({from}){
    // do something with from
}

but if you want to get the prev route in vue instance you can use但是如果你想在 vue 实例中获得 prev 路由,你可以使用

this.$nuxt.context.from

also, remember that from can be undefined if there wasn't any prev page in the browser history另外,请记住,如果浏览器历史记录中没有任何上一页,则from可以是未定义的

In the [middleware] directory you can put this script [routing.js]:在 [middleware] 目录中,您可以放置​​此脚本 [routing.js]:

/* eslint-disable no-undef */
/* eslint-disable no-console */
export default function (context) {
  // current route
  console.log('route=', context.route.name)
  // previous route
  if (process.client) {
    const from = context.from
    console.log('from=', from)
  }
}

In [nuxt.config.js]:在 [nuxt.config.js] 中:

router: {
  ...
  middleware: 'routing'
},

Whenever you change the current page in the client you should see a log showing the previous page.每当您更改客户端中的当前页面时,您应该会看到显示前一页的日志。 Maybe it can help you.也许它可以帮助你。

In your page you can add asyncData hook which have access to context object with from property:在您的页面中,您可以添加asyncData钩子,该钩子可以使用from属性访问context对象:

asyncData({ from }) {
  console.log(from);
}

There is no out of the box way to get this information.没有现成的方式来获取这些信息。

What you can do is attach a beforeRouteEnter as a global guard and save the route before navigating to a new route.您可以做的是附加beforeRouteEnter作为全局守卫并在导航到新路线之前保存路线。

Then you can check to see if there is a previous route saved and execute this.$router.go(-1)然后就可以查看是否有之前保存的路由,执行this.$router.go(-1)

If you are using the router in history mode you could be tempted to use the History api that vue-router is using to get this information.如果您在history模式下使用路由器,您可能会尝试使用 vue-router 用来获取此信息的History api。 But HistoryApi doesn't allow for this as this would be a huge privacy problem.但是 HistoryApi 不允许这样做,因为这将是一个巨大的隐私问题。 You could see the entire history of the user in the current tab.您可以在当前选项卡中查看用户的完整历史记录。

You can achieve this by implementing the Vue Navigation guards, on the component/page.您可以通过在组件/页面上实现 Vue 导航守卫来实现这一点。

<script>
export default {
  beforeRouteEnter(to, from, next) {
    console.log(from)
    next()
  },
  data() {
    return {
      ...
      prevRoute: null,
    }
  }
}
</script>

Or there is this guy https://gist.github.com/zolotyx/b4e7fda234b3572fb7adaf11391f8eef#file-auth-js, he has made a script to help in redirecting或者有这个人https://gist.github.com/zolotyx/b4e7fda234b3572fb7adaf11391f8eef#file-auth-js,他做了一个脚本来帮助重定向

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

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