简体   繁体   中英

[Vue warn]: Failed to resolve directive: ref

I'm learning about Components in Vue.js.

The template is:

<script type="text/template" id="child1">
    <h3>This is the child1~!</h3>
</script>
<script type="text/template" id="child2">
    <h3>This is the child2~!</h3>
</script>
<script id="parent" type="text/template">
    <div>
        <h2>This is the parent~!</h2>
        <child1 v-ref:cc1></child1>
        <child2 v-ref:cc2></child2>
        <button v-on:click="getChildren">get children msg</button>
    </div>
</script>

and the JS is:

    Vue.component('parent', {
        template: '#parent',
        methods: {
            getChildren:function() {
                alert(this.$refs.cc1);
                alert(this.$refs.cc2);
            }
        },
        components: {
            'child1': {
                template: '#child1',
                data: function() {
                    return {
                        msg: 'This is the child1 ~!'
                    }
                }
            },
            'child2': {
                template: '#child2',
                data: function() {
                    return {
                        msg: 'This is the child2 ~!'
                    }
                }
            }
        }
    });

Vue throws

warn:vue.js:525 [Vue warn]: Failed to resolve directive: ref (found in component )

Who can tell me why? Thanks!

You are using Vue 2.0 with Vue 1.0 gramma:

see the migration documentation:

https://v2.vuejs.org/v2/guide/migration.html#v-el-and-v-ref-replaced

So, you should use:

<child1 ref="cc1"></child1>
<child2 ref="cc2"></child2>

Or change the Vue version back to 1.x

除非您在组件模板中使用 Pug(以前称为 Jade),否则您需要使用 HTML:

template: '<div id="parent"></div>',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM