18720358503
4000-399-004
发表日期:2021-01-12 11:27文章编辑:jianzhan浏览次数: 标签:
前言
Vue.js的一大特色就是构建单页面应用十分方便,既然要方便构建单页面应用那么自然少不了路由,vue-router就是vue官方提供的一个路由框架。本文主要介绍了Vue-router 2.0的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:
一、基础用法:
div id="app" h1 Hello App! /h1 !-- 使用 router-link 组件来导航. -- !-- 通过传入 `to` 属性指定链接. -- !-- router-link 默认会被渲染成一个 ` a ` 标签 -- router-link to="/foo" Go to Foo /router-link router-link to="/bar" Go to Bar /router-link !-- 路由出口 -- !-- 路由匹配到的组件将渲染在这里 -- router-view /router-view /div template id='foo' p this is foo! /p /template template id='bar' p this is bar! /p /template
二、动态路由匹配:
div id="app" h1 Hello App! /h1 router-link to="/user/foo/post/123" Go to Foo /router-link router-link to="/user/bar/post/456" Go to Bar /router-link router-view /router-view /div template id='user' p User:{{ $route.params.id }},Post:{{$route.params.post_id}} /p /template
四、编程式路由:
div id="app" h1 Hello App! /h1 router-link to="/user/foo" Go to Foo /router-link router-view /router-view /div template id='user' h2 User:{{ $route.params.id }} /h2 /template template id="register" p 注册 /p /template
五、命名路由:
div id="app" h1 Named Routes /h1 p Current route name: {{ $route.name }} /p li router-link :to="{ name: 'home' }" home /router-link /li li router-link :to="{ name: 'foo' }" foo /router-link /li li router-link :to="{ name: 'bar', params: { id: 123 }}" bar /router-link /li /ul router-view /router-view /div template id='home' div This is Home /div /template template id='foo' div This is Foo /div /template template id='bar' div This is Bar {{ $route.params.id }} /div /template
六、命名视图:
div id="app" router-link to="/" Go to Foo /router-link router-view /router-view router-view name="a" /router-view router-view name="b" /router-view /div template id='foo' div This is Foo /div /template template id='bar' div This is Bar {{ $route.params.id }} /div /template template id='baz' div This is baz /div /template
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对凡科的支持。