Vue:路由的基本使用

安装:npm i vue-router
在main.j引入
引入vue-router
import Vue from 'vue'
import App from './App.vue'
//引入使用
import VueRouter from 'vue-router'
Vue.use(VueRouter)
Vue.config.productionTip = false

new Vue({

    render: h => h(App),
}).$mount('#app')


引入之前先要配置

image

index.js:配置规则
//引入使用
import  VueRouter from 'vue-router'
//引入组件
import school from '../components/school'
import student from  '../components/student'

export  default  new VueRouter({
    //编写对应关系
    routes:[
        {
            path:"/school",
            component:school
        },
        {
            path:"/student",
            component:student
        }
    ]
})

导入规则
更新main.js
import Vue from 'vue'
import App from './App.vue'
//引入使用
import VueRouter from 'vue-router'
Vue.use(VueRouter)
Vue.config.productionTip = false

//引入自定义规则
import router from  './router'

new Vue({

    router:router,
    render: h => h(App),
}).$mount('#app')


在APP组件测试
router的两个标签
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <router-link to="/school">学校</router-link><br/>
    <router-link to="/student">学生</router-link>

<router-view></router-view>



  </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>


<router-link to>
<router-view></router-view>

posted @ 2021-11-10 17:27  旅祸少年  阅读(50)  评论(0编辑  收藏  举报