【问题标题】:Add trailing slash to the every route path of the project in nuxt.js在 nuxt.js 中为项目的每个路由路径添加斜杠
【发布时间】:2018-11-23 08:20:06
【问题描述】:

nuxt.js 项目中有 SEO 优化的需求。它要求项目内的每条路线都需要添加一个斜杠。如何解决问题?

顺便说一句,有一个关于它的问题,但接受的答案不适合我。 the question link is here:

【问题讨论】:

    标签: vue-router nuxt.js


    【解决方案1】:

    嗨 :) 您需要安装 @nuxtjs/redirect-module 并将以下规则添加到 nuxt.config.js

    redirect: [
        {
            from: '^.*(?<!\/)$',
            to: (from, req) => req.url + '/'
        }
    ]
    

    我的回答是基于antonku's的回答。

    【讨论】:

    • 嗨,它工作得很好,但前提是请求来自浏览器地址栏。当我单击页面中的链接时,规则未按预期工作。工作模式是通用的。你有什么想法吗?
    • 哦,是的 :) 很简单,只需自己添加斜杠或在 nuxt.config.js 中设置 router: { trailingSlash: true } : ) 这仅适用于地址栏的情况,因为请求会通过服务器。点击页面是javascript路由器行为。更多信息在这里:nuxt docs
    • 谢谢。实际上我尝试了路由器对象中的斜杠属性。但是当我将此属性设置为 true 时,其他路由不起作用。例如; domain.com/contact/ 不起作用。我还在想办法解决这个问题。
    猜你喜欢
    • 2021-09-15
    • 2020-08-17
    • 2014-05-05
    • 1970-01-01
    • 2019-02-21
    • 2011-07-24
    相关资源
    最近更新 更多