首页 > 其他分享 >【快应用】表单组件的model属性实际运用

【快应用】表单组件的model属性实际运用

时间:2023-04-23 10:01:01浏览次数:35  
标签:sampleName 绑定 value 表单 组件 input model

 【关键词】

表单组件、model

 

【问题背景】

在使用表单组件时,往往需要同时绑定值和 change 事件动态更新数据,当表单交互较多的场景下会有大量与业务无关的代码。

快应用从1100版本开始引入 model 指令,使用 model 指令可以简化代码逻辑,在框架内部对值进行绑定与更新,实现双向绑定的效果。

 

【实现方法】

model 指令本质上是一个语法糖,原理是在编译时为组件自动绑定了 change 事件,通过 change 事件为双向绑定的数据更新变更后的值,目前仅支持在input,select和textarea中使用。

使用 model 指令实现双向绑定

Input组件type为text时:

 <text class="txt">input value:{{ inputValue }}</text>
 <input class="btn" model:value="{{inputValue}}" />

Input组件type为raido时, 可以为“model:value”绑定一个数组,该数组将包含所有选中的 checkbox 的 value 值:

<div>
      <input type="checkbox" id="jack" value="Jack" model:value="{{checkedNames}}"></input>
      <label target="jack">Jack</label>
    </div>
    <div>
      <input type="checkbox" id="john" value="John" model:value="{{checkedNames}}"></input>
      <label target="john">John</label>
    </div>
    <div>
      <input type="checkbox" id="mike" value="Mike" model:value="{{checkedNames}}"></input>
      <label target="mike">Mike</label>
    </div>
    <text>Checked names: {{ checkedNames }}</text>

Select组件中使用model:

    <text class="txt">selected value:{{ selectedValue }}</text>
    <select class="select" model:value="{{selectedValue}}">
      <option value="item0">item0</option>
      <option value="item1">item1</option>
      <option value="item2">item2</option>
      <option value="item3">item3</option>
      <option value="item4">item4</option>
    </select>

 Textarea中使用model: 

    <text class="txt">input textarea value:{{ newValue }}</text>
    <textarea class="btn" model:value="{{newValue}}"></textarea>

组件中使用model

1.定义一个含有表单元素的自定义组件,当触发change事件时,“update:sampleName”事件会向父级传递值。

“update:”为固定前缀。

“sampleName”为当前组件和父级双向绑定的属性名称。

<template>
  <input class="btn" value="{{sampleName}}" @change="$emit('update:sampleName', evt.value)"></input>
</template> 
<script>
    export default {
        props: ['sampleName']
    }
</script>

2. 父组件中正常引入组件,并使用“model:sample-name”,即可完成自定义组件的 sampleName 属性双向绑定。

    <text class="title">comp use model</text>
    <div style="flex-direction: column">
      <text>sampleName: {{ name }}</text>
      <model-sample model:sample-name="{{name}}"></model-sample>
    </div>

Tips请勿在使用 model 指令时,再为组件手动绑定 change 事件,如果同时使用 toolkit 编译时,只会保留开发者定义的 change 事件,导致 model 指令失效。

 

【完整代码】

hello.ux

<import name="model-sample" src="./comp.ux"></import>
<template>
  <div class="container">
    <text class="txt">input value:{{ inputValue }}</text>
    <input class="btn" model:value="{{inputValue}}" />
    <text class="txt">selected value:{{ selectedValue }}</text>
    <select class="select" model:value="{{selectedValue}}">
      <option value="item0">item0</option>
      <option value="item1">item1</option>
      <option value="item2">item2</option>
      <option value="item3">item3</option>
      <option value="item4">item4</option>
    </select>
    <text class="txt">input textarea value:{{ newValue }}</text>
    <textarea class="btn" model:value="{{newValue}}"></textarea>

    <text class="title">input type is radio</text>
    <div>
      <input type="checkbox" id="jack" value="Jack" model:value="{{checkedNames}}"></input>
      <label target="jack">Jack</label>
    </div>
    <div>
      <input type="checkbox" id="john" value="John" model:value="{{checkedNames}}"></input>
      <label target="john">John</label>
    </div>
    <div>
      <input type="checkbox" id="mike" value="Mike" model:value="{{checkedNames}}"></input>
      <label target="mike">Mike</label>
    </div>
 
    <text>Checked names: {{ checkedNames }}</text>
    <text class="title">comp use model</text>
    <div style="flex-direction: column">
      <text>sampleName: {{ name }}</text>
      <model-sample model:sample-name="{{name}}"></model-sample>
    </div>
  </div>
</template>
<style>
  .container {
    flex-direction: column;
  }
  .btn {
    width: 90%;
    height: 100px;
    border: 1px solid #000000;
    margin-bottom: 10px;
  }
  .select {
    width: 90%;
    height: 100px;
    border: 1px solid #000000;
  }
  .txt {
    font-size: 40px;
  }
  .title {
    font-size: 50px;
    color: red;
  }
</style>
<script>
  export default {
    data: {
      inputValue: 'new value',
      selectedValue: 'item0',
      newValue: 'textarea value',
      checkedNames: [],
      name: "demo sample"
    },
  }
</script>

comp.ux

<template>
  <input class="btn" value="{{sampleName}}" @change="$emit('update:sampleName', evt.value)"></input>
</template>

 

<script>
    export default {
        props: ['sampleName']
    }
</script>
<style>
    .btn {
      width: 90%;
      height: 100px;
      border: 1px solid #000000;
      margin-bottom: 10px;
    }
</style>

【效果图】

cke_30976.png

 

 欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

标签:sampleName,绑定,value,表单,组件,input,model
From: https://www.cnblogs.com/developer-huawei/p/17345620.html

相关文章

  • layui学习3(静态表格、表单元素)
    1.静态表格表格:<tableclass="layui-table"></table>属性:lay-even实现隔行换色效果lay-skin="属性值"【line(行边框风格)、row(列边框风格)、nob(无边框风格)】lay-size="属性值"【sm(小尺寸)、lg(大尺寸)】表格-页面元素-Layui(ilayuis.com)<!DOCTYPEht......
  • Django—Form两种解决表单数据无法动态刷新的方法
    一、无法动态更新数据的实例#Createyourmodelshere.classClasses(models.Model):title=models.CharField(max_length=32)def__str__(self):returnself.titleclassTeacher(models.Model):name=models.CharField(max_length=32)t2c=model......
  • 一个低代码拖拉拽的表单编辑器,开源咯!!!
    编辑器介绍先来个图,有个初步的认识抱歉,原谅图有点模糊哈github:https://github.com/Liberty-liu/Everright-formEditordemo:https://everright.site/zh-cn/module/formEditor/introduction.htmlEverright-formEditor是一个基于vue3的可视化编辑器,依赖于element-plus和vant进行开......
  • react18中antd的Upload组件上传头像,并且拿到服务器返回的头像的url地址在页面中显示头
    业务需求:上传头像,上传完毕后拿到头像的url,把头像展示在页面中,最终把头像url和其他用户信息一起发送给服务器 上传头像流程 导入Upload组件和图标(一个加号,一个加载中)import{Upload}from'antd';import{PlusOutlined,LoadingOutlined}from'@ant-design/ic......
  • toga高级组件
    Toga还提供了一些高级组件,如表格、滚动容器等,以满足更复杂的应用需求。以下是一些常用的高级组件的介绍和使用方法:表格-toga.Tabletoga.Table用于创建一个表格控件,用于显示数据。常用参数:id:表格控件的唯一标识符。data:表格的数据,格式为二维数组。headings:表格的标题,格式为......
  • # jquery # form表单上传文件
    form表单上传文件<formaction="/upload/"method="post"enctype="multipart/form-data">头像:<inputtype="file"name="head-pic">用户名:<inputtype="text"name="username">......
  • 论文解读(PGD)《Towards deep learning models resistant to adversarial attacks》
     论文信息论文标题:Towardsdeeplearningmodelsresistanttoadversarialattacks论文作者:AleksanderMadry,AleksandarMakelov,LudwigSchmidt,DimitrisTsipras,AdrianVladu论文来源:ICLR2018论文地址:download 论文代码:download视屏讲解:click1 介绍对抗攻击2......
  • 微信小程序:uni-app页面Page和组件Component生命周期执行的先后顺序
    目录H5微信小程序测试代码文档页面生命周期https://uniapp.dcloud.net.cn/tutorial/page.html#lifecycle组件生命周期https://uniapp.dcloud.net.cn/tutorial/page.html#componentlifecycle经测试,得出结论:H5和微信小程序的生命周期函数调用顺序不一致H5pagebeforeCreatepag......
  • vscode中react组件
    title:"vscode中ES7+React/Redux/React-Nativesnippets插件使用"date:2023-04-0723:21:32tags:['Vscode','插件']categories:["工具篇"]通过使用这个插件我们可以很方便的进行组件/方法/文件的导入本篇博客仅对插件进行介绍翻译,便于自己以后使用常用片段列表imr:......
  • System.ComponentModel.Win32Exception:拒绝访问
    调试.NETFramework4.6项目,通过创建新进程在程序中调用ProgramFiles下的其他可执行程序,运行报错:System.ComponentModel.Win32Exception:‘拒绝访问’System.ComponentModel.Win32Exception:‘Accessisdenied’ (图片源自客户公开,非实际项目,请勿对号入座。下述截图与该图片非......