vue路由:vue-router

vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用。vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路径和组件映射起来。传统的页面应用,是用一些超链接来实现页面切换和跳转的。在vue-router单页面应用中,则是路径之间的切换,也就是组件的切换。

下载方式:npm install vue-router

html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue路由</title>
<script src="vue.min.js"></script>
<script src="vue-router.min.js"></script>
</head>
<body>
<div id="box">
<div>
<router-link to="/home">主页</router-link>
<router-link to="/news">新闻</router-link>
<router-link to='/about'>关于</router-link>
</div>
<div>
<router-view></router-view>
</div>
</div>
</body>
</html>

JavaScript:

    <script>
//组件
const Home = {
template:'<h3>我是主页</h3>'
};
const News = {
template:'<h3>我是新闻</h3>'
}
const About = {
template:'<h3>我是关于</h3>'
}
//配置路由
const routes = [
{path:'/home', component :Home},
{path:'/news', component:News},
{path:'/about',component:About},
//重定向
{path:'*',redirect:'/home'}
]
//生成路由实例
const router = new VueRouter({
routes
})
//挂载到vue上
new Vue({
router,
el:'#box'
})
</script>

CSS:

<style>
.router-link-active{
background: #ccc;
padding: 5px;
text-decoration: none;
}
</style>

总体:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue路由</title>
<script src="vue.min.js"></script>
<script src="vue-router.min.js"></script>
<style>
.router-link-active{
background: #ccc;
padding: 5px;
text-decoration: none;
}
</style>
</head>
<body>
<div id="box">
<div>
<router-link to="/home">主页</router-link>
<router-link to="/news">新闻</router-link>
<router-link to='/about'>关于</router-link>
</div>
<div>
<router-view></router-view>
</div>
</div> <script>
//组件
const Home = {
template:'<h3>我是主页</h3>'
};
const News = {
template:'<h3>我是新闻</h3>'
}
const About = {
template:'<h3>我是关于</h3>'
}
//配置路由
const routes = [
{path:'/home', component :Home},
{path:'/news', component:News},
{path:'/about',component:About},
//重定向
{path:'*',redirect:'/home'}
]
//生成路由实例
const router = new VueRouter({
routes
})
//挂载到vue上
new Vue({
router,
el:'#box'
})
</script>
</body>
</html>

vue2.0路由-适合刚接触新手简单理解的更多相关文章

  1. vue2.0路由

    现在用vue-cli搭建的环境里面vue-router是下载好的 vue2.0路由方式和以前也有些不同 没了了map和start方法 目录结构如上图 这里有三个文件,app.vue显示,main.js ...

  2. vue2.0路由写法、传参和嵌套

    前置知识请戳这里 vue-routerCDN地址:https://unpkg.com/vue-router@3.0.1/dist/vue-router.js vue-router下载地址:https: ...

  3. vue2.0 路由学习笔记

    昨天温故了一下vue2.0的路由 做个笔记简单记录一下! 1.首相和vue1.0一样 要使用vuejs的路由功能需要先引入vue-router.js 2.然后修改原有a标签处代码 这里以一个ul li ...

  4. vue2.0路由变化1

    路由的步骤 1.定义组件 var Home={ template:'<h3>我是主页</h3>' }; var News={ template:'<h3>我是新闻& ...

  5. vue2.0路由进阶

    一.路由的模式 第一种用history方式实现,HTML5使用window.history.pushState()实现路由的切换而不刷新页面. 第二种使用hash值的方式来实现. vue2.0两种都可 ...

  6. vue2.0路由-路由嵌套

    vue一个重要的方面就是路由,下面是自己写的一个路由的例子: 1.引入依赖库就不必再说 2.创建组件 两种写法 第一种:间接 <template id="home"> ...

  7. vue2.0 双向绑定原理分析及简单实现

    Vue用了有一段时间了,每当有人问到Vue双向绑定是怎么回事的时候,总是不能给大家解释的很清楚,正好最近有时间把它梳理一下,让自己理解的更清楚,下次有人问我的时候,可以侃侃而谈. 一.首先介绍Obje ...

  8. 解决vue2.0路由 TypeError: Cannot read property 'matched' of undefined 的错误问题

    刚开始使用vue-router2.0,虽然也用了vux,用起来却发现一个问题--具体如下: 正常情况下使用脚手架跑完之后,然后修改源项目,首先在main.js入口里把该import进去的vuex,vu ...

  9. vue2.0路由写法

    // 0. 如果使用模块化机制编程,導入Vue和VueRouter,要调用 Vue.use(VueRouter) // 1. 定义(路由)组件. // 可以从其他文件 import 进来 var Fo ...

随机推荐

  1. 【并查集】BZOJ4551-[Tjoi2016&Heoi2016]树

    NOIP太可怕了((( -口-) 题目链接 [题目大意] 给定一颗有根树(根为1),有以下两种操作: 1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均无标记,而且对于某个结点 ...

  2. 工具使用-----Jmeter的基础用法

    //摘抄至http://www.cnblogs.com/TankXiao/p/4045439.html 以下是我自己录制的关于这篇文章的视频,有兴趣的可以下载哦 https://yunpan.cn/c ...

  3. BZOJ4276 : [ONTAK2015]Bajtman i Okrągły Robin

    建立线段树, S向每个叶子连边,容量1,费用0. 孩子向父亲连边,容量inf,费用0. 每个强盗向T连边,容量1,费用为c[i]. 对应区间内的点向每个强盗,容量1,费用0. 求最大费用流即可. #i ...

  4. 【NOI2005】聪聪和可可 概率与期望 记忆化搜索

    1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1635  Solved: 958[Submit][Statu ...

  5. spring-boot 速成(11) - 单元测试

    一.添加依赖项: testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.2.RELEASE' 二.单元测试代码示例 im ...

  6. ProFTPd Local pr_ctrls_connect Vulnerability - ftpdctl 漏洞及攻击代码分析

    攻击代码网址:http://www.exploit-db.com/exploits/394/ 1.执行环境: 1.ProFTPD 1.3.0/1.3.0a 2.编译ProFTPD时.--enable- ...

  7. Windows XP Manifest in Delphi

    Find out how you can include the manifest into a Delphi project to allow your application to share t ...

  8. javascript:常用数组操作

    concat()方法 数组和数组的 粘结: var a=[1,2,3,4]; var b=[5,6,7,8]; var c=a.concat(b); console.log(c); // [1,2,3 ...

  9. 简述 IOS中的LazyLoad思想

    Lazy,谁懒?当然是计算机偷懒.通常用法,你有一个NSArray的property,但是你不在初始化方法里为其alloc/init,它就只是一个指针,不会占用内存.然后你写了此property的访问 ...

  10. HTML5文件上传qq、百度、taobao等比较(改进支持三种状态提示)

    拖拽过程详解: 1:文件未拖出文件选择框的时候提示:将要上传的文件或文件夹拖拽至此区域 2:文件拖出文件选择框但未拖入上传的文件框提示:请继续拖拽文件或文件夹至此区域 3:文件拖出文件选择框且已拖入上 ...