javascript - Communication between vue components, error reporting
黄舟
黄舟 2017-05-18 10:51:45
0
4
612

Communication between components, error "ReferenceError: tip is not defined"

    <body>
        <p id="app">
            <button v-on:click="tip = !tip">Toggle</button>
            <my-tip></my-tip>
        </p>
    </body>
    <script type="text/javascript" src="../script/vue.js"></script>
    <script type="text/javascript">
        var tipTemplate = {
          template: '<transition name="fade">\
                        <p v-if="tip" class="vertical-horizontal-center">\
                            <img src="../image/no-log.png">\
                            <h2>暂无记录</h2>\
                            <p class="aui-btn aui-btn-info">重新加载</p>\
                        </p>\
                    </transition>'
        }
        new Vue({
            el: "#app",
            data: {
                tip: false,
            },
            components: {
                'my-tip': tipTemplate,
                props: ['tip'],
            },
            created: function() {}
        })
    </script>
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(4)
我想大声告诉你
var tipTemplate = {
          template: '<transition name="fade">\
                        <p v-if="tip" class="vertical-horizontal-center">\
                            <img src="../image/no-log.png">\
                            <h2>暂无记录</h2>\
                            <p class="aui-btn aui-btn-info">重新加载</p>\
                        </p>\
                    </transition>',
           props: ['tip']
        }
<my-tip tip="tip"></my-tip>

It is recommended to read the document again and see the scope of the component

过去多啦不再A梦

props: ['tip'] should be written in var tipTemplate={} instead of new Vue({}). The scope of the component is wrong

我想大声告诉你

There is a problem with the answer above. Components need dynamic attribute binding:

<my-tip :tip="tip"></my-tip>

The whole is as follows:

    <body>
        <p id="app">
            <button v-on:click="tip = !tip">Toggle</button>
            <my-tip :tip="tip"></my-tip>
        </p>
    </body>
    <script type="text/javascript" src="../script/vue.js"></script>
    <script type="text/javascript">
        Vue.component('simple-counter', {
          template: '<transition name="fade">\
                        <p v-if="tip" class="vertical-horizontal-center">\
                            <img src="../image/no-log.png">\
                            <h2>暂无记录</h2>\
                            <p class="aui-btn aui-btn-info">重新加载</p>\
                        </p>\
                    </transition>',
          props: ['tip']
        })
        
        new Vue({
          el: '#app',
          data: {
              tip: false
          },
          created: function() {}
        })
    </script>
Peter_Zhu

Add a v-if="tip" to the child component

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template