vue.js - vuejs props 传function的问题
本文介绍了vue.js - vuejs props 传function的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
vuejs中用组件的props怎么传个function过去呢?
<show-modal :show.sync="showModal" fns='function(){alert(123)}'>
<!--
you can use custom content here to overwrite
default content
-->
<h3 slot="header">custom header</h3>
<div slot='footer'>
<button @click='oKfns' class="modal-default-button">
OK
</button>
<button @click='showModal = false' class="modal-default-button">
CANCEL
</button>
</div>
</show-modal>
我的想法是fns传过去一个方法,然后组件内部使用这个方法
<template lang="html">
<div class="modal-mask" v-show="show" transition="modal">
<div class="modal-wrapper">
<div class="modal-container">
<div class="modal-header">
<slot name="header">
default header
</slot>
</div>
<div class="modal-body">
<slot name="body">
default body
</slot>
</div>
<div class="modal-footer">
<slot name="footer">
default footer
<button class="modal-default-button"
@click="show = false">
OK
</button>
<button class="modal-default-button"
@click="fns">
cancel
</button>
</slot>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data () {
return {}
},
props: {
show: {
type: Boolean,
required: true,
twoWay: true
},
fns : {
type : Function
}
}
}
</script>
现在是会报这个错~
vuejs新手求大神解答,轻喷!
解决方案
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div id="app">
<com1 :func="func"></com1>
</div>
<script src="http://cdn.bootcss.com/vue/1.0.17/vue.js"></script>
<script>
'use strict';
Vue.component('com1', {
template: '<div><com1 :func="func"></com1></div>',
name: 'com1',
props: {
func: Function
},
created() {
this.func()
}
})
new Vue({
el: '#app',
data() {
return {
func: function () {
console.log(this)
}
}
}
})
</script>
</body>
</html>
这篇关于vue.js - vuejs props 传function的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文