【问题标题】:vue-router route with params not working on page reload带有参数的vue-router路由在页面重新加载时不起作用
【发布时间】:2017-02-18 10:06:16
【问题描述】:

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

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

这是我的路线:

'/example/:username': {
    name: 'example-profile',
    title: 'Example Profile',
    component: ExampleComponent
}

这就是我使用vue-routerroute的方式:

<a v-link="{ name: 'example-profile', params: { username: raaaaf } }">
    Example Link
</a>

当我选择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>

【问题讨论】:

    标签: vue.js vue-router


    【解决方案1】:

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

    Number($route.params.id)
    

    【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-12-10
      • 2017-10-15
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      • 2017-05-08
      相关资源
      最近更新 更多