npm install vue-router@3.6.5 --save
HomeView.vue
<template>
<div>homediv>
template>
<script>
export default {
name:'HmeView',
data(){
return{}
},
methods:{},
created(){},
mounted(){}
}
script>
<style lang=scss>
style>
<template>
<div>aboutdiv>
template>
<script>
export default {
name:'About',
data(){
return{}
},
methods:{},
created(){},
mounted(){}
}
script>
<style lang=scss>
style>
//引入vue和vue-router
import Vue from 'vue'
import VueRouter from 'vue-router'
//引入路由页面
import HomeView from '../views/HomeView.vue'
import VideoView from '../views/VideoView.vue'
Vue.use(VueRouter)
//定义路由
const routes = [
{
path:'/',
name:'home',
component: HomeView
},
{
path:'/about',
name:'about',
component:()=>import(/*webpackChunkName:"about"*/'../views/About.vue')
}
]
//创建路由
const router = new VueRouter({
//模式
mode:'history',
//使用定义的路由
routes
})
export default router
import Vue from 'vue'
import App from './App.vue'
import router from './router'
Vue.config.productionTip = false
new Vue({
router,
render: h => h(App),
}).$mount('#app')
<template>
<div id='app'>
<!-- 使⽤router-link组件来导航 -->
<!-- 通过传⼊to属性指定连接 -->
<!-- router-link默认会被渲染成⼀个a标签 -->
<router-link to='home'>Home</router-link>
<router-link :to={name:'about'}>About</router-link>
<!-- 路由出⼝ 路由渲染位置 -->
<router-view/>
</div>
</template>