zoukankan      html  css  js  c++  java
  • Vue中使用Vue.component定义两个全局组件,用单标签应用组件时,只显示一个组件的问题和 $emit的使用。

    解决方法:  

    定义了两个 Vue.component 在 el 中使用的时候要用 双标签, 用单表标签的时候,只会显示第个 组件间
    这样写只显示 welcome-button 组件
    <welcome-button @welcome="sayHi"/>
    <magic-eight-ball @give-advice="showAdvice"/>
    --------------------------------
    改成双标签后,就会显示两个组件了。
    <welcome-button @welcome="sayHi"></welcome-button>
    <magic-eight-ball @give-advice="showAdvice"></magic-eight-ball>

    完整代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>$emit</title>
        <script src="node_modules/vue/dist/vue.min.js"></script>
    </head>
    <body>
        <div id="app">
            <welcome-button @welcome="sayHi"></welcome-button>
            <magic-eight-ball @give-advice="showAdvice"></magic-eight-ball>
        </div>
        <script>
            /*
            注:
                定义了两个 Vue.component 在 el 中使用的时候要用  双标签, 用代表标签的换,只会显示第个 组件间
                这样写只显示 welcome-button 组件
                 <welcome-button @welcome="sayHi"/>
                 <magic-eight-ball @give-advice="showAdvice"/>
                 --------------------------------
                 改成双标签后,就会显示两个组件了。
                 <welcome-button @welcome="sayHi"></welcome-button>
                 <magic-eight-ball @give-advice="showAdvice"></magic-eight-ball>
    
            */
    
    
            /*---------------无参数---------------------*/
            Vue.component('welcome-button', {
                template: `<button v-on:click="$emit('welcome')">
                             点我
                           </button>`
            });
    
            /*-----------------有参数---------------*/
            Vue.component('magic-eight-ball', {
                data: function () {
                    return {
                        possibleAdvice: ['Yes', 'No', 'Maybe']
                    }
                },
                methods: {
                    giveAdvice: function () {
                        var randomAdviceIndex = Math.floor(Math.random() * this.possibleAdvice.length);
                        // console.log( this.possibleAdvice[randomAdviceIndex]);
                        this.$emit('give-advice', this.possibleAdvice[randomAdviceIndex])
                    }
                },
                template: ` <button v-on:click="giveAdvice">
                               点我出发父组件的方法,并传参
                            </button>`
            })
    
            new Vue({
                el: '#app',
                methods: {
                    sayHi(){
                      alert('Hi!');
                    },
                    showAdvice(advice){
                        alert(advice)
                     }
                },
            });
    
    
        </script>
    </body>
    </html>

    使用单标签引用组件时,效果图:

     

    使用双标签引用组件时,效果图:

  • 相关阅读:
    android的窗口创建过程
    android的Binder
    Android Intent.FLAG_NEW_TASK详解,包括其他的标记的一些解释
    android的事件分发测试结果
    Don't Store Data in the Application Object
    关于算法
    自定义控件其实很简单3/4
    自定义控件其实很简单2/3
    建设一个能承受500万PV/每天的网站
    strust2里面package的元素排列顺序
  • 原文地址:https://www.cnblogs.com/taohuaya/p/10233033.html
Copyright © 2011-2022 走看看