vue之vue-router路由

vue-router简介:

Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌。包含的功能有:

  • 嵌套的路由/视图表
  • 模块化的、基于组件的路由配置
  • 路由参数、查询、通配符
  • 基于 Vue.js 过度系统的视图过度效果
  • 细粒度的导航控制
  • 带有自动激活的 CSS class 的链接
  • HTML5 历史模式活 hash 模式,在 IE9 中自动降级
  • 自定义的滚动条行为

vue-router的安装:

vue-router 是安装在项目当中 node_modules 文件下面,先查看下是否存在。

vue-router 是一个插件包,所有我们还是需要用 npm/cnpm 进行安装,打开命令行工具,在你项目目录下,进行执行命令。

npm install vue-router --save-dev

如果在一个模块化工程中使用它,必须要通过 Vue.use() 明确地安装路由功能:

import Vue from ‘vue’
import VueRouter from ‘vue-router’
//使用路由
Vue.use(VueRouter );

示例:

创建一个vue项目之后,目录结构!

Main.vue,Content.vue等

<template>
    <h1>Say Hi , Main !</h1>
</template>

<script>
    export default {
        name: 'main'
    }
</script>

<style scoped>

</style>

index.js文件(src下router中的)

import Vue from 'vue'
import Router from 'vue-router'

import Main from '../components/Main'
import Content from '../components/Content'
import BigJi from '../components/BigJi'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/main',
      name: 'main',
      component: Main
    },
    {
      path: '/content',
      name: 'content',
      component: Content
    }
  ]
})

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'
import router from './router'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  //配置路由
  router,
  components: { App },
  template: '<App/>'
})

App.vue

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-link  to="/main">首页</router-link>
    <router-link  to="/content">内容</router-link>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</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;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值