vue3 modifier 是什么?

modifier 中文意思为修饰符.

在vue3中主要是体现在v-model上,vue3允许我们添加自定义修饰符。

比如说这样:

<template>
<img alt="Vue logo" src="./assets/logo.png" />
<!-- <HelloWorld msg="Welcome to Your Vue.js App" /> -->
<Test v-model:userinput.trimall="userinput"></Test>
<p>{{userinput}}</p>
</template> <script setup>
import HelloWorld from "./components/HelloWorld.vue";
import Test from "./components/Test.vue";
import {ref} from 'vue'
const userinput = ref(''); </script>

我们自定义的修饰符在子组件中如何获取呢?其实很简单,

我们v-model:msg.trimall 传递到子组件中一共有两个属性,如下所示:

const props = defineProps({
userinput: {
type: String,
default: "",
},
userinputModifiers: {
type: Object,
default: () => ({}),
},
});

我们的子组件的自定义修饰符全部存放在userinputModifiers这个对象里面,这个对象key组成是 属性+Modifiers,所以

<Test v-model:something.tom="value"></Test>

我们的子组件就会收到除去something属性之外额外的属性 somethingModifiers。

接着上面的说下去,

userinputModifiers 会有一个属性是你的修饰符名称,比如:


所以我们就可以根据这个值做我们想做的事情了.

代码示例如下:
App.vue
<template>
<img alt="Vue logo" src="./assets/logo.png" />
<!-- <HelloWorld msg="Welcome to Your Vue.js App" /> -->
<Test v-model:userinput.trimall="userinput" ></Test>
<p>{{userinput}}</p>
</template> <script setup>
import HelloWorld from "./components/HelloWorld.vue";
import Test from "./components/Test.vue";
import {ref} from 'vue'
const userinput = ref(''); </script> <style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

Test.vue

<template>
<div>
<input :value="userinput" @input="onInput" type="text" />
</div>
</template> <script setup>
import { getCurrentInstance, defineProps, toRef, useAttrs } from "vue"; const props = defineProps({
userinput: {
type: String,
default: "",
},
userinputModifiers: {
type: Object,
default: () => ({}),
},
}); const userinput = toRef(props, "userinput");
const instance = getCurrentInstance(); console.log("value:", props);
function onInput({ target }) {
var value = props.userinputModifiers.trimall
? target.value.replace(/\s/g, "")
: target.value;
instance.emit("update:userinput", value);
}
</script>

注:

在vue3.2之前 还是有 useContext 这个函数的,之后这个函数被废弃了,取而代之的是 useAttrs(主要用来获取一些非props属性) 和 useSlots (如果你不编写JSX或者TSX,这个函数你应该用不到)

是故学然后知不足 23:29:33

vue3 modifier的更多相关文章

  1. vue2升级vue3指南(二)—— 语法warning&error篇

    本文总结了vue2升级vue3可能会遇到的语法警告和错误,如果想知道怎样升级,可以查看我的上一篇文章:vue2升级vue3指南(一)-- 环境准备和构建篇 Warning 1.deep /deep/和 ...

  2. [转]Java反射之如何判断类或变量、方法的修饰符(Modifier解析)

    Java针对类.成员变量.方法,有很多修饰符,例如public.private.static.final.synchronized.abstract等,这些修饰符用来控制访问权限或其他特性. 本文就用 ...

  3. jmeter 中的 HTTP URL Re-writing Modifier

    URL rewriting modifier,因为tomcat的session实现不是通过cookie的,而是通过session id的,就是说,用户登录有了session之后,tomcat就会维护一 ...

  4. 【转载】#273 - Parameter Modifier Summary

    Here's a summary of the different parameter modifiers and how the behavior changes for each, when us ...

  5. 解决Deprecated: preg_replace(): The /e modifier is deprecated, use

    使用php5.5运行ecshop的时候出现如下错误Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace ...

  6. 7.modifier插件的自定义和使用

    1.在plugins下面创建一个文件 modifier.changeDate.php 编写: <?php function smarty_modifier_changeDate($utime,$ ...

  7. Sample Code for Qp_preq_pub.Price_request Api to Simulate an Ask for Promotion Modifier

    DECLARE p_line_tbl QP_PREQ_GRP.LINE_TBL_TYPE; p_qual_tbl QP_PREQ_GRP.QUAL_TBL_TYPE; p_line_attr_tbl ...

  8. vue3+typescript引入外部文件

    vue3+typescript中引入外部文件有几种方法 (eg:引入echarts) 第一种方法: 1 indext.html中用script引入 <div id="app" ...

  9. 预计2019年发布的Vue3.0到底有什么不一样的地方?

    摘要: Vue 3.0预览. 原文:预计今年发布的Vue3.0到底有什么不一样的地方? 作者:小肆 微信公众号:技术放肆聊 Fundebug经授权转载,版权归原作者所有. 还有几个月距离 vue2 的 ...

  10. java反射(java.lang.reflect)---java.lang.reflect.Modifier中状态码

    1. 详情请看jvm(虚拟机)规范 java.lang.reflect.Modifier public static final int ABSTRACT 1024 public static fin ...

随机推荐

  1. 常用IDE(开发工具)

    一.开发工具 Visual Studio Microsoft Visual Studio(简称VS)是微软公司提供的IDE,可以在VS上编写C.C++.C#等多种语言的项目,所写的代码适用于微软支持的 ...

  2. leetcode简单(数组,字符串,链表):[1, 9, 13, 14, 20, 21, 26, 27, 35, 58]

    目录 1. 两数之和 9. 回文数 13. 罗马数字转整数 14. 最长公共前缀 20. 有效的括号 21. 合并两个有序链表 26. 删除有序数组中的重复项 27. 移除元素 35. 搜索插入位置 ...

  3. [rCore学习笔记 014]批处理系统

    写在前面 本随笔是非常菜的菜鸡写的.如有问题请及时提出. 可以联系:1160712160@qq.com GitHhub:https://github.com/WindDevil (目前啥也没有 本章目 ...

  4. pytest批量执行多个测试文件(批量执行一个文件夹下的所有测试用例)

    图片 代码 #!/usr/bin/env python # @File : test_runall.py import pytest import os # path = os.path.dirnam ...

  5. SpringTask

    SpringTask是spring提供的一个任务调度工具,按照约定的时间自动执行代码逻辑 定时任务框架,即定时自动执行某段代码 应用场景:信用卡每月还款提醒,火车售票系统处理未支付订单 cron表达式 ...

  6. idea快捷键Ctrl+alt+m:如何快速抽离部分方法

    Ctrl+alt+m 效果如下图

  7. Unity入门学习日记(一)

    UGUI的初步使用 1. Canvas 使用UI的时候,所有的UI元素都作为Canvas的子节点存在于Canvas中,如果创建UI元素时没有Canvas作为父节点,会自动生成一个Canvas,是一位& ...

  8. Segment-anything学习到微调系列_SAM初步了解

    Segment-anything学习到微调系列_SAM初步了解 前言 本系列文章是博主在工作中使用SAM模型时的学习笔记,包含三部分: SAM初步理解,简单介绍模型框架,不涉及细节和代码 SAM细节理 ...

  9. postfix&dovecot搭建邮件服务器

    本篇参考 https://blog.51cto.com/5001660/2377785和小翔博客https://www.liuyixiang.com/post/113927.html. 邮件发送和接受 ...

  10. 【Windows】(USB热点连接)使用手机给主机提供热点连网

    1.问题起源 昨天跟和几个哥们一起装机,发现安装好的系统, 直连网卡提示安装成功,但是网络设置显示未连接 找不到其他原因的办法下,我们看能不能使用手机对电脑进行连网 2.解决过程 我想到的是,先从手机 ...