plugin

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <script src="../js/vue.js"></script>
     
</head>
<body>
    <div id="root"></div>
    <script>  
        // plugin
        const myPlugin = {
            install(app,options){
                // 扩展额外参数
                app.provide('name','Eric')
                // console.log(app);
                // 扩展指令
                app.directive('focus',{
                    mounted(el){
                        el.focus()
                    }
                })
                // 扩展mixin
                app.mixin({
                    mounted(){
                        console.log('mixin mounted');
                    }
                })
                // 扩展全局 属性
                app.config.globalProperties.$sayHello = 'hello word'
            }
        }

        var app = Vue.createApp({  
            template:`  
                <my-title />  
            `,  
        })

        app.component('my-title',{
            inject:['name'],
            mounted(){
                console.log(this.$sayHello);
            },
            template:`
                <div>
                    {{name}}
                    <input v-focus />
                </div>
            `
        })

        app.use(myPlugin,{name:'Eric'})

         
       const vm = app.mount('#root')

    </script>

</body>
</html>
posted @ 2021-12-08 11:51  13522679763-任国强  阅读(100)  评论(0)    收藏  举报