【问题标题】:Redirect on startup by the given browser URL通过给定的浏览器 URL 在启动时重定向
【发布时间】:2020-02-05 21:09:37
【问题描述】:

我创建了一个带有基本路径的新 Vue 应用程序。由于该应用程序嵌入到另一个 Vue 应用程序中,因此该应用程序不会在启动时呈现 router-view。如果您想了解更多关于如何或为何将此应用嵌入到另一个应用中,请查看此处:

mount Vue apps into container of main Vue app

以及我自己对这个问题的回答:

https://stackoverflow.com/a/58265830/9945420

启动我的应用程序时,它会呈现除router-view 之外的所有内容。我想在启动时通过给定的浏览器 URL 重定向。这是我的路由器配置:

import Vue from 'vue';
import Router from 'vue-router';

import One from './views/One.vue';
import Two from './views/Two.vue';

Vue.use(Router);

const router = new Router({
    base: '/my-embedded-app/',
    mode: 'history',
    routes: [
        {
            path: '/',
            component: One,
        },
        {
            path: '/two',
            component: Two,
        },
    ],
});

router.replace(router.currentRoute.fullPath);

export default router;

我希望应用在调用.../my-embedded-app/two 时呈现组件Two。不幸的是,router.replace 总是重定向到/(所以浏览器 URL 是.../my-embedded-app/)。我调试了router,发现路径是/,但我希望它是/two

重要提示: 每当 url 为 / 时,我都不想将用户重定向到 /two。我只想在 url 为/two 时呈现视图Two。目前没有。

可能出了什么问题?也许我什至不需要那个重定向,并且可以用更优雅的方式解决问题。

【问题讨论】:

  • 你的意思是,你想在你写的时候渲染组件一 - /my-embedded-app/one 作为 URL 和组件二 - /my-embedded-app/two?如果是,您是否尝试过 - { path: 'two', component: Two, } Removing '/' from '/two'
  • 是的,完全正确。我尝试了你的方法,但后来我收到一条警告,这条路线必须使用前导斜杠。运行应用程序时,这条路线当时不起作用..
  • 你能改变你的路线顺序吗?

标签: javascript vue.js vuejs2 vue-router


【解决方案1】:

更新:jsfiddle

最后发生的是route.currentRoute.fullPath 在路由器准备好之前执行。

【讨论】:

  • 这并没有改变任何东西。 router.currentRoute.fullPath 在调用localhost:3000/apps/my-embedded-app/two 时仍返回/ 而不是/two
  • 试试router.onReady(() => { router.replace(router.currentRoute.fullPath); });
  • 抱歉没用。当我使用 onReady 事件时,我什至收到此错误message: "Navigating to current location ("/subApps/app-one") is not allowed"
  • @hrp8sfH4xQ4 我建议您制作一个 jsfiddle 并准确解释您需要什么,并且可能人们可以为您更改它,我们无法准确重现该问题。
  • 对不起,我无法用小提琴重现这个。 IFrame 容器需要文件服务器。但这是一个用于测试目的的存储库github.com/byteCrunsher/router-startup
【解决方案2】:

这是正常行为,这是单页应用程序的工作方式。 “磅” - 像 # - 符号表示链接不会将应用程序移动到另一个页面。

所以你可以将路由器模式更改为

mode:"hash"

【讨论】:

    【解决方案3】:

    试试beforeEnter。看看下面的代码:

    const router = new Router({
      base: "/my-embedded-app/",
      mode: "history",
      routes: [
        {
          path: "/",
          component: One,
          beforeEnter: (to, from, next) => {
            // Your are free to add whatever logic here.
            // On first visit
            next("/two");
          }
        },
        {
          path: "/two",
          component: Two
        }
      ]
    });
    

    阅读更多关于导航守卫here

    【讨论】:

    • 抱歉,tony19 的回答相同。我不想重定向用户。如果浏览器 url 是 .../two,我想渲染 Two 视图。目前没有
    【解决方案4】:

    你能提供一个 github repo 或一些来测试实际吗?

    否则我的第一个想法是使用带有“空”视图的子路由作为基础,这里是我尝试过的示例。 (在我的情况下工作)

    router.js

    export default new Router({
        mode: 'history',
        base: '/my-embedded-app/',
        routes: [
          {
            path: '/',
            name: 'base',
            component: Base,
            children: [
              {
                path: '',
                name: 'one',
                component: One
              },
              {
                path: 'two',
                name: 'two',
                component: Two
              }
            ]
          }
        ]
    })
    

    Base.vue

    <template>
        <router-view/>
    </template>
    

    One.vue

    <template>
      <div>One</div>
    </template>
    

    Two.vue

    <template>
      <div>Two</div>
    </template>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-04-27
    • 1970-01-01
    相关资源
    最近更新 更多