Vue.js 中函数传参
如何在 Vue.js 中向函数传递参数
在 Vue.js 中,有两种主要方法可以向函数传递参数:
- 使用插槽 (slot):插槽允许你在组件的 HTML 模板中传递数据。在组件内,你可以访问插槽传递的数据,并将其作为函数的参数使用。
- 使用 bind 绑定:bind 允许你在 Vue.js 实例中绑定函数。在绑定时,你可以提供函数的参数。
使用插槽传递参数
<!-- 父组件 --> <template><my-component><!-- 向 my-component 组件传递参数 --><param>{{ message }} </my-component></template><!-- 子组件 --><template><div> <!-- 接收父组件传递的参数 --> <p>{{ slot }}</p> </div> </template><script> export default { props: [\'slot\'] } </script>