父子组件demo

<html>

<head>
    <title>vue</title>
    <link rel="stylesheet" href="./index.css">
</head>

<body>
    <div id="app">
        <!-- 答题卡子组件 -->
        <Card v-on:switch_question="switch_question" :datalist="dataList" :index="index"></Card>
    </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>
    //答题卡子组件(题号选择功能),传入dataList/index,修改index,传出index
    Vue.component('Card', {
        props: ['datalist','index'],
        template:
            `<div>
            <div class="card1">
                <p class="card-title">答题卡</p>
            </div>
            <div class="card2">
                <div :class="index==i?'current-question':'others-question'" v-for="(question, i) in datalist"
                    :key="question._id" @click="switch_question(i)">
                    {{i + 1}}
                </div>
            </div>
        </div>`,
        methods: {
            switch_question: function (i) {
                this.$emit('switch_question', i);    
            }
        }
    });

    //最外层
    new Vue({
        el: '#app',
        data: {
            index: 0,
            dataList: [
                {
                    id: '1',
                    type: 'join',
                    content: '四大名著连一连',
                    leftItems: ['《水浒传》', '《西游记》', '《三国演义》', '《红楼梦》'],
                    rightItems: ['罗贯中', '施耐庵', '曹雪芹', '吴承恩'],
                    answer: [[0, 1], [1, 3], [2, 0], [4, 2]],
                    userAnswer: null
                },
                {
                    id: '2',
                    type: 'judgment',
                    content: '所有的苹果都是水果',
                    answer: true,
                    userAnswer: null
                }
            ],
        },
        methods: {
            switch_question(i) {
                this.index = i
                console.log(this.index);
            }
        }
    })
</script>

</html>

:datalist="dataList" :index="index" 右边外层的数据,左边组件内用到的数据

因为html大小写不区分,所以左边要小写或者是用个短横线

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值