簡體   English   中英

[Vue 警告]:無法解析指令:ref

[英][Vue warn]: Failed to resolve directive: ref

我正在學習 Vue.js 中的組件。

模板是:

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

JS是:

    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 拋出

警告:vue.js:525 [Vue 警告]:無法解析指令:ref(在組件中找到)

誰能告訴我為什么? 謝謝!

您正在使用 Vue 2.0 和 Vue 1.0 語法:

請參閱遷移文檔:

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

所以,你應該使用:

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

或者將 Vue 版本改回 1.x

除非您在組件模板中使用 Pug(以前稱為 Jade),否則您需要使用 HTML:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM