当前位置: 首页 > news >正文

【vue2】组件写法

  • 组件基本骨架
<template><div class="my-component"><!-- 组件的 HTML 结构 --><h1>{{ title }}</h1><!-- 事件绑定 --><button @click="handleClick">点击我</button><!-- 输入框与双向数据绑定 --><input v-model="inputValue" placeholder="请输入内容" /><!-- 列表渲染 --><ul><li v-for="(item, index) in items" :key="index">{{ item }}</li></ul></div>
</template><script>
export default {name: 'MyComponent', // 组件的名称props: {initialTitle: {type: String,default: '默认标题'}},data() {return {title: this.initialTitle, // 通过 props 初始化的数据inputValue: '', // 绑定输入框的值items: ['Item 1', 'Item 2', 'Item 3'] // 列表数据};},methods: {handleClick() {alert('按钮被点击了!');this.title = '标题已更新'; // 更新 title}}
};
</script><style scoped>
/* 样式作用域只在当前组件生效 */
.my-component {padding: 20px;background-color: #f9f9f9;
}.my-component h1 {color: #333;
}.my-component button {padding: 10px 20px;background-color: #42b983;color: white;border: none;border-radius: 5px;cursor: pointer;
}.my-component button:hover {background-color: #38a169;
}
</style>
  1. props(组件的输入参数):父调用 输入的参数
  2. name :组件名称设置为 MyComponent,可以通过 name 来识别和复用
  3. data(组件的内部状态):定义数据,把父调用的赋值
  • 父组件调用
<template><div><!-- 传递 props --><MyComponent initialTitle="初始标题"></MyComponent></div>
</template><script>
import MyComponent from './MyComponent.vue';export default {components: {MyComponent}
};
</script>
http://www.lryc.cn/news/446537.html

相关文章:

  • 5G 扬帆新质跃,技术蝶变开新篇-第七届“绽放杯”5G应用征集大赛 5G应用融合技术专题赛圆满收官
  • 3d gaussian splatting公式推导
  • 金属增材制造咋突破?纳米纹理粉末如何助力金属增材制造?
  • openpnp - 为了防止物料操作混乱,做一张物料分布位置图清晰一些
  • 懒人帮美食系统小程序的设计
  • David律所代理Jose Martin幽默水果版权首发维权,尚未TRO
  • 读构建可扩展分布式系统:方法与实践15可扩展系统的基本要素
  • 解决VisualStudio msvsmon.exe远程调试器未启动问题
  • 如果淘汰是注定的,那么读书还有意义吗?
  • Python 中 三种常用的绘图方式 ! ! !
  • 统一回复OneAPI:failed to get gpt-3.5-turbo token encoder的解决办法
  • Flash Attention是怎么做到又快又省显存的?
  • CAN报文ID过滤
  • ELK-05-skywalking监控SpringCloud服务日志
  • 17年数据结构考研真题解析
  • nginx 安装(Centos)
  • 异步编程利器:深入解析 Python 异步并发库 Gevent
  • Python pyusb 使用指南【windows+linux】
  • Xcode报错:The request was denied by service delegate (SBMainWorkspace)
  • 面试系列-携程暑期实习一面
  • 你以为建站很复杂?Baklib 5分钟解决你的痛点
  • 极狐GitLab 17.4 重点功能解读【二】
  • LVS-DR实战案例,实现四层负载均衡
  • 网游和3A类型游戏的CPU选择分析
  • 2024免费录屏软件的宝藏功能与实用技巧
  • linux---进程程序替换详解
  • 笔试编程-百战成神——Day01
  • Qt+toml文件读写
  • 浅谈C++之指针
  • 在虚幻引擎中实时显示帧率