07Vue-router路由

目录

 1. main.js  【演示导入组件】

2.App.vue  [演示使用]

3.1Content.vue

4.index.js

5.main.js  【配置路由】

 6.使用App.vue


 安装

基于第一个vue-cli进行测试学习; 先查看node modules中是否存在vue-router   

vue-router是一个插件包, 所以我们还是需要用n pm/cn pm来进行安装的。打开命令行工具,进入你的项目目录,输入下面命令。

 npm install vue-router --save-dev

说明

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

 import Vue from 'vue'
 import Router from 'vue-router'
 ​
 Vue.use(Router );

 1. main.js  【演示导入组件】

import Vue from 'vue'
import App from './App'
//导入vue-router
import Router from 'vue-router'

Vue.config.productionTip = false


//显示声明使用Router
Vue.use(Router)

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

2.App.vue  [演示使用]

<template>
  <div id="app">

  </div>
</template>

<script>

  import Content from './components/Content'

export default {
  name: 'App',
  comments: {
    Content
  }
}
</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>

3.1Content.vue

<template>

  <h1>内容页面</h1>
</template>

<script>
    export default {
        name: "Content"
    }
</script>

<style scoped>

</style>


3.2Main.vue

<template>
  <div>
    <h1>首页</h1>
  </div>
</template>

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

<style scoped>

</style>

4.index.js

import Vue from 'vue'
import VueRouter from 'vue-router'

//导入组件
import Content from '../components/Content'
import Main from '../components/Main'


//安装路由
Vue.use(VueRouter);

//配置导出路由
export default new VueRouter({
  routes: [   //路由
    {
      // 路由的路径
      path: '/content',
      name: 'content',
      // 路由的跳转
      component: Content

    },
    {
      path: '/main',
      name: 'main',
      component: Main
    }
  ]
})

5.main.js  【配置路由】

import Vue from 'vue'
import App from './App'

import router from './router' //自动扫描里面的路由配置 路由文件

Vue.config.productionTip = false;

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

 6.使用App.vue

<template>
  <div id="app">

    <h1>Vue-Router</h1>
    <router-link to="/main">首页</router-link>
    <router-link to="/content">内容页</router-link>

    <router-view></router-view>

  </div>
</template>

<script>

  import Content from './components/Content'

export default {
  name: 'App',
  comments: {
    Content
  }
}
</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>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gh-xiaohe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值