vue 之 vue-router
// 0. 如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter) // 1. 定义(路由)组件。
// 可以从其他文件 import 进来
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' } // 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
] // 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
routes // (缩写)相当于 routes: routes
}) // 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
router
}).$mount('#app') // 现在,应用已经启动了!
开始
通过注入路由器,我们可以在任何组件内通过 this.$router 访问路由器,也可以通过 this.$route 访问当前路由:
// Home.vue
export default {
computed: {
username () {
// 我们很快就会看到 `params` 是什么
return this.$route.params.username
}
},
methods: {
goBack () {
window.history.length > 1
? this.$router.go(-1)
: this.$router.push('/')
}
}
}
Home.vue
1:先下载路由
npm install vue-router --save
2:导入
import VueRouter from "vue-router"
// 定义(路由)组件。 3:
如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter)
Vue.use(VueRouter)
1定制路由(组件)
// 导入路由组件
import Index from './Index'
import Luffy from './Luffy' // 2、创建 router 实例,然后传 `routes` 配置 const router = new VueRouter({
mode: 'history',
routes:[
{ path: '/', component: Index },
{ path: '/luffy', component: Luffy }
]
}) 3
new Vue({
el: '#app',
router, // 挂载router实例
render: h => h(App)
}) 4,在主主件写出口
<!-- 路由出口 所有路由匹配的组件都会被渲染到这里 -->
<router-view></router-view> 5:a标签要换成router-link
<router-link v-for='(item,index) in urls' :to="item.href" :class='{active:currentIndex==index}' @click.native='clickHandler(index)' >{{item.name}}</router-link> <!-- 给router-link添加事件 会阻止click事件的触发,需要加上.navtive就可以了。加上.navtive 才是真正点击了a标签的click事件,在组件中不加.native 不会触发原生的事件。注意了注意了 -->
<!-- <router-link to="/luffy">路飞学城</router-link> -->
主主件
<template>
<div id="app">
<img src="./assets/logo.png">
<h1>{{ msg }}</h1>
<h2>Essential Links</h2>
<ul> <router-link v-for='(item,index) in urls' :to="item.href" :class='{active:currentIndex==index}' @click.native='clickHandler(index)' >{{item.name}}</router-link> <!-- 给router-link添加事件 会阻止click事件的触发,需要加上.navtive就可以了。加上.navtive 才是真正点击了a标签的click事件,在组件中不加.native 不会触发原生的事件。注意了注意了 -->
<!-- <router-link to="/luffy">路飞学城</router-link> --> </ul> <!-- 路由出口 所有路由匹配的组件都会被渲染到这里 -->
<router-view></router-view> </div>
</template> <script>
export default {
name: 'app',
data () {
return {
msg: 'Welcome to Your Vue.js App',
urls:[
{href:'/',name:"首页"},
{href:'/luffy',name:"路飞学城"} ],
currentIndex:0
}
},
methods:{
clickHandler(index){
console.log(index)
this.currentIndex = index; }
}
}
</script> <style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
} h1, h2 {
font-weight: normal;
} ul {
list-style-type: none;
padding: 0;
} li {
display: inline-block;
margin: 0 10px;
} a {
color: #42b983;
}
a.active{
color: yellow;
}
</style>
app。vue
子主件
<template>
<div class="luffy">
<h4>我是路飞</h4>
</div>
</template>
<script>
export default{
name:'luffy',
data(){
return { }
}
}
</script>
<style> </style>
luffy.vue
<template>
<div class="index">
<h3>我是首页</h3>
</div>
</template>
<script>
export default{
name:'index',
data(){
return { }
}
}
</script>
<style> </style>
index。vue
vue 之 vue-router的更多相关文章
- Vue 组件之 Router
Vue 组件之 Router Vue 开发单页应用的时候,免不了使用Vue组件.在单页应用上如何进行组件切换? 结构如下图所示: 主页面包含Foo组件与Bar组件,在主页面中可以进行Foo与 Bar的 ...
- vue路由请求 router
创建一个Router.js文件 // 路由请求//声明一个常量设置路菜单// import Vue from "vue/types/index";import Vue from ' ...
- vue学习之router
路由文档:https://router.vuejs.org/zh/guide/ 使用vue做spa应用的话,一定会涉及到路由. 安装 安装router插件 npm install vue-router ...
- vue学习之用 Vue.js + Vue Router 创建单页应用的几个步骤
通过vue学习一:新建或打开vue项目,创建好项目后,接下来的操作为: src目录重新规划——>新建几个页面——>配置这几个页面的路由——>给根实例注入路由配置 src目录重整 在项 ...
- 六、vue路由Vue Router
一.基本概念 route, routes, router 1, route,它是一条路由,由这个英文单词也可以看出来,它是单数, Home按钮 => home内容, 这是一条route, a ...
- vue +ts 在router的路由中import报错的解决方案
在router.ts中引入.vue文件,会提示打不到module,但是编译可能成功,运行也不报错 找了好久,发现了这个答案 https://segmentfault.com/a/11900000167 ...
- vue项目中router路由配置
介绍 路由:控制组件之间的跳转,不会实现请求.不用页面刷新,直接跳转-切换组件>>> 安装 本地环境安装路由插件vue-router: cnpm install vue-rou ...
- Vue的路由Router之导航钩子和元数据及匹配
一.文件结构 二.vue.js 打开此链接 https://cdn.bootcss.com/vue/2.6.10/vue.js 复制粘贴页面的所有内容 三.vue-router.js 打开此链接 h ...
- vue路由--使用router.push进行路由跳转
手机赚钱怎么赚,给大家推荐一个手机赚钱APP汇总平台:手指乐(http://www.szhile.com/),辛苦搬砖之余用闲余时间动动手指,就可以日赚数百元 route-link是在html中静态定 ...
- vue全家桶router、vuex、axios
main.js import Vue from 'vue' import App from './App' import router from './router' import store fro ...
随机推荐
- Spring容器基础xmlbeanfactory(一起看源码)
在spring中,如果你想创建容器少不了使用常见的xmlbeanfactory,ClassPathXmlApplicationContext,FileSystemXmlApplicationConte ...
- JAVA多线程本质分析
多线程是Java开发中的重中之重,其重要性和难度,可见一斑.掌握并精通多线程开发,是每一个程序员的必修之课.哪怕中间的过程很痛苦,只要坚持了,并最终豁然开朗了,都是一种升华. 多线程的优化:合理利用C ...
- Graph_Master(连通分量_E_Hungry+Tarjan)
hdu_4685 终于来写了这题的解题报告,没有在昨天A出来有点遗憾,不得不说数组开大开小真的是阻碍人类进步的一大天坑. 题目大意:给出n个王子,m个公主,只要王子喜欢,公主就得嫁(这个王子当得好霸道 ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B题
2017-09-24 19:16:38 writer:pprp 题目链接:https://www.jisuanke.com/contest/877 题目如下: You are given a list ...
- nagios配置邮件报警
1.配置sendmail vi /etc/mail.rc 加入以下行 set bsdcompat set from=邮件用户名@domain.com smtp=smtp.126.com set smt ...
- springboot idea 配置热加载
在idea 配置springboot的热加载,只需要三步: 第一步.引用jar包 <dependency> <groupId>org.springframework.boot& ...
- angular指令与指令交互
app.directive('mansory',function(){ return { controller:function($scope){ this.changed = function(){ ...
- C# 获取命名空间对应的程序集位置
由于同名命名空间会被多个程序集使用,C#没有提供直接的方法(对象浏览器也不行)通过命名空间获得程序集位置,这样就不方便找到那些引用文件时什么. 那么可以在立即窗口,中断某个代码的时候,去查询类所在程序 ...
- 修改Pycharm for Mac背景色
Mac 上面的Pycharm的背景是白色,太刺眼,网上教程那么多,实用性都不高,最终在csdn找到了一个. 修改步骤如下: pycharm -->Preferences --> Appea ...
- windows下使用selenium报错selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH
问题 :执行程序代码报错: WebDriverException:Message:'geckodriver'executable needs to be in Path 或者 selenium.com ...