1 原始方式

1), 路由, vue-router 2

简单安装方式, 但先讲原理

cnpm install --save vue-router

2), 引用

在main.js中

// 引入 router
import VueRouter from "vue-router"
Vue.use(VueRouter)

3), 注入:

import VueRouter from "vue-router"

import HelloWorld from './components/HelloWorld'

Vue.use(VueRouter)

var router = new VueRouter({
routes: [{
path: "/hello",
component: HelloWorld
}]
})

在 vue 中注入:

new Vue({
el: '#app',
components: {App},
template: '<App/>',
router
})

4), 视图加载位置

在app.vue中

<template>
<div id="app">
<img src="./assets/logo.png">
<HelloWorld/> <router-view></router-view> </div>
</template>

通过  http://localhost:8080/#/hello 的方式访问

2,  但这样使用太乱了, 所以将路由单独提出来:

新建 router 目录, 在下面新建 index.js 文件

import Vue from 'vue'

// 引入 router
import VueRouter from "vue-router" import HelloWorld from '../components/HelloWorld'
import HellowIwen from '../components/HellowIwen' Vue.use(VueRouter) export default new VueRouter({
routes: [{
path: "/hello",
component: HelloWorld
}, {
path: "/iwen",
component: HellowIwen
}]
})

main.js抽取逻辑后变为 ;

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App' // 引入 默认加载 index.js
import router from './router' Vue.config.productionTip = false /* eslint-disable no-new */
new Vue({
el: '#app',
components: {App},
template: '<App/>',
router
})

确定加载位置: , 在 app.vue 中

<template>
<div id="app">
<img src="./assets/logo.png">
<HelloWorld/> <router-view></router-view> </div>
</template>

3, 跳转

1), 新建 navelist .vue, 使用 router-link 标签进行跳转

<template>
<div>
<!--用来跳转的-->
<ul>
<li>
<!--使用基本的 to-->
<router-link to="/hello">hello world</router-link>
</li> <li>
<router-link to="/iwen">hello iwen</router-link>
</li>
</ul> <ul>
<li>
<!--v-bind:to 动态数据加载-->
<router-link :to="urlData.helloworld">hello world</router-link>
</li>
</ul> <ul>
<li>
<!-- 使用 path -->
<router-link :to="{path: urlData.helloworld}">hello world</router-link>
</li>
</ul> </div>
</template> <script>
export default {
name: "NavList",
data() {
return {
urlData: {
helloworld: "/hello",
helloIwen: "/iewn"
}
}
}
}
</script> <style scoped>
ul {
list-style: none;
text-align: center;
}
li {
color: red
}
</style>

当点击时, 通过 router-link 标签解携为 a 标签, 进行叶面间跳转

可以在进行 vue init webpack router-example 的时候, 直接创建router, 和上面的结构一致

vue-10-路由的更多相关文章

  1. vue(5)—— vue的路由插件—vue-router 常用属性方法

    前端路由 看到这里可能有朋友有疑惑了,前端也有路由吗?这些难道不应该是在后端部分操作的吗?确实是这样,但是现在前后端分离后,加上现在的前端框架的实用性,为的就是均衡前后端的工作量,所以在前端也有了路由 ...

  2. 【Vue的路由,SPA概念】

    前言 本章是为了以后实现前端页面的搭建而写的, 重点在于如何实现 单页Web应用 因为相对于以前的传统多页面web,有很大的缺陷. 那么就必须了解一下Vue的路由设置. SPA的概念 总的而言,我们知 ...

  3. Vue 嵌套路由使用总结

    Vue 嵌套路由使用总结   by:授客 QQ:1033553122   开发环境   Win 10   node-v10.15.3-x64.msi 下载地址: https://nodejs.org/ ...

  4. [VUE]关于路由哪些事儿

    什么是路由 之前有个小伙伴面试被问到:面试官:不用vue能不能写单页面应用?答:用angular啊(咳咳,开个玩笑),答案确实是可以的,原生js中有个事件叫做onhashchange,可以在windo ...

  5. 四、vue前端路由(轻松入门vue)

    轻松入门vue系列 Vue前端路由 七.Vue前端路由 1. 路由的基本概念与原理 后端路由 前端路由 实现简单的前端路由 vue-router基本概念 2. vue-router的基本使用 基本使用 ...

  6. Vue 路由懒加载, VueRouter一步完成Vue的路由懒加载 一行代码搞定懒加载

    Vue Router路由配置中的component里面配置即可 1 // 路由懒加载的方式加载组件 2 3 component: () => import('@/views/Detail'), ...

  7. vue父路由默认选中第一个子路由,切换子路由让父路由高亮不会消失

    vue父路由默认选中第一个子路由,切换子路由让父路由高亮不会消失 正常默认会有 .router-active-class 识别高亮 达到以上注意: 1. exact 不要加 注意是不要加,exact ...

  8. vue的路由映射问题

    遇到的问题 今天在项目中遇到了一个问题,明明在Router文件夹下的路由js映射文件中,配置好了,如下: // 生日贺卡 { path: 'birthdayRemind', component: lo ...

  9. 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI

    一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...

  10. Vue.js路由

    有时候,我们在用vue的时候会有这样的需求,比如一个管理系统,点了左边的菜单栏,右边跳转到一个新的页面中,而且刷新的时候还会停留在原来打开的页面. 又或者,一个页面中几个不同的画面来回点击切换,这两种 ...

随机推荐

  1. 第三周助教工作总结——NWNU李泓毅

    本周点评作业数量:第一部分是第一篇博客的剩余部分,给出了共计22份作业的具体分数.第二部分是第二篇博客的已提交部分共计19份作业. 这是我的博客地址:https://www.cnblogs.com/N ...

  2. 每日一练ACM 2019.0418

    Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离.   Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2 ...

  3. IOS KVO没有在delloc中移除导致奔溃

    1.背景 为了监听tableview的移动 [_tableView addObserver:self forKeyPath:@"contentOffset" options:NSK ...

  4. 走进JDK(十一)------LinkedHashMap

    概述LinkedHashMap 继承自 HashMap,在 HashMap 基础上,通过维护一条双向链表,解决了 HashMap 不能随时保持遍历顺序和插入顺序一致的问题.除此之外,LinkedHas ...

  5. Win7 VS2017编译magnum及例子

    magnum是一个开源的图形中间件 Lightweight and modular C++11/C++14 graphics middleware for games and data visuali ...

  6. 用chrome模拟微信浏览器访问页面

    有的网页通过限制 user agent  来达到禁止在微信浏览器以外的地方访问,我们通过chrome修改user agent即可破解. CHROME浏览器,进入开发者模式 切换到手机浏览模板 打开 N ...

  7. CentOS 6下升级Python版本

    CentOS6.8默认的python版本是2.6,而现在好多python组件开始只支持2.7以上的版本,比如说我今天遇到的pip install pysqlite,升级python版本是一个痛苦但又常 ...

  8. Cookie的使用(14)

    一:cookie的简要介绍: (1)什么是cookie a.cookie是一种客户端的状态管理技术b.当浏览器向服务器发送请求的时候,服务器会将少量的数据以set-cookie消息头的方式发送给浏览器 ...

  9. 【转载】java定义二维数组问题。分清数组与集合的区别

    出处: 度娘知道 答案由用户{ heitianba }提供. Q: int a[][] = new int[3][2];  a[0] = {1,6};  报错:第二句是非法表达式.为什么? A: in ...

  10. ext__给grid Panel设置绑定事件

    使用面板来展示详情信息 1.创建一个面板 (双击添加) 2.给该面板设置itemid的值为:detailPanel 3.给面板设置模板 4.添加下面的内容 id:{id}</br> nam ...