Vuex_通过props传递函数,顶替事件通知

传递给 子组件的函数prop可以来自自身的methods和data域.都是可以的.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/WEB-INF/webpage/common/taglibs.jspf"%>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title}</title>
<meta name="decorator" content="single"/>
<script src="/static/common/js/vue.js"></script>


</head>
<body>
<div id="app">
{{name}}
{{user}}
<input v-model="user.name">
<input v-model="data">
<div2 :u="user" :d="data" txt="helloworld" :parent_func="parent_func" arr="[1,2,3]"></div2>
<div2 :u="user" :d="data" txt="helloworld" :parent_func="parent_func" :arr="[1,2,3]"></div2>
<div2 :u="user" :d="data" txt="helloworld" g="1" :arr="[1,2,3]"
:parent_func="parent_func"
></div2>
</div>
<!--
props 中声明的数据与组件data 函数return 的数据主要区别就是props 的来自父级,而data 中
的是组件自己的数据,作用域是组件本身,这两种数据都可以在模板template 及计算属性computed
和方法methods 中使用.
如果你要直接传递数字、布尔值、数组、对象,而且不使用v-bind ,传递的仅
仅是字符串
<div2 :u="user" :d="data" txt="helloworld" :arr="[1,2,3]"></div2>
-->


<template id="div2">
<div>
txt:{{txt}}<br/>
{{counter}}-{{u}}
<div>{{d}}</div>
子组件的d属性: <input v-model="d">
<div>
{{arr.length}}
</div>
<div>
age-{{age}}
</div>
<div>
txt-{{name}}
</div>
<button @click="child_function">
触发父类传递过来的function
</button>
</div>
</template>
<script>

Vue.component("div2",{
template:"#div2",
props:['u','d','txt','arr','g',"parent_func"],
data:function () {
return {
counter:1,
age:this.g,
};
},
computed:{
name:function(){
return this.txt+"_name"
}
},
methods:{
child_function:function(){
console.info("子类开始调用父类方法")
this.parent_func("data here");
}
}
})

var app=new Vue({
el:"#app",
data:{
"name":"gz",
"user":{
"name":"gongz",
age:1,
},
data:1,
// parent_func:function(d){
// console.info("传递到子类的data")
// },

},
methods:{
parent_func:function(d){
console.info("parent function "+ d)
}
}
})
</script>
</body>

</html>