带有参数的 vue-router 路由在页面重新加载时不起作用

raf*_*fff 8 vue.js vue-router

我在我的项目中使用 vue-router。

我能够完美地导航到我的命名路线。我唯一的问题是当我使用需要参数的命名路由时,刷新页面时它不会加载。

这是我的路线:

'/example/:username': {
    name: 'example-profile',
    title: 'Example Profile',
    component: ExampleComponent
}
Run Code Online (Sandbox Code Playgroud)

这就是我使用的方式vue-router route

<a v-link="{ name: 'example-profile', params: { username: raaaaf } }">
    Example Link
</a>
Run Code Online (Sandbox Code Playgroud)

当我选择时,Example Link我得到mydomain.com/example/raaaaf.

在第一次加载时,它呈现正确的模板,但是当我刷新或手动输入地址栏上的链接时,我被重定向到我的Page Not Found页面,并且不会触发创建页面时调用的方法。

这是我的ExampleComponent

    <template>
    <div class="small-12 left">
        <side-bar></side-bar>
        <div class="store-right">
            <store-details></store-details>
            <store-menu></store-menu>
            <store-listings></store-listings>
        </div>
    </div>
    </template>
    
    <script>
        export default {
            data() {
                return {
                    username: null,
                    user: null,
                }
            },
    
            created() {
                this.getUser()
            },
    
            methods: {
                getUser() {
                    console.log(this.$route.params);
                }
            }
        }
    </script>
Run Code Online (Sandbox Code Playgroud)

Mik*_*xle 8

我不知道其他人是否面临同样的问题,但我在刷新时获取路由参数时遇到问题。我试图获取的路由参数是一个 ID 号,我使用该 ID 来获取数据并填充页面。我发现(通过许多控制台日志)当我刷新时,数字变成了一个字符串,这就是页面不工作的原因。我整理了它,但在使用它之前将 ID 转换为数字:

Number($route.params.id)
Run Code Online (Sandbox Code Playgroud)


Bil*_*ell 6

您需要正确配置您的服务器。您的服务器正在寻找/example/raaaaf目录中的索引文件。我仔细阅读了这个页面:http : //router.vuejs.org/en/essentials/history-mode.html