广告
返回顶部
首页 > 资讯 > 前端开发 > node.js >vue.js中Vue-router 2.0基础的示例分析
  • 581
分享到

vue.js中Vue-router 2.0基础的示例分析

2024-04-02 19:04:59 581人浏览 安东尼
摘要

这篇文章主要介绍vue.js中Vue-router 2.0基础的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、基础用法:<div id="app

这篇文章主要介绍vue.jsVue-router 2.0基础的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

一、基础用法:

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

二、动态路由匹配:

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

三、嵌套路由:

<div id="app"> 
 <h2>Hello App!</h2> 
 <p> 
  <router-link to="/user/foo">Go to Foo</router-link> 
  <router-link to="/user/foo/profile">Go to profile</router-link> 
  <router-link to="/user/foo/posts">Go to posts</router-link> 
 </p> 
 <router-view></router-view> 
</div> 
 
<template id='user'> 
 <div> 
  <h3>User:{{ $route.params.id }}</h3> 
  <router-view></router-view> 
 </div> 
</template> 
 
<template id="userHome"> 
 <p>主页</p> 
</template> 
 
<template id="userProfile"> 
 <p>概况</p> 
</template> 
 
<template id="userPosts"> 
 <p>登录信息</p> 
</template>
// 1. 定义组件。 
const User = { 
 template:'#user' 
}; 
const UserHome = { 
 template:'#userHome' 
}; 
const UserProfile = { 
 template:'#userProfile' 
}; 
const UserPosts = { 
 template:'#userPosts' 
}; 
// 2. 创建路由实例 
const router = new VueRouter({ 
 routes:[ 
  { path:'/user/:id', component:User, 
   children:[ 
    // 当 /user/:id 匹配成功, 
    // UserHome 会被渲染在 User 的 <router-view> 中 
    { path: '', component: UserHome}, 
    // 当 /user/:id/profile 匹配成功, 
    // UserProfile 会被渲染在 User 的 <router-view> 中 
    { path:'profile', component:UserProfile }, 
    // 当 /user/:id/posts 匹配成功 
    // UserPosts 会被渲染在 User 的 <router-view> 中 
    { path: 'posts', component: UserPosts } 
   ] 
  } 
 ] 
}); 
//3. 创建和挂载根实例 
const app = new Vue({ router:router }).$mount('#app');

四、编程式路由:

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

五、命名路由:

<div id="app"> 
 <h2>Named Routes</h2> 
 <p>Current route name: {{ $route.name }}</p> 
 <ul> 
  <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 class="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>
const Home = { template: '#home' }; 
const Foo = { template: '#foo' }; 
const Bar = { template: '#bar' }; 
 
const router = new VueRouter({ 
 routes: [ 
  { path: '/', name: 'home', component: Home }, 
  { path: '/foo', name: 'foo', component: Foo }, 
  { path: '/bar/:id', name: 'bar', component: Bar } 
 ] 
}); 
 
new Vue({ router:router }).$mount('#app');

六、命名视图:

<div id="app"> 
 <router-link to="/">Go to Foo</router-link> 
 <router-view class="view one"></router-view> 
 <router-view class="view two" name="a"></router-view> 
 <router-view class="view three" 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>
const Foo = { template: '#foo' }; 
const Bar = { template: '#bar' }; 
const Baz = { template: '#baz' }; 
 
const router = new VueRouter({ 
 routes: [ 
  { 
   path: '/', 
   components: { 
    default:Foo, 
    a:Bar, 
    b:Baz 
   } 
  } 
 ] 
}); 
 
new Vue({ router:router }).$mount('#app');

以上是“vue.js中Vue-router 2.0基础的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网node.js频道!

--结束END--

本文标题: vue.js中Vue-router 2.0基础的示例分析

本文链接: https://www.lsjlt.com/news/78458.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • vue.js中Vue-router 2.0基础的示例分析
    这篇文章主要介绍vue.js中Vue-router 2.0基础的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、基础用法:<div id="app...
    99+
    2022-10-19
  • vue-router 2.0 常用基础知识点之router-link的示例分析
    这篇文章给大家分享的是有关vue-router 2.0 常用基础知识点之router-link的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1,$route.param...
    99+
    2022-10-19
  • Vue.js中基础指令的示例分析
    这篇文章给大家分享的是有关Vue.js中基础指令的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Vue.js 是一套构建用户界面的渐进式框架。他自身不是一个全能框架——只...
    99+
    2022-10-19
  • vue中VueRouter路由基础的示例分析
    这篇文章主要介绍vue中VueRouter路由基础的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、VueRouter1、说明用 Vue.js + Vue Router 创建单页应用,感觉很自然:使用 Vu...
    99+
    2023-06-22
  • vue-router路由的示例分析
    这篇文章将为大家详细讲解有关vue-router路由的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。官方文档:旧版:https://github.com/vuej...
    99+
    2022-10-19
  • Java基础的示例分析
    小编给大家分享一下Java基础的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!1、Java主要特点简单性、跨平台性、分布性、安全性、健壮性、平台独立与可移...
    99+
    2023-06-20
  • MySQL基础的示例分析
    这篇文章给大家分享的是有关MySQL基础的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。MySQL入门mySQL (关系型数据库管理系统)MySQL是一个关系型数据库管理...
    99+
    2022-10-18
  • javaScript基础的示例分析
    这篇文章主要为大家展示了“javaScript基础的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“javaScript基础的示例分析”这篇文章吧。首先讲...
    99+
    2022-10-19
  • node.js基础的示例分析
    这篇文章给大家分享的是有关node.js基础的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。什么是NodeJSJS是脚本语言,脚本语言都需要一个解析器才能运行。对于写在H...
    99+
    2022-10-19
  • Three.js基础的示例分析
    这篇文章给大家分享的是有关Three.js基础的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、Three.js官网及使用Three.js必备的三个条件1.Three....
    99+
    2022-10-19
  • Vue.js中vue-resource的示例分析
    小编给大家分享一下Vue.js中vue-resource的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!vue-reso...
    99+
    2022-10-19
  • vue-router初始化的示例分析
    这篇文章主要为大家展示了“vue-router初始化的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“vue-router初始化的示例分析”这篇文章吧。v...
    99+
    2022-10-19
  • vue-router append属性的示例分析
    这篇文章将为大家详细讲解有关vue-router append属性的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。 append 类型: boolean 默...
    99+
    2022-10-19
  • Vue 2.0之内部指令的示例分析
    这篇文章主要介绍了Vue 2.0之内部指令的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1.Vue.js介绍  ...
    99+
    2022-10-19
  • Vue全家桶入门基础的示例分析
    这篇文章主要介绍了Vue全家桶入门基础的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1. Vue概述Vue(读音 /vjuː/,类似于 view) 是一套用于构建用...
    99+
    2023-06-15
  • Vue.js项目API、Router配置拆分的示例分析
    这篇文章主要介绍Vue.js项目API、Router配置拆分的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!前后端分离开发方式前端拥有更高的控制权随着前端框架技术的飞速发展,...
    99+
    2022-10-19
  • mysql中基础知识的示例分析
    这篇文章将为大家详细讲解有关mysql中基础知识的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。mysql架构一、网络连接层客户端连接器(Client Conne...
    99+
    2022-10-18
  • java中基础知识的示例分析
    这篇文章主要为大家展示了“java中基础知识的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“java中基础知识的示例分析”这篇文章吧。一.异常Java对异常的处理同Delphi一样,不是...
    99+
    2023-06-03
  • Vue Router过渡动效的示例分析
    这篇文章给大家分享的是有关Vue Router过渡动效的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Vue的优点Vue具体轻量级框架、简单易学、双向数据绑定、组件化、数据和结构的分离、虚拟DOM、运行速...
    99+
    2023-06-14
  • vue-router实现原理的示例分析
    这篇文章将为大家详细讲解有关vue-router实现原理的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。大致流程可以看成这样:浏览器发出请求服务器监听到80端口(...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作