<!DOCTYPE html>
<html>
<head>
<title>vue router</title>
<meta charset="utf-8">
</head>
<body>
<div id='app'>
<h1>Hello App</h1>
<p>
<!--显示连接的标题-->
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>
<router-link to="/user/admin">Go to User-id</router-link>
<router-link to="/home">Go to Home</router-link>
<button @click='jump2Home'>跳转到</button>
</p>
<router-view></router-view>
</div>
<template id='home'>
<div>
<h2>首页</h2>
<router-link to="/home/login">登录</router-link>
<router-link to="/home/reg">注册</router-link>
<router-view></router-view>
</div>
</template>
<template id='login'>
<div>登陆界面</div>
</template>
<template id='reg'>
<div>注册界面</div>
</template>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script type="text/javascript">
const Home = {template:"#home"};
const Login = {template:"#login"};
const Reg = {template:"#reg"};
const Foo = {
template:"<div>foo</div>"
}
const Bar = {
template:"<div>bar</div>"
}
const User = {
template:"<div>User {{$route.params.id}}</div>",//显示路由本身
}
const routes = [
{path:"/",redirect:"/foo"},//设置默认值,初始页面显示“/foo”路径的内容
{path:"/foo",component:Foo},//“/foo”下的内容为组件Foo
{path:"/bar",component:Bar},
{name:'user',path:"/user/:id",component:User},
{
name:'home',
path:"/home",
component:Home,
children:[
{path:"/home/login",component:Login},
{path:"/home/reg",component:Reg}
]
}
]
const router = new VueRouter({
routes
})
//const可以理解为都为组件/路由
var app = new Vue({
el:"#app",
watch:{
'$route'(to,from){
console.info(to);
console.info(from)
}
},
methods:{
jump2Home(){
//push,将当前页面入栈,按后退键可以返回到前一个页面
//this.$router.push({name:"home"}) 用name确定跳转页面
//this.$router.push({path:"/home/login"}) //用路径确定跳转页面
//this.$router.push({name:"user",params:{id:'aaabbb'}})//跳转到user页面,并将id设为aaabbb
//replace
this.$router.replace({name:"user",params:{id:'aaabbb'}})
//go的参数为-1时,可以回到上一个页面,参数为1时可以到下一个页面
//this.$router.go(-1)
}
},
router//id为‘app’的区块有路由router
})
</script>
</body>
</html>
vue-router
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 实际经历,在公司vue项目中,一个商品页面可以从两个入口进入,分别是首页的一个<router-link to="/...
- 1.router.push(location)=====window.history.pushState 说明:想...
- (一) (1) vue-router简单使用 官网:https://router.vuejs.org/zh-cn/...
- 1. 安装webpack npm install webpack -g 2. 安装Vue脚手架 npm insta...