vue访问组件的数据和方法

news/2024/10/4 15:11:46/

组件源码

javascript"><template><view class="c_container" :style="myStyle" @click="clickCust"><view style="font-size: 18px;text-align: center;">{{item.name}}</view><view style="margin-top: 10px;font-size: 10px;"><view style="text-align: center;">{{item.sex+"&nbsp;&nbsp;&nbsp;"+item.age+"岁"}}</view></view></view></template><script>export default {name: "Customer",props: {item: {type: Object,default: {id: 0,name: "王美丽",sex: '女',age: 24}},selected: {type: Boolean,default: false}},watch: {selected(val) {console.log("watch,selected", this.selected);this.isSelected = this.selected;}},data() {return {isSelected: false,selectedColor: '#ffffff',unselectedColor: '#f4f4f4',borderColor: '#1296db',myStyle: ''};},mounted() {this.setStyle();},methods: {setStyle() {if (this.isSelected) {//选中this.myStyle = {"background-color": this.selectedColor,border: "1px solid " + this.borderColor};} else {//没选中this.myStyle = {"background-color": this.unselectedColor,border: "none"};}console.log("this.myStyle", this.myStyle);},clickCust() {this.isSelected = !this.isSelected;console.log("clickCust,isSelected", this.isSelected);this.setStyle();this.$emit("clickCust", {id: this.item.id,isSelected: this.isSelected});}}}
</script><style>.c_container {/* float: left; */height: 60px;width: 150px;border: 1px solid;padding-top: 20px;border-radius: 15px;margin-right: 10px;}
</style>

调用组件

javascript"><template><view class="s_container"><uni-nav-bar :fixed="true" dark background-color="#007AFF" title="预约咨询" class="nav" left-icon="left"@clickLeft="back" /><view class="c_box"><view><image mode="widthFix" style="width: 100%"src="/static/images/myimages/fetal_counseling/fetal_counseling_1.jpg"></image></view><view class="box2"><view class="box2_title_box"><view class="box2_title">选择服务客户</view><view class="box2_right" @click="addCustomer">+添加</view></view><scroll-view :scroll-x="true" style="width: 100%;padding: 10px 0;"><view style="width: 2000px;"><!-- <view class="card" v-for="item in customers"style="float:left;height: 60px;width: 150px;border: 1px solid ;padding-top: 20px;border-radius: 15px;margin-right: 10px;"><view style="font-size: 18px;text-align: center;">{{item.name}}</view><view style="margin-top: 10px;font-size: 10px;"><view style="text-align: center;">{{item.sex+"&nbsp;&nbsp;&nbsp;"+item.age+"岁"}}</view></view></view> --><Customer ref="cust" v-for="item in customers" :item="item" style="float: left;"@clickCust="clickCustomer"></Customer></view></scroll-view><view style="clear: both;"></view><view><button @click="test">测试</button></view></view></view></view>
</template><script>import Customer from "@/components/Customer.vue"export default {components: {Customer},data() {return {customers: [{id: 1,name: '王美丽',sex: '女',age: '24'}, {id: 2,name: '王美丽1',sex: '女',age: '24'}, {id: 3,name: '王美丽2',sex: '女',age: '24'}, {id: 3,name: '王美丽3',sex: '女',age: '24'}, {id: 4,name: '王美丽4',sex: '女',age: '24'}]}},methods: {back() {uni.showToast({title: '回退'})},addCustomer() {uni.showToast({title: '添加客户'})},clickCustomer(e) {console.log("clickCustomer,e", e);},test() {console.log("cust", this.$refs.cust);//调用数据console.log("cust[0]", this.$refs.cust[0].isSelected);//调用方法this.$refs.cust[0].clickCust();}}}
</script><style>.s_container {background-color: rgba(244, 244, 244, 1);}.c_box {padding: 8px 8px;}.box2 {padding: 5px;margin-top: 5px;background-color: #ffffff;}.box2_title_box {position: relative;}.box2_title {font-size: 14px;font-weight: bold;}.box2_right {position: absolute;right: 20px;top: 0px;}
</style>

调用组件数据和方法的代码

javascript">test() {console.log("cust", this.$refs.cust);//调用数据console.log("cust[0]", this.$refs.cust[0].isSelected);//调用方法this.$refs.cust[0].clickCust();
}

http://www.ppmy.cn/news/1534478.html

相关文章

C++模拟实现vector容器【万字模拟✨】

更多精彩内容..... &#x1f389;❤️播主の主页✨&#x1f618; Stark、-CSDN博客 本文所在专栏&#xff1a; 学习专栏C语言_Stark、的博客-CSDN博客 项目实战C系列_Stark、的博客-CSDN博客 数据结构与算法_Stark、的博客-CSDN博客 座右铭&#xff1a;梦想是一盏明灯&#xff…

lambda表达式底层实现:反编译LambdaMetafactory + 转储dump + 运行过程 + 反汇编 + 动态指令invokedynamic

一、结论先行 lambda 底层实现机制 1.lambda 表达式的本质&#xff1a;函数式接口的匿名子类的匿名对象 2.lambda表达式是语法糖 语法糖&#xff1a;编码时是lambda简洁的表达式&#xff0c;在字节码期&#xff0c;语法糖会被转换为实际复杂的实现方式&#xff0c;含义不变&am…

Arduino UNO R3自学笔记17 之 Arduino为啥要用中断?

注意&#xff1a;学习和写作过程中&#xff0c;部分资料搜集于互联网&#xff0c;如有侵权请联系删除。 前言&#xff1a;学习Arduino中断的概念及其功能。 1.什么是中断&#xff1f; 单片机在执行程序时&#xff0c;发生一些其它紧急的事情&#xff0c;单片机将立即暂停当前…

Python编码系列—Python访问者模式:为对象结构添加新功能的艺术

&#x1f31f;&#x1f31f; 欢迎来到我的技术小筑&#xff0c;一个专为技术探索者打造的交流空间。在这里&#xff0c;我们不仅分享代码的智慧&#xff0c;还探讨技术的深度与广度。无论您是资深开发者还是技术新手&#xff0c;这里都有一片属于您的天空。让我们在知识的海洋中…

工作笔记20240927——vscode + jlink调试

launch.json的配置&#xff0c;可以用的 {"name": "Debug","type": "cppdbg","request": "launch","miDebuggerPath": "./arm-gnu-toolchain-12.2.rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-g…

看480p、720p、1080p、2k、4k、视频一般需要多大带宽呢?

看视频都喜欢看高清&#xff0c;那么一般来说看电影不卡顿需要多大带宽呢&#xff1f; 以4K为例&#xff0c;这里引用一位网友的回答&#xff1a;“视频分辨率4092*2160&#xff0c;每个像素用红蓝绿三个256色(8bit)的数据表示&#xff0c;视频帧数为60fps&#xff0c;那么一秒…

Spring:强制登陆与拦截器

1.只使用session验证 &#xff08;1&#xff09;第一步&#xff1a;用户登陆时存储session ApiOperation("用户登陆") PostMapping("/login") public AppResult login(HttpServletRequest request,RequestParam("username") ApiParam("用…

C++——函数功能是:将两个两位数的正整数a、b合并成一个整数c,合并规则是将a的十位和个位分别放在c的千位和个位,将b的十位和个位分别放在c的十位和百位。

没注释的源代码 #include <iostream> using namespace std; int he(int x,int y); int main() { int a,b; cout<<"请输入两个两位数的正整数:"; cin>>a>>b; cout<<"合并结果为:"<<he(a,b); return 0; } int he(i…