vue父子组件

基本使用

复制代码
<!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>Document</title>
</head>
<body>
    <div id="app">
    <div>
        <cpn2></cpn2>
    </div>
    
    </div>

    <script src="../js/vue.js"></script>
    <script>
        //1. 创建组件构造器对象
        const cnpC1 = Vue.extend({
            template: `
                        <div>
                        <h3> 标题1 </h3>
                        <p>aaaaaaaaaaaa</p>
                        <p>bbbbbbbbbbbb</p>
                        </div>
                        `
        })

        const cnpC2 = Vue.extend({
            template: `
                        <div>
                        <h3> 标题2 </h3>
                        <p>cccccccccccc</p>
                        <p>dddddddddddd</p>
                        <cpn1></cpn1>
                        </div>
                        `,
            components:{
                cpn1:cnpC1
            }
        })

        


        const app = new Vue({
            el: "#app",
            data:{
                message: "hello world",
            },
            // 2.局部组件注册
            components:{
                
                cpn2:cnpC2
            }
            
        })

    </script>
</body>
</html>
复制代码

 

父传子

 

复制代码
<!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>Document</title>
</head>
<body>
    <div id="app">
    <cpn :cmessage="message"></cpn>
    </div>


    <template id="cpn">
        <div>
        <h2>{{cmessage}}</h2>
        <p>{{cmovies}}</p>
        </div>
    </template>


    <script src="../js/vue.js"></script>
    <script>
        // 父传子用props
        const cpn = {
            template:"#cpn",
            data(){
                return{

                }
            },
            // 第一种
            //props:['cmovies', "cmessage"]
            //第二种
            // props:{
            //     cmovies:Array,
            //     cmessage:String,
            // },
            //第三种
            props:{
                cmovies:{
                    type: Array,
                    default(){
                        return ["言叶之庭", "秒速五厘米", "声之形"]
                    }
                },
                cmessage:
                {
                    type: String,
                    default:"moon",
                    required: true,  //标识必传
            },
        }
        
    }


        const app = new Vue({
            el: "#app",
            data:{
                message: "hello world",
                movies: ["月色真美", "你的名字","天气之子"]
            },
            
            components:{
                cpn
            }
            
        })

    </script>
</body>
</html>
复制代码

 

子传父

 

复制代码
<!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>Document</title>
</head>
<body>
    <div id="app">

    <cpn @itemclick="cpnclick"></cpn>

    </div>

    <template id="cpn">
        <div>
        <Button type="" v-for="item in categories" @click="btnclick(item)">{{item.name}}</Button>
        </div>
    </template>


    <script src="../js/vue.js"></script>
    <script>
        // 
        const cpn = {
            template:"#cpn",
            data(){
                return{
                    categories:[
                        {id:1, name:"热门推荐"},
                        {id:2, name:"手机数码"},
                        {id:3, name:"家具家电"},
                        {id:4, name:"电脑办公"},
                    ]
                }
            },
            methods: {
                btnclick(item){
                    // console.log(item.id)
                    // 发送事件, 自定义事件
                    this.$emit("itemclick", item.id)
                }
            },
        
    }


        const app = new Vue({
            el: "#app",
            data:{
                message: "hello world",
                movies: ["月色真美", "你的名字","天气之子"]
            },
            
            components:{
                cpn
            },
            methods:{
                cpnclick(item_id){
                    console.log(item_id)
                }
            }
            
        })

    </script>
</body>
</html>
复制代码

 

 

父访问子

复制代码
<!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>Document</title>
</head>
<body>
    <div id="app">
    <cpn ref="aaa"></cpn>
    <button @click="btnclick">按钮</button>
    </div>

    <template id="cpn">
        <div>我是子组件</div>
       
    </template>

    <script src="../js/vue.js"></script>
    <script>
        
        const cpn = {
            template:'#cpn',
            methods: {
                showmessage(){
                    console.log("showmessage111")
                }
            },
            data(){
                return{
                    name:7777777
                }
            }
        }



        const app = new Vue({
            el: "#app",
            data:{
                message: "hello world",
            },
            // 局部组件注册
            components:{
                cpn
            },
            methods:{
                btnclick(){
                    // $children
                    console.log(this.$children)
                    this.$children[0].showmessage()
                    console.log(this.$children[0].name)
                    // $refs 一般用这个,需要在组件上加ref属性
                    console.log(this.$refs.aaa)
                    console.log(this.$refs.aaa.name)
                }
            }
            
        })

    </script>
</body>
</html>
复制代码

 

子访问父

 

复制代码
<!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>Document</title>
</head>
<body>
    <div id="app">
    <cpn ref="aaa"></cpn>
    
    </div>

    <template id="cpn">
        <div>
        <div>我是子组件</div>
        <button @click="btnclick">按钮</button>
        </div>
    </template>

    <script src="../js/vue.js"></script>
    <script>
        
        const cpn = {
            template:'#cpn',
            methods:{
                btnclick(){
                    console.log(this.$parent);
                    console.log(this.$parent.message)
                    // 访问根组件
                    console.log(this.$root)
                }
            }
        }



        const app = new Vue({
            el: "#app",
            data:{
                message: "hello world",
            },
            // 局部组件注册
            components:{
                cpn
            },
            methods:{
               
            }
            
        })

    </script>
</body>
</html>
复制代码

 

posted @   Wchime  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示