1. <div id="app">
  2. <h1>Hello App!</h1>
  3. <p>
  4. <!-- 使用 router-link 组件来导航. -->
  5. <!-- 通过传入 `to` 属性指定链接. -->
  6. <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
  7. <router-link to="/foo">Go to Foo</router-link>
  8. <router-link to="/bar">Go to Bar</router-link>
  9. </p>
  10. <!-- 路由出口 -->
  11. <!-- 路由匹配到的组件将渲染在这里 -->
  12. <router-view></router-view>
  13. </div>
  14. <template id='foo'>
  15. <p>this is foo!</p>
  16. </template>
  17. <template id='bar'>
  18. <p>this is bar!</p>
  19. </template>
  1. // 1. 定义(路由)组件。
  2. // 可以从其他文件 import 进来
  3. const Foo = { template:'#foo' };
  4. const Bar = { template:'#bar' };
  5. // 2. 定义路由
  6. // 每个路由应该映射一个组件。 其中"component" 可以是
  7. // 通过 Vue.extend() 创建的组件构造器,
  8. // 或者,只是一个组件配置对象。
  9. const routes = [
  10. { path: '/foo', component: Foo },
  11. { path: '/bar', component: Bar }
  12. ];
  13. // 3. 创建 router 实例,然后传 `routes` 配置
  14. // 你还可以传别的配置参数, 不过先这么简单着吧。
  15. const router = new VueRouter({ routes:routes });
  16. // 4. 创建和挂载根实例。
  17. // 记得要通过 router 配置参数注入路由,
  18. // 从而让整个应用都有路由功能
  19. const app = new Vue({ router:router }).$mount('#app');

2)动态路由匹配:

  1. <div id="app">
  2. <h1>Hello App!</h1>
  3. <p>
  4. <router-link to="/user/foo/post/123">Go to Foo</router-link>
  5. <router-link to="/user/bar/post/456">Go to Bar</router-link>
  6. </p>
  7. <router-view></router-view>
  8. </div>
  9. <template id='user'>
  10. <p>User:{{ $route.params.id }},Post:{{$route.params.post_id}}</p>
  11. </template>
  1. // 1. 定义组件。
  2. const User = {
  3. template:'#user',
  4. watch:{
  5. '$route'(to,from){
  6. console.log('从'+from.params.id+'到'+to.params.id);
  7. }
  8. }
  9. };
  10. // 2. 创建路由实例 (可设置多段路径参数)
  11. const router = new VueRouter({
  12. routes:[
  13. { path:'/user/:id/post/:post_id',component:User }
  14. ]
  15. });
  16. //3. 创建和挂载根实例
  17. const app = new Vue({ router:router }).$mount('#app');

3)嵌套路由:

  1. <div id="app">
  2. <h1>Hello App!</h1>
  3. <p>
  4. <router-link to="/user/foo">Go to Foo</router-link>
  5. <router-link to="/user/foo/profile">Go to profile</router-link>
  6. <router-link to="/user/foo/posts">Go to posts</router-link>
  7. </p>
  8. <router-view></router-view>
  9. </div>
  10. <template id='user'>
  11. <div>
  12. <h2>User:{{ $route.params.id }}</h2>
  13. <router-view></router-view>
  14. </div>
  15. </template>
  16. <template id="userHome">
  17. <p>主页</p>
  18. </template>
  19. <template id="userProfile">
  20. <p>概况</p>
  21. </template>
  22. <template id="userPosts">
  23. <p>登录信息</p>
  24. </template>
  1. // 1. 定义组件。
  2. const User = {
  3. template:'#user'
  4. };
  5. const UserHome = {
  6. template:'#userHome'
  7. };
  8. const UserProfile = {
  9. template:'#userProfile'
  10. };
  11. const UserPosts = {
  12. template:'#userPosts'
  13. };
  14. // 2. 创建路由实例
  15. const router = new VueRouter({
  16. routes:[
  17. { path:'/user/:id', component:User,
  18. children:[
  19. // 当 /user/:id 匹配成功,
  20. // UserHome 会被渲染在 User 的 <router-view> 中
  21. { path: '', component: UserHome},
  22. // 当 /user/:id/profile 匹配成功,
  23. // UserProfile 会被渲染在 User 的 <router-view> 中
  24. { path:'profile', component:UserProfile },
  25. // 当 /user/:id/posts 匹配成功
  26. // UserPosts 会被渲染在 User 的 <router-view> 中
  27. { path: 'posts', component: UserPosts }
  28. ]
  29. }
  30. ]
  31. });
  32. //3. 创建和挂载根实例
  33. const app = new Vue({ router:router }).$mount('#app');

4)编程式路由:

  1. <div id="app">
  2. <h1>Hello App!</h1>
  3. <p>
  4. <router-link to="/user/foo">Go to Foo</router-link>
  5. </p>
  6. <router-view></router-view>
  7. </div>
  8. <template id='user'>
  9. <h2>User:{{ $route.params.id }}</h2>
  10. </template>
  11. <template id="register">
  12. <p>注册</p>
  13. </template>
  1. // 1. 定义组件。
  2. const User = {
  3. template:'#user'
  4. };
  5. const Register = {
  6. template:'#register'
  7. };
  8. // 2. 创建路由实例
  9. const router = new VueRouter({
  10. routes:[
  11. { path:'/user/:id', component:User },
  12. { path:'/register', component:Register }
  13. ]
  14. });
  15. //3. 创建和挂载根实例
  16. const app = new Vue({ router:router }).$mount('#app');
  17. //4.router.push(location)
  18. router.push({ path: 'register', query: { plan: 'private' }});

5)命名路由:

  1. <div id="app">
  2. <h1>Named Routes</h1>
  3. <p>Current route name: {{ $route.name }}</p>
  4. <ul>
  5. <li><router-link :to="{ name: 'home' }">home</router-link></li>
  6. <li><router-link :to="{ name: 'foo' }">foo</router-link></li>
  7. <li><router-link :to="{ name: 'bar', params: { id: 123 }}">bar</router-link></li>
  8. </ul>
  9. <router-view class="view"></router-view>
  10. </div>
  11. <template id='home'>
  12. <div>This is Home</div>
  13. </template>
  14. <template id='foo'>
  15. <div>This is Foo</div>
  16. </template>
  17. <template id='bar'>
  18. <div>This is Bar {{ $route.params.id }}</div>
  19. </template>
  1. const Home = { template: '#home' };
  2. const Foo = { template: '#foo' };
  3. const Bar = { template: '#bar' };
  4. const router = new VueRouter({
  5. routes: [
  6. { path: '/', name: 'home', component: Home },
  7. { path: '/foo', name: 'foo', component: Foo },
  8. { path: '/bar/:id', name: 'bar', component: Bar }
  9. ]
  10. });
  11. new Vue({ router:router }).$mount('#app');

6)命名视图:

  1. <div id="app">
  2. <router-link to="/">Go to Foo</router-link>
  3. <router-view class="view one"></router-view>
  4. <router-view class="view two" name="a"></router-view>
  5. <router-view class="view three" name="b"></router-view>
  6. </div>
  7. <template id='foo'>
  8. <div>This is Foo</div>
  9. </template>
  10. <template id='bar'>
  11. <div>This is Bar {{ $route.params.id }}</div>
  12. </template>
  13. <template id='baz'>
  14. <div>This is baz</div>
  15. </template>
  1. const Foo = { template: '#foo' };
  2. const Bar = { template: '#bar' };
  3. const Baz = { template: '#baz' };
  4. const router = new VueRouter({
  5. routes: [
  6. {
  7. path: '/',
  8. components: {
  9. default:Foo,
  10. a:Bar,
  11. b:Baz
  12. }
  13. }
  14. ]
  15. });
  16. new Vue({ router:router }).$mount('#app');

vue router 几种方式对比 (转载)的更多相关文章

  1. Linux上删除大量文件几种方式对比

    目录 Linux上删除大量文件几种方式对比 1. rm删除:因为文件数量太多,rm无法删除(报错) 2. find查找删除:-exec 3. find查找删除:xargs 4. find调用-dele ...

  2. Android自动化测试中AccessibilityService获取控件信息(2)-三种方式对比

    Android自动化测试中AccessibilityService获取控件信息(2)-三种方式对比   上一篇文章: Android自动化测试中AccessibilityService获取控件信息(1 ...

  3. vue.js 三种方式安装--npm安装

    Vue.js是一个构建数据驱动的 web 界面的渐进式框架.     Vue.js 的目标是通过简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易上手,便于与第三方库或既有项目整合.     ...

  4. 每日技术总结:vue router传参方式,js获取设备高度

    今天貌似没什么问题,23333…… 1.vue router 路由传参的方式 应用情景:从分类页(category.vue)进入商品列表页(list.vue),需要传递商品分类id(catId),商品 ...

  5. Spark读写Hbase的二种方式对比

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 一.传统方式 这种方式就是常用的TableInputFormat和TableOutputForm ...

  6. ASP.NET页面之间传递值的几种方式(转载)

    页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有QueryString,Session,Cookie ...

  7. Windows上安装配置SSH教程(7)——几种方式对比

    服务端:Windows XP 客户端:Windows 10 由于Cygwin也可以安装OpenSSH,所以客户端其实可以直接使用Cygwin安装OpenSSH,那么在Windows下使用SCP(安全拷 ...

  8. 在C#中,Json的序列化和反序列化的几种方式总结 转载

    转载自  https://www.cnblogs.com/caofangsheng/p/5687994.html    谢谢 在这篇文章中,我们将会学到如何使用C#,来序列化对象成为Json格式的数据 ...

  9. vue.js 三种方式安装

    Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手 ...

随机推荐

  1. JavaScript总结(七)

    JavaScript表单编程 表单是Web上与用户进行交互的主要界面.则我们需要掌握如何访问用户输入的表单数据,校验用户输入的正确性显得至关重要. ♞ 对Form元素进行脚本编写 ✍ 获取表单的应用 ...

  2. xgboost: 速度快效果好的boosting模型

    转自:http://cos.name/2015/03/xgboost/ 本文作者:何通,SupStat Inc(总部在纽约,中国分部为北京数博思达信息科技有限公司)数据科学家,加拿大Simon Fra ...

  3. spark-sql用hive表格,在spark-submit运行jar包时遇到的问题

    1.编程时无法加载hive包,需要在编译好的spark(用spark-shell启动,用spark-sql能够直接访问hive表)的lib目录下,考出assembly包,为其创建一个maven的rep ...

  4. python爬虫之urllib库介绍

    一.urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urllib. ...

  5. Linux下的man page使用守则

    文章链接:https://blog.csdn.net/qq_38646470/article/details/80147650

  6. [PLC]ST语言七:MOV_SMOV_CML_BMOV_FMOV_XCH_BCD_BIN

    一:MOV/SMOV/CML/BMOV/FMOV/XCH/BCD/BIN 说明:简单的顺控指令不做其他说明. (MOV)控制要求:无 (MOV)编程梯形图: (MOV)结构化编程ST语言: (*传送指 ...

  7. oracle vm virtualbox 保存虚拟系统,重装后使用

    直接将VirtualBox VMs 文件夹打包,然后在新系统中装好virtualbox 解压virtualBox VMs ,在系统文件夹中找到 .vbox结尾的文件,点击, 就可以看到熟悉的系统出现在 ...

  8. [codeForce-1006C]-Three Parts of the Array (简单题)

    You are given an array d1,d2,…,dnd1,d2,…,dn consisting of nn integer numbers. Your task is to split ...

  9. 富文本(wangEditor框架)的使用教程

    富文本的使用教程(wangEditor框架) 一,相信很多人用过很多富文本的框架,现在我向大家推荐一个很实用的一个富文本框架,具有丰富的功能项,容易使用. 所以本博客介绍这个富文本编辑器的使用哈!觉得 ...

  10. Tomcat之初识初体验

    1.what's this? Stable performance, free Java web application server! 相关: Java,Javac,JVM,JRE,JDK,Java ...