1. 程式人生 > >vue 子頁面怎麽調用父頁面的方法

vue 子頁面怎麽調用父頁面的方法

back name bsp round rip vid () com app

首先環境要說一下,是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 子頁面怎麽調用父頁面的方法