第4章 Vue全家桶(vue-router+vuex) - 4.2 vue-router的基本使用

 index.js

// index.js里面的内容
 
import Vue from 'vue'
// 1.导入
import VueRouter from 'vue-router'
// 2.模块化机制,使用Router
import HomeView from '../views/HomeView.vue'
// 3.创建路由器对象
 
Vue.use(VueRouter)
 
const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]
 
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
 
export default router

main.js里面的内容

// main.js里面的内容
 
import Vue from 'vue'
import App from './App.vue'
import router from './router'
 
Vue.config.productionTip = false
 
new Vue({
  // 4.挂载到vue实例中
  router,
  render: h => h(App)
}).$mount('#app')

 App.vue里面的内容

// App.vue里面的内容
 
<template>
  <div id="app">
    <nav>
      <!-- router_link相当于a标签,to属性相当于a标签的href -->
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link>
      <!-- //router-view相当于路由组件的出口 -->
    </nav>
    <router-view/>
  </div>
</template>
 
<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
 
nav {
  padding: 30px;
}
 
nav a {
  font-weight: bold;
  color: #2c3e50;
}
 
nav a.router-link-exact-active {
  color: #42b983;
}
</style>

HomeView.vue

// HomeView.vue里面的内容
 
<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
  </div>
</template>
 
<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
 
export default {
  name: 'HomeView',
  components: {
    HelloWorld
  }
}
</script>

AboutView.vue

// AboutView.vue里面的内容
 
<template>
  <div class="about">
    <h1>This is an about page</h1>
  </div>
</template>

上面这些是主要文件的内容,其它的文件只是项目架构的初始化内容。

项目展示的效果:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值