Vue 3.0 路由

发布时间:2024-10-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Vue 3.0 路由脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

#官方 Router

对于大多数单页面应用,都推荐使用官方支持的 vue-router 库。更多细节可以移步 vue-router 文档

#从零开始简单的路由

如果你只需要非常简单的路由而不想引入一个功能完整的路由库,可以像这样动态渲染一个页面级的组件:

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 Vue.h(this.CurrentComponent)
  }
}


Vue.createApp(SimpleRouter).mount('#app')

结合 HTML5 History API,你可以建立一个麻雀虽小但是五脏俱全的客户端路由器。可以直接看实例应用

#整合第三方路由

如果你有更偏爱的第三方路由,如 Page.js 或者 Director,整合起来也一样简单。这里有一个使用了 Page.js 的完整示例

脚本宝典总结

以上是脚本宝典为你收集整理的Vue 3.0 路由全部内容,希望文章能够帮你解决Vue 3.0 路由所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。