当前位置:网站首页>事件绑定触发
事件绑定触发
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>
边栏推荐
猜你喜欢

task05 PyTorch可视化

pytorch安装笔记——Pytorch在conda+CUDA10.2环境安装task01

吃瓜教程task01 第1章 绪论

npm install 时报 npm ERR Cannot read properties of null (reading ‘pickAlgorithm‘)

RK3399上的Tengine实践笔记

Flask framework learning: template inheritance

03-npm安装包详解,解决npm下载慢的问题,引入nrm等

task06 PyTorch生态

【转载】CMake 语法 - 详解 CMakeLists.txt

C语言结构体详解 (2) 结构体内存对齐,默认对齐数
随机推荐
task03 Pytorch模型定义
扩展运算符和剩余参数rest
Flask framework to study: the debug and configuration items
【备忘】于仕琪的libfacedetection相关
C语言——动态内存分配常见的错误案例
C language - program compilation and execution, detailed macro definition
(3) Construction of a real-time performance monitoring platform (Grafana+Prometheus+Node_explorer+Jmeter)
08-Express路由详解
C语言——逆序输出字符串的函数实现
程序员小白的自我救赎之路。
C语言——程序的编译与执行、宏定义详解
看完这篇博客之后,含着泪学会了TCP/IP
标准模板题:采药
第13章类继承
LeetCode43.字符串相乘 (大数相乘可用此方法)
怎么用管理员方式打开压缩包
Chapter 13 Class Inheritance-1
Qt 字符串截取 查找字符串
【背包】采药题解
【分享】一个免费语料库