ITPub博客

首页 > IT职业 > IT生活 > Vue组件复用 - router变化 导致请求只发生一次

Vue组件复用 - router变化 导致请求只发生一次

IT生活 作者:mifeng 时间:2021-09-09 10:48:02 0 删除 编辑

组件复用 - router变化 导致请求只发生一次

导航 main.vue

	<div v-for='c in list'>
		<div @click='onClick(c)'></div>
	<div>
	export default{
		data(){
			return{
				list:[
					{name:'ddd',price:9999},
					{name:'cccc',price:9999}
					]
			}
		},
		methods:{
			onClick(c){
				this.$router.push({
					name:"detail",
					params:{name:c.name}
				})
			}
		}
	}

detail.vue

	create(){
		console.log('发送详情获取请求');//只执行一次,需求是要每次切换路由到详情页都要执行
	}

route.js

	{
		path:'/main'
		name:'/main',
		component:()= import('../views/Main.vue'),
		children:[
			{
				path:'/main/course/:name',
				name:'detail',
				component:()=> import('../views/Detail.vue')
			}
		]
	}

修改detail.vue 增加watch监听route

	watch:{
		$route:{
			immediate:true,//初始化就执行一次
			handle(){
				console.log('发送详情获取请求')
			}
		}
	}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3038/viewspace-2826263/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    79
  • 访问量
    942