[Vue.js 3.0] Guide – Scaling Up – Routing

# Routing

# Official Router

For most Single Page Applications, it's recommended to use the officially-supported vue-router library(opens new window). For more details, see vue-router's documentation(opens new window).

# Simple Routing from Scratch

If you only need very simple routing and do not wish to involve a full-featured router library, you can do so by dynamically rendering a page-level component like this:

const { createApp, h } = Vue
const NotFoundComponent = { template: '<p>Page not found</p>' }
const HomeComponent = { template: '<p>Home page</p>' }
const AboutComponent = { template: '<p>About page</p>' }
const routes = {
'/': HomeComponent,
'/about': AboutComponent
}
const SimpleRouter = {
data: () => ({
currentRoute: window.location.pathname
}),
computed: {
CurrentComponent() {
return routes[this.currentRoute] || NotFoundComponent
}
},
render() {
return h(this.CurrentComponent)
}
}
createApp(SimpleRouter).mount('#app')

Combined with the History API(opens new window), you can build a very basic but fully-functional client-side router. To see that in practice, check out this example app(opens new window).

# Integrating 3rd-Party Routers

If there's a 3rd-party router you prefer to use, such as Page.js(opens new window) or Director(opens new window), integration is similarly straightforward(opens new window). Here's a complete example(opens new window) using Page.js.

以上是[Vue.js 3.0] Guide – Scaling Up – Routing的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>