本文实例讲述了vue 组件开发原理与实现方法。分享给大家供大家参考,具体如下:
概要
vue 的一个特点是进行组件开发,组件的优势是我们可以封装自己的控件,实现重用,比如我们在平台中封装了自己的附件控件,输入控件等。
组件的开发
在vue 中一个组件,就是一个独立的.vue 文件,这个文件分为三部分。
1.模板
2.脚本
3.样式
我们看一个系统中最常用的组件。
<template> <div > <div v-if="right=='r'" class="readOnlyBgColor">{{value}}</div> <div class="box-custom-component" v-else-if="right=='w'"> <input type="text" @blur="blurHandler" @focus="focusHandler" :required="required" v-model="currentValue" :placeholder="placeholder" ></input> <a href="javascript:;" rel="external nofollow" class="yd-input-clear" tabindex="-1" @click="clearInput" v-show="showClear && !isempty"></a> </div> </div> </template> <script type="text/ecmascript-6"> import { calcRight } from "@/assets/app.js"; import {VTypes,RxUtil} from "@/assets/util.js"; export default{ name : "rx-input", props: { value:[String,Number], permission:Object, permissionkey:String, showClear:{ type: Boolean, default: true }, readonly: { type: Boolean, default: false }, placeholder:{ type: String, default: "" }, required: { type: Boolean, default: false }, /** * 验证表达式 */ vtype:{ type: String, default: "" }, onblur:Function, onfocus:Function, conf:Object }, data(){ return { currentValue: this.value, iserror:false, isempty:true, checkReq:true } }, computed: { right :function () { return calcRight(this); } }, mounted(){ this.valid(this.required); }, methods: { valid(chkReq_) { var val=this.currentValue; if(chkReq_ && this.required){ if(RxUtil.isEmpty(val)){ // this.iserror=true; return false; } } if(!this.vtype) { // this.iserror=false; return true; } var validFunc=VTypes[this.vtype]; if(typeof validFunc=="undefined") { // this.iserror=false; return true; } //验证 var rtn= validFunc.valid(val); // this.iserror=!rtn; return rtn; }, blurHandler(e) { // this.iserror=!this.valid(this.checkReq); this.onblur && this.onblur(e); }, focusHandler(e) { this.showClear = true; this.onfocus && this.onfocus(e); }, clearInput(){ this.currentValue = ''; if(this.required){ // this.iserror=true; } } }, watch: { currentValue: function (val, oldVal){ this.$emit('input', this.currentValue); //是否为空 this.isempty=RxUtil.isEmpty(val); }, value :function(val,oldVal){ if(val!=oldVal){ this.currentValue=this.value; } } } } </script> <style scoped> .box-custom-component::after{ content: ''; display: block; clear: both; } .box-custom-component input{ float: left; width:calc(100% - .65rem); } .box-custom-component a{ float: left; width: .65rem; } </style>
定义好组件后,使用方法如下:
1.import 组件
import RxInput from '@/components/common/form/RxInput';
2.注册组件
Vue.component(RxInput.name, RxInput);
3.使用组件
<rx-input v-model="data.email" permissionkey="email" :required="true" vtype="email" :readonly="false" :permission="" ></rx-input>
这里我们定义了v-model 我们通过将数据绑定到组件上实现数据的双向绑定。
实现双向绑定,需要注意两点:
1.定义一个value 的属性。
在上面组件的代码中,我们可以看到我们定义了一个value属性。
在只读的情况下 直接绑定显示。
<div v-if="right=='r'" class="readOnlyBgColor">{{value}}</div>
另外我们在data定义上,将value 赋值给了 currentValue 。这个值绑定到输入控件上。
2.数据改变时调用方法。
this.$emit('input', this.currentValue);
这样就实现了数据的双向绑定。
希望本文所述对大家vue.js程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- Merryland《Merryland》[320K/MP3][69.48MB]
- 苏芮.1991-停在我心里的温柔【福茂】【WAV+CUE】
- 苏慧伦.2020-面面Every.Side.of.Me【相信音乐】【WAV+CUE】
- 群星.1991-不一样的感觉REMIX【宝丽金】【WAV+CUE】
- 银霞.1983-《你那好冷的小手》珍藏纪念版[WAV分轨]
- 银霞.2002-《国语巨星专辑VOL.3》2CD台湾版[WAV+CUE]
- 金池刘罡《昨日情歌HQ》头版限量编号[低速原抓WAV+CUE]
- 段奥娟.2024-23·岁碎念【白米范】【FLAC分轨】
- 顺子.2001-昨日·唯一·更多·顺子自选辑【魔岩】【WAV+CUE】
- 无印良品.1999-私藏三首·原创相对论(EP)【滚石】【WAV+CUE】
- Merryland《Merryland》[FLAC/分轨][170.22MB]
- 庄锭欣 ELKIE《没有你的未来》[320K/MP3][15.78MB]
- 庄锭欣 ELKIE《没有你的未来》[FLAC/分轨][36MB]
- TchaikovskySwanLake,Op.20柴可夫斯基天鹅湖(2013)SACD[Hi-Res]
- 24bit达人艺典《琴歌终极版》整轨WAV+CUE