1、运行以下命令安装vue-router
yarn add vue-router
2、在src目录下的components中新建两个vue页面
3、在src目录下新建router文件夹,在router文件夹下面新建index.js文件
4、配置main.js文件
//引入Vue
import Vue from "vue";
//引入App
import App from './App';
//引入vue-router
import VueRouter from "vue-router";
import router from './router';//关闭Vue的生产提示
Vue.config.productionTip = false;//应用vue-router插件
Vue.use(VueRouter);new Vue({el: '#app',render: h => h(App),router
});
5、在App.vue中使用router-link实现路由切换,router-view确定视图的位置
<template><div><div class="row"><div class="col-xs-offset-2 col-xs-8"><div class="page-header"><h2>Vue Router Demo</h2></div></div></div><div class="row"><div class="col-xs-2 col-xs-offset-2"><div class="list-group"><!--原始使用a标签跳转多个页面,多页面应用--><!-- <a class="list-group-item active" href="./about.html">About</a>--><!-- <a class="list-group-item" href="./home.html">Home</a>--><!--vue中借助router=link标签实现路由的切换--><router-link class="list-group-item" active-class="active" to="/about">About</router-link><div style="width: 20px"></div><router-link class="list-group-item" active-class="active" to="/home">Home</router-link></div></div><div class="col-xs-6"><div class="panel"><div class="panel-body"><!--router-view确定视图的位置--><router-view> </router-view></div></div></div></div></div>
</template><script>
export default {name: "App",
};
</script>
<style lang="css" scoped>
</style>
5、最终实现页面
点击About时