vm.$props
当前组件接收到的 props 对象。Vue 实例代理了对其 props 对象属性的访问
ps:props 里声明了继承下来的内容
vm.$attrs
包含了父作用域中不作为 prop 被识别 (且获取) 的特性绑定 (class
和 style
除外)。当一个组件没有声明任何 prop 时,这里会包含所有父作用域的绑定 (class
和 style
除外),并且可以通过 v-bind="$attrs"
传入内部组件——在创建高级别的组件时非常有用
ps:只包括没 props 的内容
vm.$listeners
包含了父作用域中的 (不含 .native
修饰器的) v-on
事件监听器。它可以通过 v-on="$listeners"
传入内部组件——在创建更高层次的组件时非常有用
ps:绑定的方法(v-on: @) html中大写无效, 模板中大小写[-]全部保留
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>关于 vm.$props, vm.$attrs, vm.$listeners 的理解</title>
<style></style>
</head>
<body>
<div id="swq">
<box></box>
</div>
<script type="text/x-template" id="box-template">
<c-a @onFunA1="funA1" @on-fun-a1="funA1" @onfuna1="funA1" :box1="box1" :box2="box2"></c-a>
</script>
<script type="text/x-template" id="cA-template">
<div>
<div>$props props 里声明了继承下来的内容</div>
<div>$attrs 只包括没 props 的内容</div>
<div>$listeners 绑定的方法(v-on: @) html中大写无效, 模板中大小写[-]全部保留</div>
</div>
</script>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
<script type="text/javascript">
var cA = {
template: "#cA-template",
props: ["box1"],
created() {
this.$emit("onFunA1");
console.log(this.$props)
console.log(this.$attrs)
console.log(this.$listeners)
},
};
var box = {
template: "#box-template",
data() {
return {
box1: "box1box1",
box2: "box2box2",
}
},
components: {
"c-a": cA
},
methods: {
funA1() {
console.log("funA1")
},
},
};
var vu = new Vue({
el: "#swq",
components: {
box: box
},
})
</script>
</body>
</html>
end