一、最简范式
//index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>test-vue</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id="app">
<h1>test test this is index.html content</h1>
<p>
<router-link to="/app">use router-link label Go to App.vue</router-link>
<router-link to="/bar">use router-link label Go to bar文件</router-link>
</p>
<router-view></router-view>
</div>
<script>
const App = {
template: "<div>app</div>"
}
const bar = {
template: "<div>bar</div>"
}
const routes = [
{
path: '/app',
component: App
},
{
path: "/bar",
component: bar
},
]
const router = new VueRouter({
routes
})
const app = new Vue({
router
}).$mount('#app')
</script>
</body>
</html>
二、在vue项目中使用router实现页面跳转过程
- 创建vue项目后(具体创建过程见vue项目的创建,根目录是这样的:
- 根据最简范式,对根目录下的文件进行一一对应:
a.定义组件:HelloWorld.vue b.router的创建: c.通过Home.vue将组件HelloWorld.vue的内容给到入口文件App.vue中 d.在App.vue文件中利用<router-view> 标签承接输出的路由内容:
|