javascript - 如何组合两个组件 Vue.js

标签 javascript html vue.js vuejs2 vue-component

我有一个组件是一个按钮,一个是 html。我需要从具有 v-if 条件的 html 组件中的按钮获取响应。如果按钮单击 true 附加 html。

 <template>

    <button v-on:click="greet">Greet</button>

</template>

    <script>
        export default {
    
            data () {
                return {
                    toggle: false,
                }
            },
    
            mounted() {
                console.log('Add more componente mounted.')
            }
        }
    </script>

HTML 组件:

<template>

        <div v-show='toggle' class="bg-white">
            <div class="row">
                <div class="col-md-5">
                    <h1>Title</h1>
                    ....
                    .....
                </div>
            </div>
        </div>

</template>

    <script>
        export default {
    
            data () {
                return {
                    toggle: true
                }
            },
    
            mounted() {
                console.log('Add more componente mounted.')
            }
        }
    </script>

如何获取/传递和使用html组件中的按钮响应,toggle: false,

最佳答案

您可以在父组件和子组件之间交换数据,使用 props 将数据从父组件发送到子组件,使用 this.$emit 事件将数据从子组件发送到父组件.

子按钮组件

<template>
        <button v-on:click="greet">Greet</button>
 </template>
 <script>
            export default {
                props:[toggle:{type:boolean,default:false}]
                data () {
                    return {}        
                },
                methods:{
                greet(){
                this.$emit("btn-click");
                }
                }
                ,
                mounted() {
                    console.log('Add more componente mounted.')
                }
            }
</script>

和父html组件

<template>

        <div v-show='toggle' class="bg-white">
            <div class="row">
                <div class="col-md-5">
                    <h1>Title</h1>
                  <your-btn :toggle="toggle" v-on:btn-click="greetFromBtn"/>
                </div>
            </div>
        </div>

</template>

    <script>
    import yourBtn from 'pathToYourBtnComponent/yourBtn'
        export default {

            data () {
                return {
                    toggle: true
                }
            },
            components:{
            yourBtn
            },
            methods:{
            greetFromBtn(){
            
            }
            },

            mounted() {
                console.log('Add more componente mounted.')
            }
        }
    </script>

关于javascript - 如何组合两个组件 Vue.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52165035/

相关文章:

javascript - D3 值键的匹配方式是否与使用 enter() 时默认索引号的匹配方式相同?

javascript - 单击时使图像显示在另一个图像下方

jquery - Colorbox IE7 高度问题

javascript - Vue.js 使用 import { } 时找不到导出错误

javascript - 如何在 VS Code 编辑器中使用 ESLint + Airbnb 规则 + TypeScript + Stylelint 为 SCSS 配置 Vue CLI 4,并在保存时自动修复?

javascript - 能够在循环中使用 ( v-if ) 切换 True/False

javascript - 模块化 AngularJS 应用程序 commonJS 与 AMD 模块语法

javascript - jQuery : fire callback only once

javascript - 让 Node.js 与 tree-model-js 一起工作

html - 如何在底部对齐 flexbox 内的 div?