组件VUE3

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.js"></script>
</head>

<body>
    <div id="app"></div>
</body>
<script>
    const Counter = {
        data() {
            return {
                count: 0
            }
        },
        template: `<div>{{count}}<button @click="count++">增加1</button></div>`
    }
    const XieDaJiao = {
        template: `<h2>谢大脚</h2>`
    }
    const app = Vue.createApp({
        components: {
            jspang: Counter,
            'xie-da-jiao': XieDaJiao
        },
        template: `
            <h2>JSPang.com</h2>
            <xie-da-jiao />
            <jspang />

        `
    })
    const vm = app.mount("#app")

</script>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Demo04</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.js"></script>
</head>

<body>
    <div id="app"></div>
</body>
<script>
    const app = Vue.createApp({
        data() {
            return {
                name: 123,
                pay: () => {
                    alert('给你500元')
                }
            }
        },

        template: `
            <h2>JSPang.com</h2>
            <Son :name="name" />
        `
    })

   app.component('Son',{
            props:['name'],
            template:`<div>{{ typeof name}} div </div>`
        })

    const vm = app.mount("#app")
</script>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Demo07</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.js"></script>
</head>

<body>
    <div id="app"></div>
</body>
<script>
    const app = Vue.createApp({
        data() {
            return {
                counter: 0
            }
        },
        template: `
                    <h2>JSPang.com</h2>
                    <counter :counter="counter"/>  
             `
    })
    app.component('Counter', {
        props: ['counter'],
        template: `
        {{counter}}<button @click="this.counter+=1">增加数量</button>
    `
    })

    const vm = app.mount("#app")
</script>

</html>

数据从父级组件传递给子组件,只能单向绑定。子组件内部不能直接修改从父组件传递过来的数据

单向数据流就是父组件可以向子组件传递数据,但是子组件不能修改数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值