Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

如何优雅地用vue-router记录上一次访问页面地路径?

需求是:

用户可能会点击登录 进入登录的组件
登录成功以后我希望用户可以回到登录前的路由并继续操作。

如何优雅地用vue路由处理这个问题?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

可以利用beforeRouteEnter钩子函数来记录进入登陆页之前的页面路径。

beforeRouteEnter(to, from, next) {
  next((vm) => {
    vm.setFromPage(from.path)
  })
},
methods: {
  setFromPage(path) {
    this.fromPage = path
  },
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...