$attrs
用于接收父組件通過v-bind傳遞的所有參數(shù)
使用場景:
多用于需要將從父組件接收的多個(gè)參數(shù)都傳遞給子組件時(shí)
補(bǔ)充:
使用props接收后的參數(shù)將從$attrs中
$listeners
用于接收父組件通過v-on傳遞的所有監(jiān)聽事件
使用場景:
多用于需要將從父組件接收的多個(gè)監(jiān)聽事件都傳遞給子組件時(shí)
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<cpn-a :obj="obj" :message="message" @input="handleInput" @blur="handleBlur"/>
</div>
<template id="cpnA">
<div>
<cpn-a-a v-bind="$attrs" v-on="$listeners" />
</div>
</template>
<template id="cpnAA">
<div>
<input type="text" v-model="count" v-on="$listeners">
</div>
</template>
<script>
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!',
obj: {
name: 'attrs'
}
},
components: {
cpnA: {
template: '#cpnA',
props: ['obj'],
components: {
cpnAA: {
template: '#cpnAA',
data () {
return {
count: ''
}
}
}
},
}
},
methods: {
handleInput(e) {
console.log('handleInput',e.target.value)
},
handleBlur(e) {
console.log('handleBlur',e.target.value)
}
}
})
</script>
</body>
</html>