<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12"></script>
<title>Vue自學(xué):props屬性的小駝峰寫法為什么不被支持</title>
</head>
<body>
<div id="app">
{{message}}
<!-- 當(dāng)props里面的屬性名稱使用了小駝峰標(biāo)識的時候,v-bind:中的屬性要在駝峰標(biāo)識的大寫字母前使用-,并且全部小寫 -->
<!-- 否則HTML標(biāo)簽不支持小駝峰標(biāo)識,會導(dǎo)致內(nèi)容無法展示 -->
<!-- 正確使用:v-bind:c-info:"info" -->
<!-- 這里的屬性值為該數(shù)組或?qū)ο蟮拿Q -->
<cpn v-bind:c-info="info"></cpn>
</div>
</body>
<template id="cpn">
<div>
<!-- 傳遞過來的值也養(yǎng)要個props里面的值一樣,要大寫 -->
<h3>{{cInfo}}</h3>
</div>
</template>
<script type="text/javascript">
//定義一個局部組件的常量
const cpn = {
// 綁定的是template標(biāo)簽中的id值
template:'#cpn',
data(){
},
props:{
//當(dāng)組件的傳遞值為小駝峰標(biāo)識時
//1、要么屬性的全部都要對應(yīng)大寫
//2、要么綁定屬性的該大寫字母前面要加-
// cInfo是另外取的屬性名稱
cInfo:{
type:Object,
default(){
return []
}
}
}
}
const app = new Vue({
el:'#app',
data:{
message:'tesh this message',
info:{
name:'梁凱達(dá)',
age:'27',
height:'178cm',
}
},
methods:{
},
computed:{
},
components:{
cpn,
}
})
</script>
</html>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號