<div id="app"> hello <my-button> </my-button> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.7/vue.common.dev.js"></script> <script> let vm = new Vue({ provide: { msg: '100' }, el: '#app', components: { 'MyButton': { inject: ['msg'], template: `<div>{{msg}}</div>`, } } }) </script>
provide 是在父组件中定义,然后所有子组件都是可以通过 inject 注入该变量进行操作