We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
例如,在 User 组件的模板添加一个 <router-view>:
<router-view>
const User = { template: ` <div class="user"> <h2>User {{ $route.params.id }}</h2> <router-view></router-view> </div> `, };
要在嵌套的出口中渲染组件,需要在 VueRouter 的参数中使用 children 配置:
const router = new VueRouter({ routes: [ { path: "/user/:id", component: User, children: [ { // 当 /user/:id/profile 匹配成功, // UserProfile 会被渲染在 User 的 <router-view> 中 path: "profile", component: UserProfile, }, { // 当 /user/:id/posts 匹配成功 // UserPosts 会被渲染在 User 的 <router-view> 中 path: "posts", component: UserPosts, }, ], }, ], });
vue-router 有 3 种路由模式:hash、history、abstract,对应的源码如下所示:
switch (mode) { case "history": this.history = new HTML5History(this, options.base); break; case "hash": this.history = new HashHistory(this, options.base, this.fallback); break; case "abstract": this.history = new AbstractHistory(this, options.base); break; default: if (process.env.NODE_ENV !== "production") { assert(false, `invalid mode: ${mode}`); } }
其中,3 种路由模式的说明如下:
早期的前端路由的实现就是基于 location.hash 来实现的。
location.hash 的值就是 URL 中 # 后面的内容。比如下面这个网站,它的 location.hash 的值为 '#search':
https://www.word.com#search
hash 路由模式的实现主要是基于下面几个特性:
HTML5 提供了 History API 来实现 URL 的变化。
其中做最主要的 API 有以下两个:history.pushState() 和 history.repalceState()。
history.pushState()
history.repalceState()
这两个 API 可以在不进行刷新的情况下,操作浏览器的历史纪录。
唯一不同的是,前者是新增一个历史记录,后者是直接替换当前的历史记录,如下所示:
window.history.pushState(null, null, path); window.history.replaceState(null, null, path);
history 路由模式的实现主要基于存在下面几个特性:
history.replaceState()
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Vue Router
例如,在 User 组件的模板添加一个
<router-view>
:要在嵌套的出口中渲染组件,需要在 VueRouter 的参数中使用 children 配置:
vue-router 有 3 种路由模式:hash、history、abstract,对应的源码如下所示:
其中,3 种路由模式的说明如下:
vue-router 中常用路由模式实现原理
hash 模式的实现原理
早期的前端路由的实现就是基于 location.hash 来实现的。
location.hash 的值就是 URL 中 # 后面的内容。比如下面这个网站,它的 location.hash 的值为 '#search':
https://www.word.com#search
hash 路由模式的实现主要是基于下面几个特性:
history 模式的实现原理
HTML5 提供了 History API 来实现 URL 的变化。
其中做最主要的 API 有以下两个:
history.pushState()
和history.repalceState()
。这两个 API 可以在不进行刷新的情况下,操作浏览器的历史纪录。
唯一不同的是,前者是新增一个历史记录,后者是直接替换当前的历史记录,如下所示:
history 路由模式的实现主要基于存在下面几个特性:
history.pushState()
或history.replaceState()
不会触发 popstate 事件,这时我们需要手动触发页面跳转(渲染)。学习资料
The text was updated successfully, but these errors were encountered: