javascript - [Vue警告] : Failed to resolve directive: ref

标签 javascript vue.js

我正在学习 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 抛出异常

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

谁能告诉我为什么?谢谢!

最佳答案

您正在使用 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

关于javascript - [Vue警告] : Failed to resolve directive: ref,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41627728/

相关文章:

javascript - Vue.js eslint 解析错误。 : unexpected token

javascript - Vue.js 方法声明的区别

javascript - Vue-GTM错误: You may need an appropriate loader to handle this file type

javascript - Ember 和 d3v4 导入错误

javascript - 使用 php 的倒计时器来激活按钮点击

JavaScript 模块模式与示例

vue.js - 如何在 Vue Nuxt 项目中导入整个 SCSS 文件夹?

javascript - 将数据传递给单个文件 Vue 组件

javascript - 错误: No package 'cairo' found on ec2 bitnami MEAN server

javascript - Firebase 存储规则经过身份验证的用户的专用 URL(不含 downloadURL 或库)