vue-router路由的基本使用 (详细步骤)

1.安装vue-router

yarn add vue-router

2.引入路由

import VueRouter from 'vue-router'

3.使用路由

Vue.use(VueRouter)

4.定义路由规则

const routes = [
  {
    path: "/find",
    component: Find
  },
  {
    path: "/my",
    component: My
  },
  {
    path: "/part",
    component: Part
  }
]

5.创建路由对象实例,传入规则

const router = new VueRouter({
	routes
})

6.关联到vue实例

new Vue({
  router
})

7.在页面添加,指定页面即可实现路由跳转

<template>
  <div>
    <div class="footer_wrap">
      <a href="#/find">发现音乐a>
      <a href="#/my">我的音乐a>
      <a href="#/part">朋友a>
    div>
    <div class="top">
      <router-view>router-view>
    div>
  div>
template>

<script>
export default {};
script>

<style scoped>
.footer_wrap {
  position: fixed;
  left: 0;
  top: 0;
  display: flex;
  width: 100%;
  text-align: center;
  background-color: #333;
  color: #ccc;
}
.footer_wrap a {
  flex: 1;
  text-decoration: none;
  padding: 20px 0;
  line-height: 20px;
  background-color: #333;
  color: #ccc;
  border: 1px solid black;
}
.footer_wrap a:hover {
  background-color: #555;
}
.top {
  padding-top: 62px;
}
style>

你可能感兴趣的:(vue.js,前端,javascript)