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. C++11标准库<chrono><future> <atomic><condition_variable><mutex><thread>梳理 (5万字)

    <chrono> C++11中提供了日期和时间相关的库chrono. chrono库主要包含三种类型的类:时间间隔duration.时钟clocks.时间点time point. 时间间隔 ...

  2. 解决方案 | vb记住上次打开的文件夹

      Private Sub Button_ImportBasicData_Click(sender As Object, e As EventArgs) Handles Button_ImportBa ...

  3. 微软账号密码修改后提示密码错误的解决方法(远程桌面&smb共享访问等)

    众所周知,自从微软将Microsoft账户与Windows账号强制绑定后,使用起来便一直有诸多困难,在Microsoft Support和搜索引擎长期搜索解决方案未果,今天偶然在一个佬的博客翻到了这个 ...

  4. C# 泛型SQLHelper<T>类

    示例[1] 1.创建SQLHelper类 using System.Collections.Generic; using System.Configuration; using System.Data ...

  5. vue进阶一~数据响应式,数据响应到视图层,手写v-model,订阅发布者模式,

    1,数据响应式 当数据发生改变的时候,我们立即知道数据发生改变,并做出相关的操作:发送请求,打印文字,操作DOM等. 1.1,vue实现数据响应的原理 vue中使用了两种模式来实现数据响应式,分别是v ...

  6. JMeter 后置处理器之JSON提取器

    后置处理器之JSON提取器 By:授客 QQ:1033553122 测试环境 JMeter 5.4.1 插件介绍 JSON后置处理器(PostProcessor)允许使用 JSON Path 语法从J ...

  7. java面试一日一题:java内存区域

    问题:请讲下java内存区域? 分析:该问题比较容易和jvm内存模型相混淆,内存模型说的是java的内存规范,规定的是多线程下访问主内存的规则:而内存区域,说的是java运行时的内存划分,换句话说就是 ...

  8. Intent 显示与隐式了解认识

    显示Intent 用于精确匹配,指定跳转目标 1.在intent构造函数中调用 Intent intent = new Intent(this,XX.class); 2.调用意图对象的setClass ...

  9. 2023/4/15 SCRUM个人博客

    1.我昨天的任务 获得了人脸识别作弊检测和绘制界面的分工,准备先从作弊检测入手 2.遇到了什么困难 对作弊检测的组件不熟悉,进展缓慢,需要进行对点的学习 3.我今天的任务 初步学习cython

  10. 从pytest源码的角度分析pytest工作原理

    从pytest源码的角度分析pytest工作原理 从 pytest 源代码的角度来分析其工作原理,我们需要关注几个关键的部分,特别是 pytest 的启动过程以及测试的收集与执行.下面是基于 pyte ...