当前位置:网站首页>事件绑定触发
事件绑定触发
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>
边栏推荐
猜你喜欢
更新啦~人生重开模拟器自制
最全总结Redis数据类型使用场景
吃瓜教程task05 第6章 支持向量机
05-Nodejs中的模块加载机制
npm install 时报 npm ERR Cannot read properties of null (reading ‘pickAlgorithm‘)
吃瓜教程task01 第2章 模型评估与选择
pytorch安装笔记——Pytorch在conda+CUDA10.2环境安装task01
Flask framework learning: template rendering and Get, Post requests
旅游住宿酒店14页
Summary: Cross Validation
随机推荐
C language - program compilation and execution, detailed macro definition
06-引入Express创建web服务器、接口封装并使用postman测试,静态资源托管
LeetCode43.字符串相乘 (大数相乘可用此方法)
QT circle函数(图片标注)
更新啦~人生重开模拟器自制
C语言版——通讯录进阶(文件版本)
第4章 复合类型-2(指针)
如何设置pip安装的国内源
task06 PyTorch生态
QT QLabel控件(使用详解)
Chapter 13 Class Inheritance
task03 Pytorch模型定义
博客帮助文档
【记录】没用知识点 - 智力题
(3) How Redis performs stress testing
第6章 分支语句和逻辑运算符
吃瓜教程task03 第4章 决策树
吃瓜教程task01 第1章 绪论
task02 fashion-mnist分类实战
C语言——文件操作函数 fseek、ftell、rewind详解