码迷,mamicode.com
首页 > 其他好文 > 详细

vue 子页面怎么调用父页面的方法

时间:2018-07-18 14:22:08      阅读:1682      评论:0      收藏:0      [点我收藏+]

标签:div   port   code   button   his   cli   rip   alert   src   

首先环境要说一下,是vue-cli 脚手架 搭的webpack 

下面是父页面的写法

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
export default {
  name: App,
  provide(){
      return{
          say:this.say
      }
  },
  methods:{
      say(){
          alert("这是父页面的方法");
      }
  }
}
</script>

<style>
</style>

下面是子页面的写法

<template>
    <button @click="recv">点击调用父页面的方法</button>
</template>
<script>
    export default {
        inject:[say],
        methods:{
         recv(){
             this.say();
         }
        }
    }
    
</script>

运行结果

技术分享图片

 

vue 子页面怎么调用父页面的方法

标签:div   port   code   button   his   cli   rip   alert   src   

原文地址:https://www.cnblogs.com/GeekXwj/p/9328422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!