方法一
<!DOCTYPE html>
<html lang="zh">
<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">
<router-link to="home">主页</router-link>
<router-view></router-view>
</div>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<script>
const Home = {
template:"<div><h1>主页AAA</h1></div>"
}
const router = new VueRouter({
routes:[
{
path:'/home',
component:Home,
name:'home'
}
]
})
var vm = new Vue({
el:"#app",
router
})
</script>
</body>
</html>
方法二
<!DOCTYPE html>
<html lang="zh">
<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">
<router-link :to="{name:'home'}">主页</router-link>
<router-view></router-view>
</div>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<script>
const Home = {
template:"<div><h1>主页AAA</h1></div>"
}
const router = new VueRouter({
routes:[
{
path:'/home',
component:Home,
name:'home'
}
]
})
var vm = new Vue({
el:"#app",
router
})
</script>
</body>
</html>
方法三
<!DOCTYPE html>
<html lang="zh">
<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">
<strong @click="changeView">主页</strong>
<router-view></router-view>
</div>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<script>
const Home = {
template:"<div><h1>主页AAA</h1></div>"
}
const router = new VueRouter({
routes:[
{
path:'/home',
component:Home,
name:'home'
}
]
})
var vm = new Vue({
el:"#app",
router,
methods:{
changeView(){
this.$router.push('/home')//设置路由,匹配path属性
}
}
})
</script>
</body>
</html>
方法四
<!DOCTYPE html>
<html lang="zh">
<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">
<router-link :to="{name:'home'}">主页</router-link>
<router-view></router-view>
</div>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<script>
const Home = {
template:"<div><h1>主页AAA</h1></div>"
}
const router = new VueRouter({
routes:[
{
path:'/home',
component:Home,
name:'home'
}
]
})
var vm = new Vue({
el:"#app",
router,
methods:{
changeView(){
this.$router.push({name:'home'})
}
}
})
</script>
</body>
</html>