当前位置:网站首页>事件绑定触发
事件绑定触发
2022-08-11 05:17:00 【-加油】
1、vue App.vue
<template>
<div id="app">
<div @click="clickfun">Hello</div>
</div>
</template>
<script>
export default {
data() {
return {
}
},
methods:{
clickfun(){
console.log('事件触发');
}
}
}
</script>
<style>
</style>
2、react App.js
import React from "react"
//函数式组件
function Hello () {
//定义的函数
const clickfun = () => {
console.log('函数组件的事件被触发了')
}
// function clickfun () {
// console.log('函数组件的事件被触发了')
// }
return <div onClick={
clickfun}>Hello,函数式组件</div>
}
//类组件
class HelloComponent extends React.Component {
//定义的函数 需要用箭头函数语法
clickfun = () => {
console.log('类组件的事件被触发了')
}
//如果不用箭头函数 需要如下改变this指向
// clickfun() {
// console.log('类组件的事件被触发了')
// }
// constructor() {
// super()
// this.clickfun = this.clickfun.bind(this)
// }
render () {
return <div onClick={
this.clickfun}>Hello,类组件</div>
}
}
function App () {
return (
<div className="App">
{
/* 使用方式跟vue组件一样 */}
<Hello></Hello>
<HelloComponent></HelloComponent>
</div>
)
}
export default App
传参时react需要将其改造成箭头函数
import React from "react"
//函数式组件
function Hello () {
//定义的函数
const clickfun = (msg) => {
console.log(msg);
console.log('函数组件的事件被触发了')
}
//改造成箭头函数
return <div onClick={
() =>clickfun('hhhh')}>Hello,函数式组件</div>
}
//类组件
class HelloComponent extends React.Component {
//定义的函数
clickfun = (msg) => {
console.log(msg);
console.log('类组件的事件被触发了')
}
render () {
//改造成箭头函数
return <div onClick={
()=>this.clickfun('hhhh')}>Hello,类组件</div>
}
}
function App () {
return (
<div className="App">
{
/* 使用方式跟vue组件一样 */}
<Hello></Hello>
<HelloComponent></HelloComponent>
</div>
)
}
export default App
而vue直接传
<template>
<div id="app">
<div @click="clickfun('hhh')">Hello</div>
</div>
</template>
<script>
export default {
data() {
return {
}
},
methods:{
clickfun(msg){
console.log(msg);
console.log('事件触发');
}
}
}
</script>
<style>
</style>
边栏推荐
猜你喜欢
【CSDN21天学习挑战赛】第一天,配置环境外加实现mnist手写数字识别
C语言——文件操作(2)文件的读写操作
task06 PyTorch生态
QT GrabWindow截取屏幕
【翻译】博客游戏项目Q1K3 – 制作
(1) Docker installs Redis in practice (one master, two slaves, three sentinels)
(1) Construction of a real-time performance monitoring platform (Grafana+Influxdb+Jmeter)
C语言自定义数据类型——联合体
Flask framework to study: the debug and configuration items
07-JS事件:事件类型、事件对象、事件传播、事件委托
随机推荐
LeetCode1166. Designing File Systems
CSDN 社区内容创作规范
C语言文件操作——数据文件类型、文件判断、文件缓冲区详解
Summary: Cross Validation
[转载]Verilog testbench总结
pytorch中tensor 生成的函数
05-JS中的BOM和DOM
吃瓜教程task04 第5章 神经网络
Minecraft
c pointer learning (1)
【网站小白】mySQL数据库异常断开
gradle-wrapper.jar说明
【C语言从初阶到进阶】第二篇 初始C语言(二)
Chapter 13 Class Inheritance
C语言——文件操作(2)文件的读写操作
(3) Construction of a real-time performance monitoring platform (Grafana+Prometheus+Node_explorer+Jmeter)
Redis - the solution to the failure of connecting to the redis server in linux using jedis
[Verilog] I2S Master Test Bench
ClionIDE compiles by specifying the compiler
win下clion打包的.exe文件在无运行环境的电脑运行显示缺失各种.dll