当前位置:网站首页>事件绑定触发
事件绑定触发
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>
边栏推荐
猜你喜欢
随机推荐
如何设置pip安装的国内源
bootstarp作业一:制作分页器
C language file operation - detailed explanation of data file type, file judgment, and file buffer
task05 PyTorch可视化
C语言结构体详解 (2) 结构体内存对齐,默认对齐数
C语言版——通讯录进阶(文件版本)
【记录】TypeScript
QT Mat转HObject和HObject转Mat 图像视觉处理
05-JS中的BOM和DOM
【背包】采药题解
[C language advanced] The first in-depth analysis of the storage of integer data in memory (1)
Introduction of several ways to initialize two-dimensional arrays in C language (private way to initialize large arrays)
c 指针学习(1)
【CSDN21天学习挑战赛】第一天,配置环境外加实现mnist手写数字识别
QT circle函数(图片标注)
【win10+cuda7.5+cudnn6.0安装caffe①】安装cuda和cudnn
Minecraft
Chapter 4-2 a complex type (pointer)
task06 PyTorch生态
Win下安装不同版本的MinGW(g++/gcc)以及对应clion编辑器的配置









