简体   繁体   English

Vue路由器和动态路由:不起作用

[英]Vue-router and dynamic routes: not working

I am using the next route: 我正在使用下一条路线:

{
  path: '/test/:name',
  name: 'Test',
  component: Test,
  // props: true,
  secure: false
}

When I'm trying to access the route http://myapp/test/helloworld in the Vue extension for the Chrome I see that route is not matched. 当我尝试访问Chrome的Vue扩展中的路由http:// myapp / test / helloworld时 ,发现该路由不匹配。 But while testing I found that route http://myapp/test/:name (yeah, with the colon and the param name) the component is loaded. 但是在测试时,我发现路由http:// myapp / test /:name (是的,带有冒号和参数名称)已加载该组件。 The component is anonymous and simple: 该组件是匿名且简单的:

let Test = { template: '<div>sd {{ $route.params.name }}</div>' }

Here a working example doing the navigation with Programmatic and Declarative ways, check if you are missing something. 这是一个使用编程和声明方式进行导航的工作示例,检查是否缺少某些内容。

Also if you do the navigation via JS, in order to pass the params to the router.push(), you need to ref the component via name prop, not the path 另外,如果您通过JS进行导航,为了将参数传递给router.push(),则需要通过名称prop而不是路径来引用组件。

 const Test = { template: `<div> {{ $route.params.name }}</div>` } const router = new VueRouter({ routes: [ { path: '/test/:name', component: Test, name:'test' } ] }) const app = new Vue({ router, methods: { fromJS () { this._router.push({ name: 'test', params: { name: 'doe' }}) } } }).$mount('#app') 
 <script src="https://unpkg.com/vue/dist/vue.js"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> <div id="app"> <p> <router-link to="/test/john">John</router-link> <router-link to="/test/doe">Doe</router-link> <button @click="fromJS">To doe from JS</button> </p> <router-view></router-view> </div> 

Sorry, it was my mistake. 抱歉,这是我的错误。 I got the next snippet in the bottom of router js file: 我在路由器js文件的底部获得了下一个代码段:

router.beforeEach((to, from, next) => {
  router.options.routes.forEach((value, key) => {
    if (value.path.localeCompare(to.path) === 0) {
      if (value.secure === false || store.getters.getLogInStatus === true) {
        next()
      } else if (value.secure === true && store.getters.getLogInStatus === false) {
        router.push({
          'name': 'Login'
        })
      }
    }
  })
})

Here the third line is where mistake happens because /test/:name is not match /test/anyname. 第三行是发生错误的地方,因为/ test /:name与/ test / anyname不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM