学习最新vue20.17.0-事件处理

vue中文官网事件处理 | Vue.js (vuejs.org)

我在官网基础上,添加些代码,方便初学者学习,能够快速理解官网内容,掌握自己所需要的知识,以便节省宝贵的时间。

事件处理

监听事件

  我们可以使用 v-on 指令 (简写为 @) 来监听 DOM 事件,

  并在事件触发时执行对应的 JavaScript。用法:v-on:click="handler" 或 @click="handler"。
  
  事件处理器 (handler) 的值可以是:

        1、内联事件处理器:事件被触发时执行的内联 JavaScript 语句 (与 onclick 类似)。

        2、方法事件处理器:一个指向组件上定义的方法的属性名或是路径。
  

内联事件处理器

  内联事件处理器通常用于简单场景,例如:
  

演示代码:

     js 代码:
         const count = ref(0)
  
     template 代码:
        <button @click="count++">Add 1</button>
        <p>Count is: {
 
 { count }}</p>
     演示结果:

        


方法事件处理器

  举例来说:
  

演示代码:

     js 代码:
         const name = ref('Vue.js')
         let say = ref('')
         let eventName= ref('')

         function greet(event) {
                        say.value=`Hello ${name.value}!`
                        // `event` 是 DOM 原生事件
                        if (event) {
                          eventName=event.target.tagName
                        }
                      }
  
     template 代码:
         <button @click="greet">greet()</button>
         <li>{
 
 {say}}</li>
         <li>{
 
 {eventName}}</li>
     演示结果:

        

方法与内联事件判断

  模板编译器会通过检查 v-on 的值是否是合法的 JavaScript 标识符或属性访问路径

  来断定是何种形式的事件处理器。

  举例来说,foo、foo.bar 和 foo['bar'] 会被视为方法事件处理器,

  而 foo() 和 count++ 会被视为内联事件处理器。
  

在内联处理器中调用方法

  除了直接绑定方法名,你还可以在内联事件处理器中调用方法。

  这允许我们向方法传入自定义参数以代替原生事件:
  

演示代码:

     js 代码:
         const sayText = ref('')
         function say(message) {
                            if (message) {
                              sayText.value =""
                              if(message === 'hello'){
                                sayText.value="hello,how are you"
                              }else {
                                sayText.value="good bye"
                              }
                            }
                          }
  
     template 代码:
        <button @click="say('hello')">Say hello</button>
        <button @click="say('bye')">Say bye</button>
        <table>{
 
 {sayText}}</table>
     演示结果:

        


在内联事件处理器中访问事件参数

  有时我们需要在内联事件处理器中访问原生 DOM 事件。

  你可以向该处理器方法传入一个特殊的 $event 变量,或者使用内联箭头函数:
  

演示代码:

     js 代码:
         const warnText = ref("")
         function warn(message, event) {
                            // 这里可以访问原生事件                                                                         warnText.value=event.target.innerText+":"+message
                          }
     template 代码:
        <button @click="warn('Form cannot be submitted yet.', $event)">
            Submit
        </button>

        <!-- 使用内联箭头函数, event1自定义变量接受事件变量-->
        <button @click="(event1) => warn('Form cannot be submitted yet. 01', event1)">
            Submit01
        </button>
        <table>{
 
 {warnText}}</table>
     演示结果:

        

Node.js 20.17.0 支持 **Vue 3**,对 **Vue 2** 的支持存在限制。 ### 回答问题: Node.js 20.17.0 是较新的 Node.js 版本,它默认启用了更多现代 JavaScript 特性,并对 ESM(ECMAScript Module)模块系统有更强的支持。然而,**Vue 2 官方并不完全支持 Node.js 16 以上的版本**,尤其是在构建工具链(如 Webpack 3/4 和 Vue CLI 3/4)方面存在兼容性问题。 具体来说: - ✅ **Vue 3**:完全支持 Node.js 20.17.0Vue 3 使用的是基于 Vite 或 Vue CLI 5+ 的构建系统,这些工具明确支持 Node.js 18+,因此在 Node.js 20.17.0 上运行和构建没有任何问题。 - ⚠️ **Vue 2**:**不推荐在 Node.js 20.17.0 上使用**。 原因如下: - Vue 2 的生态(如 `vue-cli-service`、`webpack@4`、`node-sass` 等)依赖的许多包已不再维护或与 Node.js 20 不兼容。 - `node-sass` 已弃用,其底层依赖 `libsass` 在 Node.js 20 中无法编译。 - 某些 polyfill 缺失(如 `global` 未定义),会导致运行时报错。 - 社区普遍建议升级到 Vue 3 或至少使用 Node.js 16 来维护 Vue 2 项目。 --- ### 解决方案(如果你必须使用 Vue 2): 1. **降级 Node.js 到 16.x LTS**(推荐长期维护 Vue 2 项目的方案) ```bash nvm install 16 nvm use 16 ``` 2. **替换 `node-sass` 为 `sass`(Dart Sass)** ```bash npm uninstall node-sass npm install sass sass-loader --save-dev ``` 并将所有 `.scss` 文件引用保持不变,Dart Sass 是纯 JS 实现,兼容更高版本 Node.js。 3. **手动添加 `global` 兼容层**(某些库报 `global is not defined`) 在 `vue.config.js` 中注入: ```js // vue.config.js const { defineConfig } = require('@vue/cli-service') module.exports = defineConfig({ configureWebpack: { plugins: [ { apply: (compiler) => { compiler.options.module.rules.push({ test: /\.js$/, loader: 'string-replace-loader', options: { search: 'process.env.VUE_ENV', replace: '""', }, include: /node_modules\/some-broken-package/, }) } } ] } }) ``` 或者在 `index.html` 中加入: ```html <script> if (!global) { window.global = window; } </script> ``` --- ### 推荐做法: > 升级到 **Vue 3 + Vite + Node.js 20+** 是最佳选择。Vue 3 性能更好、体积更小、API 更现代化(Composition API),并且完全适配现代工具链。 --- ### 示例代码(Vue 3 + Vite 项目结构) ```vue <!-- src/App.vue --> <script setup> import { ref } from 'vue' const count = ref(0) </script> <template> <div> <h1>Hello Vue 3 on Node.js 20.17.0!</h1> <p>Count: {{ count }}</p> <button @click="count++">Increment</button> </div> </template> <style scoped> h1 { color: #42b983; } </style> ``` ```js // vite.config.js import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' export default defineConfig({ plugins: [vue()], }) ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值