vue-router 路由 iframe嵌套

功能点:

使用vue-router.js 实现路由功能,简单路由参数设置。

在iframe里展示自定义链接内容(url地址或者加载路由地址)测试。

监听属性变化功能。


<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<!--<script src="../vue.js"></script>-->
		<!--<script src="../bower_components/vue-router/dist/vue-router.min.js"></script>-->
		
		<script src="https://unpkg.com/vue/dist/vue.js"></script>
		<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
		
		<style>
			* {
				margin:0;padding:0;color:#333;
			}
			body {
				margin:0 50px;
			}
			a { text-decoration: none; cursor: pointer;color:#fff}
			.router-link-active {
				color:#0c13e6;
			}
			.nav {
				background-color: #e45ea273;
			    width: 100%;
			    height: 60px;
			    line-height: 60px;
			    font-size: 20px;
			}
			
			ul {
				list-style: none;
				margin: 0 30px;
			}
			li {
				float: left;
				margin:0 20px;
			}
		</style>
	</head>
	<body>
		<div id="box">
			<div class="nav">
				<ul>
					<li>
						<router-link to="/home">主页</router-link>
					</li>
					<li>
						<router-link to="/news">新闻</router-link>
					</li>
					<li>
						<router-link to="/myiframe/urlPath?src=https://www.baidu.com/s?wd=abc">Myiframe_URL</router-link>
					</li>
					<li>
						<router-link to="/myiframe/home">Myiframe_主页</router-link>
					</li>
					<li>
						<router-link to="/myiframe/news">Myiframe_新闻</router-link>
					</li>
				</ul>
			</div>
			<div>
				<!--渲染匹配的组件-->
				<router-view></router-view>
			</div>
		</div>
		
		<template id="home">
			<div>
				<h3>我是主页</h3>
			</div>
		</template>
		<template id="news">
			<div>
				<div>
					<h3>我是新闻</h3>
					<router-link to="/news/details/10010?t=1">用户详细</router-link>
					<router-link to="/news/name/lucy/age/20">用户名称</router-link>
					<router-link to="/news/ret">首页</router-link>
				</div>
				<div>
					<router-view></router-view>
				</div>
			</div>
		</template>
		<template id="details">
			<div>
				<h3>我是用户详细</h3>
				<h3>当前完整路径fullPath:{{$route.fullPath}}</h3>
				<h3>当前路径path:{{$route.path}}</h3>
				<h3>当前参数params:{{$route.params}}</h3>
				<h3>当前查询参数query:{{$route.query}}</h3>
				<h3>hash:{{$route.hash}}</h3>
				<h3>router:{{$route.router}}</h3>
				<!--<h3>matched:{{$route.matched}}</h3>-->
			</div>
		</template>
		<template id="myiframe">
			<div>
				<h3>urlPath : {{urlPath}}</h3>
				<h3>routerPath : {{routerPath}}</h3>
				<h3>src : {{$route.query.src}}</h3>
				<iframe v-if="$route.query.src" :src='$route.query.src' frameborder="0" width="900px" height="500px"></iframe>
				<iframe v-else :src="urlPath" frameborder="0" width="900px" height="500px"></iframe>
				<!--<iframe :src="'//elemefe.github.io/mint-ui/#'" frameborder="0" width="300px" height="300px"></iframe>-->
			</div>
		</template>
		<script>
		
			var Home = {
				template:"#home"
			}
			var News = {
				template:"#news"
			}
			var Details = {
				template:"#details"
			}
			var Myiframe = {
				template:"#myiframe"
				,data: function() {
					return {
						msg:""
						,urlPath: this.getUrlPath() //iframe src 路径
					}
				}
				,props:['routerPath'] // 路由地址
				,watch: {
					routerPath: function(val) {// 监听routerPath变化,改变src路径
						this.urlPath = this.getUrlPath();
					}
				}
				,methods: {
					getUrlPath:function() {//获取 iframe src 路径
						let url = window.location.href;
						url = url.replace("myiframe/","");
						return url;
					}
				}
			}
			
			const routes = [
				{path:"/home",component:Home},
				{
					path:"/news",
					component:News,
					children:[
						//实际使用中可以懒加载方式={path:"index",component: resolve => require(['./index.vue'], resolve) },
						{path:"details/:id",component:Details},
						{path:"name/:name/age/:age",component:{template:"<h3>{{$route.params}}</h3>"}},
						{path:"ret",redirect:'/home'}
						
					]
				},
				{path:"/myiframe/:routerPath",component:Myiframe,props: true},//props:true 必须
				{path:"*",redirect:"/home"} //{path:"*",component:Home}
			];
			
			const router=new VueRouter({
	            routes
	        });
	        
			var app =  new Vue({
				router,
				el:'#box',
				data:{
					
				}
			});
		</script>
	</body>
</html>


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue-RouterVue.js官方提供的路由管理插件,可以实现单页面应用(SPA)中的路由切换。在Vue-Router中,我们可以通过嵌套路由来管理复杂的页面结构和页面间的跳转逻辑。 路由嵌套是指在一个路由组件中嵌套其他子路由组件,形成一个父子关系的层级结构。通过路由嵌套,我们可以更好地管理页面的细节和逻辑。 在Vue-Router中,我们首先需要定义路由的层级结构。我们可以在路由配置中使用`children`属性来定义嵌套路由。例如: ```js const router = new VueRouter({ routes: [ { path: '/home', component: Home, children: [ { path: 'about', component: About }, { path: 'contact', component: Contact } ] } ] }); ``` 在上面的代码中,我们定义了一个名为`home`的父路由,它有两个子路由`about`和`contact`。当用户访问`/home/about`时,会渲染`About`组件,而当用户访问`/home/contact`时,会渲染`Contact`组件。 在嵌套路由中,父路由的组件中需要使用`<router-view>`标签来显示子路由对应的组件。在上面的例子中,我们需要在`Home`组件中添加`<router-view>`标签。 ```html <template> <div> <h1>Home</h1> <router-view></router-view> </div> </template> ``` 当用户访问`/home`时,会渲染`Home`组件,并且`<router-view>`标签中会显示子路由对应的组件。 通过嵌套路由,我们可以更好地管理页面的结构和逻辑,将页面分成多个模块,并且可以在子路由中访问父路由的数据和方法。我们可以在路由跳转时传递参数,通过使用`$route.params`来获取参数。 总结来说,Vue-Router路由嵌套可以实现复杂的页面结构和页面间的跳转逻辑,通过定义父子关系的路由层级结构,使用`<router-view>`标签来显示子组件,并且可以通过路由传递参数来实现页面间的数据共享。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值