vue.js - VueJS : router. 解决问题

标签 vue.js vue-router

控制台中显示警告:

the id param was not provided.

文档提到将使用当前路由:

current is the current Route by default (most of the time you don't need to change this)

html代码:

<div id="app">
  <router-link to="/5">/5</router-link>
  <button @click="onClick">Resolve</button>
</div>

js代码:

new Vue({
    el: '#app',

  router: new VueRouter({
    routes: [
        {
        name: 'foo',
        path: '/:id',
        component: {
            template: '<div>Foo <router-view/></div>',
        },
        children: [
            {
            name: 'bar',
            path: 'bar',
            component: {
                template: '<div>Bar</div>'
            }
          }
        ]
      },
    ],
  }),

  methods: {
    onClick() {
      console.log('Omitting current');
        this.$router.resolve({ name: 'bar' });
      console.log('Including current');
        this.$router.resolve({ name: 'bar' }, this.$route);
    }
  }
});

最佳答案

我知道这已经过期了,但我会分享我的发现。

当你调用$router.resolve()时,它使用vue-router/src/util/location.jsnormalizeLocation() 获取 Location 对象。

您可以看到 current? 选项仅用于解析相对路径。

current.params 包含在新的 Location 中的唯一情况是给定的 RawLocation 是相对的,并且包含 参数本身。

关于vue.js - VueJS : router. 解决问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53712990/

相关文章:

vue.js - 模拟安装 Hook Jest 测试单元

vue.js - 基于路由动态插入 CSS 类到导航栏

javascript - 在Vuejs3中如何让sticky header组件在与其他组件相交时改变背景颜色?

javascript - Vuetify 在鼠标悬停时切换数组列表中的数据对象

javascript - v-select 更新 var 而不使用 v-model

vue.js - 视觉 : How to use store with component?

laravel - 使用Vue-Router路由信息生成页面头部

vue.js - 从Vue.js中的某些路由中排除Bootstrap样式

javascript - Vue - 导航到下一个子路线

vue.js - 清除 nuxt.js 构建上的间隔和超时