本文共 568 字,大约阅读时间需要 1 分钟。
对于大多数单页面应用,都推荐使用官方支持的 。更多细节可以看 。
如果只需要非常简单的路由而不需要引入整个路由库,可以动态渲染一个页面级的组件像这样:
const NotFound = { template: 'Page not found ' }const Home = { template: 'home page ' }const About = { template: 'about page ' }const routes = { '/': Home, '/about': About}new Vue({ el: '#app', data: { currentRoute: window.location.pathname }, computed: { ViewComponent () { return routes[this.currentRoute] || NotFound } }, render (h) { return h(this.ViewComponent) }}) |
结合 HTML5 History API,你可以建立一个非常基本但功能齐全的客户端路由器。可以直接看
如果有非常喜欢的第三方路由,如 或者 ,整合。这有个用了 Page.js 的。
from:
转载地址:http://tkwrf.baihongyu.com/