vue props传值失败 输出undefined的解决方法
作者:不垢 时间:2024-05-05 09:09:51
如果在prop中传的值为一个没有使用特殊命名规则的变量如:(type),可以顺利传值:
<code class="language-html"><div id="app">
<test :type="type"></test>
</div>
Vue.component("test", {
props: ['type'],
template: '<div @click="a">我是按钮{{type}}</div>',
methods: {
a() {
console.log(this.type);
}
}
});
var app = new Vue({
el: '#app',
data: {
type: 'test'
}
});</code>
而当这个变量为驼峰命名法如:(selectName),就会传不过去:
<div id="app">
<test :selectName="selectName"></test>
</div>
Vue.component("test", {
props: ['selectName'],
template: '<div @click="a">我是按钮{{selectName}}</div>',
methods: {
a() {
console.log(this.selectName);
}
}
});
var app = new Vue({
el: '#app',
data: {
selectName: 'test'
}
});
解决方法是把selectName标签改为select-Name:
<div id="app">
<test :select-Name="selectName"></test>
</div>
Vue.component("test", {
props: ['selectName'],
template: '<div @click="a">我是按钮{{selectName}}</div>',
methods: {
a() {
console.log(this.selectName);
}
}
});
var app = new Vue({
el: '#app',
data: {
selectName: 'test'
}
});
总结:如果为驼峰命名法传递的话,html不区分大小写(所有的都会转换为小写),所以testName 在html表现为 :test-name ,需要注意的是vue中使用props传递时最好不要用横杆如select-name 的写法,因为使用的时候this.select-name中的横杠会认为它是减号,导致辨认不出来。在定义事件的时候最好命名都为小写,如
this.$emit("selectchange","data");
不要写成
this.$emit("selectChange","data");
html同样认不出来,比较好的方式是这种
this.$emit("select-change","data");
来源:https://blog.csdn.net/qq_32921921/article/details/80610523
标签:vue,props,undefined
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Bootstrap table分页问题汇总
2024-04-29 13:12:07
Python实现连接两个无规则列表后删除重复元素并升序排序的方法
2021-05-23 10:35:54
![](https://img.aspxhome.com/file/2023/2/82602_0s.png)
SQL 特殊语句(学习笔记)
2024-01-13 16:24:40
Python2手动安装更新pip过程实例解析
2021-06-29 17:01:50
![](https://img.aspxhome.com/file/2023/9/115739_0s.png)
VS2013设置护眼背景颜色
2023-06-28 12:59:02
![](https://img.aspxhome.com/file/2023/4/78064_0s.png)
python 将dicom图片转换成jpg图片的实例
2023-08-24 11:12:27
Python中的四种交换数值的方法解析
2021-07-01 12:42:02
![](https://img.aspxhome.com/file/2023/6/131086_0s.png)
10款实用的jQuery图片插件
2011-01-25 12:28:00
![](https://img.aspxhome.com/file/UploadPic/20111/25/jquery_image_2-33s.jpg)
thinkphp的URL路由规则与配置实例
2023-11-16 02:58:57
卷积神经网络的发展及各模型的优缺点及说明
2023-04-04 21:58:06
![](https://img.aspxhome.com/file/2023/4/103224_0s.jpg)
Python代码覆盖率统计工具coverage.py用法详解
2021-02-02 22:55:51
Java基于正则表达式实现的替换匹配文本功能【经典实例】
2023-03-15 22:27:44
![](https://img.aspxhome.com/file/2023/7/73047_0s.jpg)
Python自动扫雷实现方法
2023-11-19 19:04:55
python实现经典排序算法的示例代码
2021-10-27 09:36:24
Golang官方限流器time/rate的使用与实现详解
2024-02-02 18:06:42
JS 用6N±1法求素数 实例教程
2024-04-16 08:44:59
python3图片转换二进制存入mysql
2023-05-18 07:06:50
Pytorch mask-rcnn 实现细节分享
2021-10-20 01:31:38
![](https://img.aspxhome.com/file/2023/0/79570_0s.jpg)
让JavaScript拯救HTML5的离线存储[译]
2009-05-15 12:26:00
![](https://img.aspxhome.com/file/UploadPic/20095/15/01-33s.jpg)
python抓取搜狗微信公众号文章
2021-10-25 17:56:08
![](https://img.aspxhome.com/file/2023/3/81653_0s.jpg)