美文网首页程序员
VUE组件开发 c-radio

VUE组件开发 c-radio

作者: 坚果jimbowhy | 来源:发表于2019-03-08 15:43 被阅读0次

基础要求

了解VUE基本程序结构,有CSS/HTML基础。

VUE组件基础

示例组件为一个 c-radio 单选框组件,展示了VUE组件的事件使用,数据互通能力。

  • Vue.component('c-radio', {...}) 方法定义组件;
  • data: function () { return {...} } 使用function返回一个数据容器给每个组件实例;
  • props: ['options'] 定义组件标签输入参数名称
  • template:'<div ...>' 定义模板
  • methods:{...} 定义组件方法

组件方法内,可以通过 $emit 私有基础方法来触发事件,如以下代码触发一个input事件,可结合双向绑定数据 v-model="price" 实现组件数据到父组的传输:

this.$emit("input", value);

使用c-radio组件:
<c-radio v-model="price" @update="show" :options="products" @click.native="setOptions"></c-radio>

参考:https://cn.vuejs.org/v2/guide/components.html

以下代码抵扣废话

vue 组件 c-radio demo
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>component example</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <script src="https://apps.bdimg.com/libs/zepto/1.1.4/zepto.min.js"></script>
    <script type="text/javascript" src="http://vuejs.org/js/vue.min.js"></script>
    <link rel="stylesheet" type="text/css" href="http://unpkg.com/iview/dist/styles/iview.css">
    <script type="text/javascript" src="http://unpkg.com/iview/dist/iview.min.js"></script>
</head>
<body>
    <style>
        .radio-frame {}
        .radio-frame .title { }
        .radio-frame .icon { float:right; width:20px;height:20px;background:#cccccc; position:relative;border-radius:20px; }
        .radio-frame .icon::after { content:"";width:18px;height:18px;position:absolute;left:1px;top:1px;background:#ffffff;border-radius:20px; }
        .radio-frame .item-active .icon::after{ animate: loading 2s ease ; width:8px;height:8px;left:6px;top:6px;}
        .radio-frame .item-active .icon { background:#26a2ff; }
        .radio-frame .item {padding:8px 8px;font-size:1.3em;margin-top:-1px;border:1px solid #d9d9d9;border-left: none;border-right:none;}
        @keyframes loading {
            50% { width:12px;height:12px;left:4px;top:4px; }
            100% { width:8px;height:8px;left:6px;top:6px; }
        }
    </style>
    <div id="app">
        <i-button @click="show">Click me!</i-button>
        <Modal v-model="visible" title="Message">pick {{price}}</Modal>
        <button class="ivu-btn ivu-btn-primary ivu-btn-large" type="primary" @click.native="go2pay">{{"请先登录"}}</button>

        <c-radio align="left" v-model="price" @update="show" :options="products" @click.native="setOptions"></c-radio>
    </div>
    <script>
    // 定义VUE组件
    Vue.component('c-radio', {
        data: function () {
            return { count: 0 };
        },
        props: ['options'],
        methods:{
            setTarget: function (event) {
                let target = event.currentTarget;
                let value  = event.currentTarget.dataset.value;
                console.log("setTarget", this, target, value);
                this.target = target;
                this.$emit("input", value);
            },
            updateState: function (event) {
                let els=event.currentTarget.getElementsByClassName('item'); 
                for(var p of els) {
                    p.className=p.className.replace('item-active','');
                }
                this.target.className+=' item-active';
                this.$emit("update", this.target.dataset.value);
                console.log("updateState", this, event, event.srcElement, event.target, event.toElement);//
            }
        },
        template:
        '<div class="radio-frame" @click="updateState">'+
        '   <div class="item" v-for="(item,index) in options" @click.capture="setTarget" :index="index" :data-value="item.value">'+
        '       <label class="icon"></label>'+
        '       <label class="title" v-html="item.label">{{count}}</label>'+
        '   </div>'+
        '</div>'
    })

    // 定义VUE程序
    new Vue({
        el: '#app',
        data: {
            price:0,
            visible: false,
            products: [
                {label:"体验7天VIP: ¥20.00 (仅限新用户)", value:20.00},
                {label:"月度VIP: ¥99.00 <del>(原价320)</del>", value:99.00},
                {label:"季度VIP: ¥198.00 <del>(原价640)</del>", value:198.00},
                {label:"季度VIP: ¥396.00 <del>(原价1280)</del>", value:396.00},
            ]
        },
        methods: {
            setOptions: function (event) {
                console.log("setOptions", this, event);
            },
            show: function (event) {
                console.log("show", event, this);
                this.visible = true;
                let that = this;
                setTimeout(function(){ that.visible=false; }, 600);
            }
        }
    })
    </script>
</body>
</html>

相关文章

  • VUE组件开发 c-radio

    基础要求 了解VUE基本程序结构,有CSS/HTML基础。 VUE组件开发 c-radio https://www...

  • vue(2)

    Vue组件化开发 组件化开发的思想 组件的注册 组件间的数据交互 组件插槽的用法 Vue调试工具的用法 组件开发思...

  • 如何开发vue、react、angular的组件库教程

    关键词:vue,react,angular,开发,封装,ui组件,组件库,常用组件 目标:开发相应的组件库vue-...

  • Vue磨刀嚯嚯

    Vue开发风格——传统方法应用vue.js Vue开发风格——单个组件式 组件 基本操作 创建一个组件构造器 注册...

  • vue开发组件设置*.vue文件模板

    title: vue组件开发模板date: 2016-11-21 开发模板 规范 本文介绍vue组件开发的模板,在...

  • 五、Vue生态介绍

    一、Vue组件库Vue组件库是使用Vue框架开发的组件,一般包含着开发者可以直接使用的基础组件:表单、弹窗、表格等...

  • vue写调色板

    组件开发App.vue

  • 0.Vue知识学习整理-1_2020-03-17

    一.Vue开发知识体系: 二.Vue知识整理:4.组件通信 Vue.component('组件name',{......

  • node 以及vue环境配置

    写在前面: 这篇文档不涉及vue的语法,单纯是处理laravel中怎么用vue进行组件化开发的(vue组件化开发需...

  • vue系列组件篇(二)

    概述 vue组件是vue常用的功能,vue也因为强大的组件功能得到很多开发者的青睐。一个好的组件,可以提供给开发者...

网友评论

    本文标题:VUE组件开发 c-radio

    本文链接:https://www.haomeiwen.com/subject/evsrpqtx.html