vue router 几种方式对比 (转载)
- <div id="app">
- <h1>Hello App!</h1>
- <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');
2)动态路由匹配:
- <div id="app">
- <h1>Hello App!</h1>
- <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');
3)嵌套路由:
- <div id="app">
- <h1>Hello App!</h1>
- <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>
- <h2>User:{{ $route.params.id }}</h2>
- <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');
4)编程式路由:
- <div id="app">
- <h1>Hello App!</h1>
- <p>
- <router-link to="/user/foo">Go to Foo</router-link>
- </p>
- <router-view></router-view>
- </div>
- <template id='user'>
- <h2>User:{{ $route.params.id }}</h2>
- </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' }});
5)命名路由:
- <div id="app">
- <h1>Named Routes</h1>
- <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');
6)命名视图:
- <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 router 几种方式对比 (转载)的更多相关文章
- Linux上删除大量文件几种方式对比
目录 Linux上删除大量文件几种方式对比 1. rm删除:因为文件数量太多,rm无法删除(报错) 2. find查找删除:-exec 3. find查找删除:xargs 4. find调用-dele ...
- Android自动化测试中AccessibilityService获取控件信息(2)-三种方式对比
Android自动化测试中AccessibilityService获取控件信息(2)-三种方式对比 上一篇文章: Android自动化测试中AccessibilityService获取控件信息(1 ...
- vue.js 三种方式安装--npm安装
Vue.js是一个构建数据驱动的 web 界面的渐进式框架. Vue.js 的目标是通过简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易上手,便于与第三方库或既有项目整合. ...
- 每日技术总结:vue router传参方式,js获取设备高度
今天貌似没什么问题,23333…… 1.vue router 路由传参的方式 应用情景:从分类页(category.vue)进入商品列表页(list.vue),需要传递商品分类id(catId),商品 ...
- Spark读写Hbase的二种方式对比
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 一.传统方式 这种方式就是常用的TableInputFormat和TableOutputForm ...
- ASP.NET页面之间传递值的几种方式(转载)
页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有QueryString,Session,Cookie ...
- Windows上安装配置SSH教程(7)——几种方式对比
服务端:Windows XP 客户端:Windows 10 由于Cygwin也可以安装OpenSSH,所以客户端其实可以直接使用Cygwin安装OpenSSH,那么在Windows下使用SCP(安全拷 ...
- 在C#中,Json的序列化和反序列化的几种方式总结 转载
转载自 https://www.cnblogs.com/caofangsheng/p/5687994.html 谢谢 在这篇文章中,我们将会学到如何使用C#,来序列化对象成为Json格式的数据 ...
- vue.js 三种方式安装
Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手 ...
随机推荐
- mfc 类成员函数
知识点 类成员变量初值 类的构造函数 类成员函数 类成员函数的位置 一.类成员变量初值 二.类的构造函数 构造函数 是一种特殊的方法,主要用来在创建对象时初始化对象,即为对象成员变量赋初始值. 构造函 ...
- BZOJ2034 【2009国家集训队】最大收益
题面 题解 第一眼:线段树优化连边的裸题 刚准备打,突然发现: \(1 \leq S_i \leq T_i \leq 10^8\) 这™用个鬼的线段树啊 经过一番寻找,在网上找到了一篇论文 大家可以去 ...
- 5285: [Hnoi2018]寻宝游戏
5285: [Hnoi2018]寻宝游戏 链接 分析: 从下面依次确定运算符号,然后在确定的过程中,需要确定的位数会逐渐减少.比如最后有一个1,如果在从下往上确定了一个or 1,那么再往前可以随便选了 ...
- [NOI2016]区间 线段树
[NOI2016]区间 LG传送门 考虑到这题的代价是最长边减最短边,可以先把边按长度排个序,双指针维护一个尺取的过程,如果存在包含某个点的区间数\(\ge m\),就更新答案并把左指针右移,这样做的 ...
- jquery 直接访问图片路径
jQuery("#img").attr("src",function(){return this.src+"?"});
- Object C学习笔记3-对象的使用和定义
1. 如何定义一个对象 在面向对象的语言中,定义一个对象是使用Class关键字,而在Object-C中则是使用@interface,@interface用于定义对象的属性和方法,@implementa ...
- Zabbix实战-简易教程--订阅类
一.需求提出 最近数据中心有一个新的需求,有一批后台任务需要在每天固定时间点运行(凌晨8:00),现在希望能够把这个任务执行的结果定时上报给他. 说明:执行的任务为一个sql查询,查询出来的是每个任务 ...
- 神器 Tmux 的超绝便利
服务器的任务不间断运行,就是利用了 tmux 的特性.就是说,一般 ssh 是断开就会停止所有之前连接 ssh 期间运行的所有 processes,而 tmux 的核心业务不在于把屏幕分成几块好看,而 ...
- 腾讯hr面
腾讯hr面面经 20181018 寒暄几句 hr自我介绍 hr介绍面试和最后出结果的时间周期 进入主题 自我介绍 考研成绩专业第一 聊考研(考研的经过.考研和保研) 本科成绩 考研成绩超长发挥还是正常 ...
- java中JVM的原理
转载:https://blog.csdn.net/witsmakemen/article/details/28600127 一.java虚拟机的生命周期: Java虚拟机的生命周期 一个运行中的Jav ...