组件通信 Provide&&inject

在父组件中利用Provide 注入数据,在所有的子组件都可以拿到这个数据 可以在vue 中用来刷新页面

<!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>

输出:

 

posted @ 2019-09-18 09:42  1点  阅读(182)  评论(0编辑  收藏  举报