<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
a{
text-decoration: none;
}
.router-link-exact-active{
text-decoration: underline;
}
</style>
</head>
<body>
<div id="app">
<p>
<!--设置路由跳转的组件-->
<router-link to="/home">主页</router-link>
<router-link to="/home/about">关于</router-link>
<router-link to="/home/contact">联系</router-link>
<router-link to="/home/address">地址</router-link>
</p>
<!--指定路由入口,通过router-view组件指定入口-->
<!--keep-alive进行组件缓存-->
<keep-alive>
<router-view></router-view>
</keep-alive>
</div>
<script src="vue.js"></script>
<!--导入Vue-Router插件-->
<script src="vue-router.js"></script>
<script src="vue-resource.js"></script>
<script>
// 创建路由对象,配置路由选项,使用不同path时,路由入口加载不同的组件
const router = new VueRouter({
//配置路由
routes:[
//home路由
{
path:'/home',
component:{//定义当前路由匹配时router-view所加载的组件
//组件
template:"<div><h1>HELLO</h1><input/></div>"
}
},
//about路由
{
path:'/home/:name',//设置动态路径参数
component:{
template:"<div><h2>{{title}}</h2><p>{{content}}</p></div>",
data(){
return{
title:"",
content:""
}
},
created(){
this.$http.get('json/'+this.$route.params.name+'.json').then(res=>{
this.title=res.data.title;
this.content=res.data.content;
})
},
watch:{
$route:function(newVal,oldVal){
console.log(newVal.params);
//console.log(oldVal.params);
//路由切换时对数据进行修改
this.$http.get('json/'+this.$route.params.name+'.json').then(res=>{
this.title=res.data.title;
this.content=res.data.content;
});
}
}
}
}
]
})
var vm = new Vue({
el:"#app",
router
})
</script>
</body>
</html>
38_动态路由
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。