在父組件中利用Provide 注入數據,在全部的子組件均可以拿到這個數據 能夠在vue 中用來刷新頁面html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Provide&&Inject通訊</title> <script src="./node_modules/vue/dist/vue.js"></script> </head> <body> <div id="app"> <my-button> <my></my> </my-button> </div> </body> <script> Vue.component('my-button',{ template:`<div><slot></slot></div>`, //在父組件注入數據 provide:{ msg:'hello' } }) //全部的子組件均可以拿到數據 Vue.component('my',{ template:`<div>{{msg}}</div>`, inject:['msg'] }) let vm = new Vue({ el:'#app' }) //hello </script> </html>
輸出:vue