Vue3值得注意的新特性之——触发组件选项

应用开发2025-11-05 13:54:4349

事件名

与组件和prop一样,值组件事件名提供了自动的得注大小写转换。如果用驼峰命名的新特性之选项子组件中触发一个事件,你将可以在父组件中添加一个kebabcase(短横线分割命名)的值组件监听器。

<my-component @my-event="doSomething"></my-component>  this.$emit(myEvent) 

与props命名一样,得注当你使用DOM模板时,新特性之选项我们建议使用kebabcase事件监听器。值组件如果你使用的得注是字符串模板,这个限制就不适用。新特性之选项

定义自定义事件

可以通过emits选项在组件上定义已发出的值组件事件。 

app.component(custom-form,得注 {   emits: [inFocus, submit] }) 

当在emits选项中定义了原生事件(如click)时,将使用组件中的新特性之选项事件替代原生事件侦听器。

验证抛出的值组件事件

与props类型验证类似,如果使用对象语法而不是得注数组语法,则可以验证它。新特性之选项

要添加验证,将为事件分配一个函数,源码下载该函数接收传递给$emit调用的参数,并返回一个布尔值以指示事件是否有效。 

app.component(custom-form, {   emits: {     // 没有验证     click: null,     // 验证submit 事件     submit: ({ email, password }) => {       if (email && password) {         return true       } else {         console.warn(Invalid submit event payload!)         return false       }     }   },   methods: {     submitForm() {       this.$emit(submit, { email, password })     }   } }) 

v-model事件

默认情况下,组件上的v-model使用modelValue作为props和update:modelValue做完事件。我们可以通过向v-model传递参数来修改这些名称:

<my-component v-model:title="bookTitle"></my-component> 

在本例中,子组件将需要一个 title prop 并发出 update:title 要同步的事件: 

app.component(my-component, {   props: {     title: String   },   emits: [update:title],   template: `     <input       type="text"       :value="title"       @input="$emit(update:title, $event.target.value)">   ` }) 

多个v-model绑定

通过利用特定prop和事件为目标的能力,正如我们之前在v-model参数中所学的那样,我们现在可以在单个组件实例上创建多个v-model绑定。

每个v-model将同步到不同的prop,而不需要在组件中添加额外的选项。 

<user-name   v-model:first-name="firstName"   v-model:last-name="lastName" ></user-name> app.component(user-name, {   props: {     firstName: String,     lastName: String   },   emits: [update:firstName, update:lastName],   template: `     <input        type="text"       :value="firstName"       @input="$emit(update:firstName, $event.target.value)">     <input       type="text"       :value="lastName"       @input="$emit(update:lastName, $event.target.value)">   ` }) 

处理v-model修饰词

在2.X中,我们对组件v-model上的.trim等修饰符提供了硬编码支持。但是,如果组件可以支持自定义修饰符,则会更有用。亿华云

在3.X中,添加到组件v-model的修饰符将通过modelModifiers prop提供给组件。

v-model有内置的修饰符——.trim,.number和.lazy。但是,在某些情况下,你可能还需要添加自己的自定义修饰符。

我们做个实例:将提供的字符串第一个字母大写。 

<my-component v-model.capitalize="myText"></my-component> app.component(my-component, {   props: {     modelValue: String,     modelModifiers: {       default: () => ({})     }   },   emits: [update:modelValue],   template: `     <input type="text"       :value="modelValue"       @input="$emit(update:modelValue, $event.target.value)">   `,   created() {     console.log(this.modelModifiers) // { capitalize: true }   } }) 

现在我们已经设置了 prop,我们可以检查 modelModifiers 对象键并编写一个处理器来更改发出的值。在下面的代码中,每当<input/> 元素触发 input 事件时,我们都将字符串大写。

<div id="app">   <my-component v-model.capitalize="myText"></my-component>   {{ myText }} </div> const app = Vue.createApp({   data() {     return {       myText:      }   } }) app.component(my-component, {   props: {     modelValue: String,     modelModifiers: {       default: () => ({})     }   },   emits: [update:modelValue],   methods: {     emitValue(e) {       let value = e.target.value       if (this.modelModifiers.capitalize) {         value = value.charAt(0).toUpperCase() + value.slice(1)       }       this.$emit(update:modelValue, value)     }   },   template: `<input     type="text"     :value="modelValue"     @input="emitValue">` }) app.mount(#app) 

对于带参数的 v-model 绑定,生成的 prop 名称将为 arg + "Modifiers": 

<my-component v-model:description.capitalize="myText"></my-component> app.component(my-component, {   props: [description, descriptionModifiers],   emits: [update:description],   template: `     <input type="text"       :value="description"       @input="$emit(update:description, $event.target.value)">   `,   created() {     console.log(this.descriptionModifiers) // { capitalize: true }   } }) 
本文地址:http://www.bhae.cn/news/132a29799570.html
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

全站热门

尽管Apache已经占据半片江山,但很多人仍然在寻找其他的方式去托管他们的站点,Apache不只是一个选择,其他很多优秀的服务器程序例如lighthttp和nginx也是不错的选择。本教程将要向您展现如何在Ubuntu操作系统上面安装,教程同样适用在Debian,尽管有一点点小差别,但并没有太大影响。怎么样,下面我们开始吧。0.开始注意为了完成教程中提到的操作,我们假设您已经安装了一个基本的Debian或者Ubuntu操作系统。怎么安装系统这是不同的教程了,这里就不再详细说明。本教程主要介绍如何简单获取Nginx+php的运行环境。1.安装Nginx第一步要做的就是从库中下载,这个操作是非常简单的。sudo apt-get install nginx更改默认的虚拟站点配置,文件在:sudo vim /etc/nginx/sites-available/default一个漂亮的关键配置是:server { listen 80; server_name localhost; access_log /var/log/nginx/localhost.access.log;## Default location location / { root /var/www; index index.php; }## Images and static content is treated different location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ { access_log off; expires 30d; root /var/www; }## Parse all .php file in the /var/www directory location ~ .php$ { fastcgi_split_path_info ^(.+.php)(.*)$; fastcgi_pass backend; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name; include fastcgi_params; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_intercept_errors on; fastcgi_ignore_client_abort off; fastcgi_connect_timeout 60; fastcgi_send_timeout 180; fastcgi_read_timeout 180; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; }## Disable viewing .htaccess & .htpassword location ~ /.ht { deny all; }}upstream backend { server 127.0.0.1:9000;} 好了,我们完成到这里,下面我们需要安装PHP所需要的文件。2.安装PHP很多站点都使用PHP提供动态内容,他们可能是一个wiki一个博客,或者一个论坛什么的。假如你现在运行的是Ubuntu,我们首先需要解决两种不同的deb包,假如你运行的是amd64版本,那需要更改i386为amd64注意:假如是debian则不需要做这些cd /tmpwget http://us.archive.ubuntu.com/ubuntu/pool/main/k/krb5/libkrb53_1.6.dfsg.4~beta1-5ubuntu2_i386.debwget http://us.archive.ubuntu.com/ubuntu/pool/main/i/icu/libicu38_3.8-6ubuntu0.2_i386.debsudo dpkg -i *.deb再次说明,这个只需要在Ubuntu上面操作就可以了,假如是最新版本的12.04可以省略掉这个。下面剩余部分将在Ubuntu和Debian上面都通用。我们需要增加APT源来增加deb包,所以我们可以使用php5.3和php-fpmsudo echo deb http://php53.dotdeb.org stable all >>/etc/apt/sources.list更新apt:sudo apt-get update下面我们开始安装PHP(第一部分)sudo apt-get install php5-cli php5-common php5-suhosin我们需要安装命令行,不然接下来就会出现些小问题sudo apt-get install php5-fpm php5-cgi假如你打算使用数据库或者一些需要的模块(例如:mcrypt,ldap,snmp等)你同样可以安装他们。好了,我们现在已经安装完了nginx和php一个注意点:假如你使用php的短标记(< ),你需要在php.ini中开启(fpm和cli同样),假如你不改变这些,你将看到代码以文本的方式显示。3.完成重启nginx服务器sudo /etc/init.d/nginx restart配置完成php.ini后需要重启php5-fpm,使用命令sudo /etc/init.d/php5-fpm restart全部完成,你可以运行了。4.测试我们可以在/var/www中创建一个index.php的文件,内容如下:< php phpinfo(); >通过浏览器访问,你可以看到php的一个大致的信息,假如没有则说明一些配置错误,需要重新检查。5.问题和最终提示假如你没有看到phpinfo,可能会出现一些错误,那可以执行:sudo tail /var/log/nginx/error.log查看错误日志,另外每次当你修改了php.ini后,需要重启php5-fpm。无需重启nginx。在默认的配置中nginx是启用了fastcgi的错误日志的,假如看到一个严重的错误(例如出现cannot redeclare class xyz),nginx可以看到这个“漂亮的”错误页面,那可能是一些错误发生了。假如这样你可以关闭,假如php5-fpm不能运行,你的php文件将不会被解析,并且nginx会显示一个错误页面。好了,我猜你已经完成了,并且现在使用nginx作为你的web服务器,nginx是一个小巧、漂亮并且你可以配置很多东西的一个东东,假如你需要伪静态,注意nginx不会运行 .htaccess 文件,你需要伪静态更改vhost配置。假如对在Linux下感兴趣的朋友可以查看《Linux下的DedeCMS站点高级安全策略》翻译:天涯  原文:http://www.howtoforge.com/installing-php-5.3-nginx-and-php-fpm-on-ubuntu-debian

华硕T20(品质稳定,适合办公与娱乐)

手机市场现状分析(如何在激烈竞争下抢占手机市场份额)

村田厨房电器的品质和性能如何?(了解村田厨房电器的特点和用户评价)

M-Audio音箱的音质与性能如何?(探索M-Audio音箱的卓越音质及功能特点)

网络U盘的使用教程(轻松实现文件共享和传输的利器)

康佳滚筒洗衣机(体验智能洗涤,让洗衣成为一种享受)

中兴双屏幕手机(开启双屏幕时代,领航智能手机发展)

热门文章

友情链接

滇ICP备2023000592号-9