【问题标题】:How we can set default route in NUXTJS我们如何在 NUXTJS 中设置默认路由
【发布时间】:2020-11-30 09:31:36
【问题描述】:

我们如何自定义NUXT 路由。目前,我正在使用默认的NUXT 页面路由机制。我想将example.vue 指向默认登录页面,而不是index.vue。我还需要在这些路由上添加身份验证。不幸的是,NUXT 文档并没有很好地帮助我。

【问题讨论】:

    标签: vue.js vuejs2 nuxt.js vue-router


    【解决方案1】:

    查看middleware Property on Nuxt

    你可以编写一个中间件并在你的 index.vue 中调用它:

    middleware: {
      'redirect-to-example'
    }
    

    中间件/redirect-to-example.js

    export default function ({ store, redirect }) {
      // If the user is not authenticated
      if (!store.state.authenticated) {
        return redirect(301, '/example');
      }
    }
    

    您可以找到有关 The Context 的有用信息,以便与 Nuxt 一起玩

    【讨论】:

    • 谢谢,如何设置默认登陆页面
    【解决方案2】:

    要更改登录页面,您可以使用以下pages/index.vue:

    <template>
    </template>
    
    <script>
    
    export default {
      created() {
        this.$router.push('/example')
      },
    }
    </script>
    

    当用户导航到https://localhost:3000 时,路由/projects 将被推送,并且url 将更改为https://localhost:3000/example,这可以看作是内部“重定向”。

    【讨论】:

    • 虽然它可以工作,但索引页面在移动之前仍会加载和显示(短暂)。这是我首先尝试的,当前的索引页面在继续推送之前加载。
    猜你喜欢
    • 2017-01-06
    • 2021-11-21
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 2018-01-14
    • 1970-01-01
    相关资源
    最近更新 更多