Vue做评价小星星以及组件完成效果

正常vue做法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
          <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.2.0/css/font-awesome.css" rel="stylesheet">
    <style>
        .star{
            color: gold;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div id="app">
        <i v-for="order in count"
         @click="score=order"
          @mouseleave="score1=score" 
          @mouseenter="score1=order"
        :class="order<=score1?'fa-star':'fa-star-o'" 
        class="fa star" 
        aria-hidden="true">
        </i>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        Vue.config.devtools = false//提示不要去装开发软件
        Vue.config.productionTip = false//不去提示
        new Vue({
            el: "#app",
            data() {
                return {
                    score:2,
                    score1:2,
                    count:10
                }
            },
        
            methods:{
            
            }
        })
    </script>
</body>
</html>

vue用组件来写

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.2.0/css/font-awesome.css" rel="stylesheet">
    <style>
        .star {
            color: gold;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div id="app">
        <h1>{{score}}</h1>
        <star label="客服态度" :score="score" @change="score = $event"></star>
        <!-- <star label="物流速度"></star>
        <star label="商品质量" :count="count"></star> -->
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.js"></script>
    <script>

        Vue.component('star', {
            props: {
                count: {
                    type: Number,
                    default: 5 //默认值
                },
                score: {
                    type: Number,
                    default: 0
                },
                label: {
                    type: String,
                    required: true //必须传
                }
            },
            template: `
            <div>
                {{label}}--{{innerScore}}
                <i v-for="order in count" @click="innerScore=order" @mouseleave="score1 = innerScore" 
                @mouseenter="score1=order"
                :class="order<=score1?'fa-star':'fa-star-o'" class="fa star" aria-hidden="true">
                </i>    
            </div>
            `,
            watch: {
                innerScore(val) {
                    //手动触发一个叫change的事件,事件对象e值为val
                    this.$emit('change', val)
                }
            },
            data() {
                return {
                    innerScore: this.score,
                    score1: this.score
                }
            },
        })

        let app = new Vue({
            el: "#app",
            data() {
                return {
                    score1: 2,
                    score: 2,
                    count: 10
                }
            },
            mounted() {
                // setTimeout(() => {
                //     this.count = 20
                // }, 2000);
            },
        })

    </script>
</body>

</html>
  • 0
    点赞
  • 2
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值