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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值