天天看點

vue 調用父頁面方法

1,使用$parent方法

//在父元件中
data(){
    return{
        a:1
    }
}
           
//在子元件中
this.$parent.a++
           

2,使用 依賴注入 (推薦使用)

//父元件
export default {
  name: 'App',
  provide(){
      return{
          say:this.say
      }
  },
  methods:{
      say(){
          alert("這是父頁面的方法");
      }
  }
}
           
//在子元件中
export default {
        inject:['say'],
        methods:{
         recv(){
             this.say();
         }
        }
    }