简体   繁体   English

(Vue.js)具有不同路线的相同组件

[英](Vue.js) Same component with different routes

I would like to use the same component for different routes in a Vue.js application.我想在Vue.js应用程序中为不同的路由使用相同的组件。

I currently have something like this:我目前有这样的事情:


main.js main.js

const routes = [
    { path: '/route-1', name: 'route-1', component: MyComponent },
    { path: '/route-2', name: 'route-2', component: MyComponent },
    { path: '/route-3', name: 'route-3', component: MyComponent },

]

const router = new VueRouter({
    routes
})

myComponent.vue myComponent.vue

<ul>
    <li><router-link to="/route-1">Route 1</router-link></li>
    <li><router-link to="/route-2">Route 2</router-link></li>
    <li><router-link to="/route-3">Route 3</router-link></li>
</ul>

When I type the route manually in the browser, everything is working well, but when I try to navigate between the routes using some of these router-generated-links , nothing happens.当我在浏览器中手动键入路由时,一切运行良好,但是当我尝试使用其中一些路由器生成的链接在这些路由之间导航时,则什么也没有发生。 The route changes but the content is still the same.路由发生变化,但内容仍然相同。 Any idea how I can solve this?知道我该如何解决吗?

Thanks!谢谢!

I would like to use the same component for different routes in a Vue.js application.我想在Vue.js应用程序中为不同的路由使用相同的组件。

I currently have something like this:我目前有这样的事情:


main.js main.js

const routes = [
    { path: '/route-1', name: 'route-1', component: MyComponent },
    { path: '/route-2', name: 'route-2', component: MyComponent },
    { path: '/route-3', name: 'route-3', component: MyComponent },

]

const router = new VueRouter({
    routes
})

myComponent.vue myComponent.vue

<ul>
    <li><router-link to="/route-1">Route 1</router-link></li>
    <li><router-link to="/route-2">Route 2</router-link></li>
    <li><router-link to="/route-3">Route 3</router-link></li>
</ul>

When I type the route manually in the browser, everything is working well, but when I try to navigate between the routes using some of these router-generated-links , nothing happens.当我在浏览器中手动键入路由时,一切运行良好,但是当我尝试使用其中一些路由器生成的链接在这些路由之间导航时,则什么也没有发生。 The route changes but the content is still the same.路由发生变化,但内容仍然相同。 Any idea how I can solve this?知道我该如何解决吗?

Thanks!谢谢!

I would like to use the same component for different routes in a Vue.js application.我想在Vue.js应用程序中为不同的路由使用相同的组件。

I currently have something like this:我目前有这样的事情:


main.js main.js

const routes = [
    { path: '/route-1', name: 'route-1', component: MyComponent },
    { path: '/route-2', name: 'route-2', component: MyComponent },
    { path: '/route-3', name: 'route-3', component: MyComponent },

]

const router = new VueRouter({
    routes
})

myComponent.vue myComponent.vue

<ul>
    <li><router-link to="/route-1">Route 1</router-link></li>
    <li><router-link to="/route-2">Route 2</router-link></li>
    <li><router-link to="/route-3">Route 3</router-link></li>
</ul>

When I type the route manually in the browser, everything is working well, but when I try to navigate between the routes using some of these router-generated-links , nothing happens.当我在浏览器中手动键入路由时,一切运行良好,但是当我尝试使用其中一些路由器生成的链接在这些路由之间导航时,则什么也没有发生。 The route changes but the content is still the same.路由发生变化,但内容仍然相同。 Any idea how I can solve this?知道我该如何解决吗?

Thanks!谢谢!

I would like to use the same component for different routes in a Vue.js application.我想在Vue.js应用程序中为不同的路由使用相同的组件。

I currently have something like this:我目前有这样的事情:


main.js main.js

const routes = [
    { path: '/route-1', name: 'route-1', component: MyComponent },
    { path: '/route-2', name: 'route-2', component: MyComponent },
    { path: '/route-3', name: 'route-3', component: MyComponent },

]

const router = new VueRouter({
    routes
})

myComponent.vue myComponent.vue

<ul>
    <li><router-link to="/route-1">Route 1</router-link></li>
    <li><router-link to="/route-2">Route 2</router-link></li>
    <li><router-link to="/route-3">Route 3</router-link></li>
</ul>

When I type the route manually in the browser, everything is working well, but when I try to navigate between the routes using some of these router-generated-links , nothing happens.当我在浏览器中手动键入路由时,一切运行良好,但是当我尝试使用其中一些路由器生成的链接在这些路由之间导航时,则什么也没有发生。 The route changes but the content is still the same.路由发生变化,但内容仍然相同。 Any idea how I can solve this?知道我该如何解决吗?

Thanks!谢谢!

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

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