• 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>

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

     

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

  • 相关阅读:
    9、par画图参数
    awk
    Android Studio Gradle下载的包在哪里?
    .net core webapi 在原有基础上修改。
    .net core webapi 部署到 IIS
    原来部署好的WCF(可以调用),因为部署.net core,而安装了DotNetCore.2.0.5-WindowsHosting,导致现在WCF站点不可以。
    sql2008R2新建链接服务器。
    Dynamics CRM 365常用js记录。
    <div>标签输入文字
    dynamics crm 365 附件上传图片并且显示。
  • 原文地址:https://www.cnblogs.com/taohuaya/p/10233033.html
Copyright © 2020-2023  润新知