<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width,initial-scale=1.0'>
<meta http-equiv='X-UA-Compatible' content='ie=edge'>
<title>Document</title>
</head>
<body>
<div id='app'>
<!-- 1.设置链接 -->
<router-link to="/home">主页</router-link>
<router-link to="/top">热点</router-link>
<router-link to="/aboutus">关于我们</router-link>
<!-- 2. 提供容器:将来渲染组件 -->
<router-view></router-view>
</div>
<script src='./vue.js'></script>
<script src="./vue-router.js"></script>
<script>
// 3. 提供要渲染的组件选项(对象)
const Home = {
template: `<div>首页组件</div>`
};
const Top = {
template: `<div>热点组件</div>`
};
const Aboutus = {
template: `<div>关于我们组件</div>`
};
const routes = [{
path: "/",
component: Home,
redirect: "/top"
}, {
path: "/home",
component: Home
}, {
path: "/top",
component: Top,
}, {
path: "/aboutus",
component: Aboutus
}];
// 4. 实例化路由对象
const router = new VueRouter({
// 路由选项 routes
// 5. 配置路由:匹配当前的标识 渲染对应的组件
// routes:routes
routes
})
new Vue({
el: '#app',
// 6. 挂载(使用)路由
// router:router
router,
data: {
},
methods: {
}
});
</script>
</body>
</html>
原文:https://www.cnblogs.com/divtab/p/10940945.html