【问题标题】:Vue router - get 'from' path inside componentVue路由器-在组件内获取“来自”路径
【发布时间】:2020-09-25 04:42:56
【问题描述】:

在组件中获取“from”路径的最佳方法是什么,即不在 router/index.js 中使用 router.beforeEach

我想为路由加载一个组件,并根据用户的来源显示/隐藏 DOM 元素。

console.log(this.$route); 给了我当前路线,但我想要history -1 路线。

【问题讨论】:

  • 你试过用可以观察的观察者观察路线吗 $route(to, from) { // 对路线变化做出反应... }

标签: vue.js vue-router


【解决方案1】:

您可以使用in-component beforeRouteEnter guard

export default {
  name: "MyComponent",
  data: () => ({
    fromPath: ""
  }),
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.fromPath = from.path
    })
  }
}

【讨论】:

    猜你喜欢
    • 2019-12-10
    • 2017-05-29
    • 2018-12-25
    • 1970-01-01
    • 2019-02-13
    • 2016-07-07
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    相关资源
    最近更新 更多