【问题标题】:TypeError: this.$refs.datasource.fetch is not a functionTypeError: this.$refs.datasource.fetch 不是函数
【发布时间】:2018-12-18 04:27:38
【问题描述】:

我正在尝试在使用a vue wrapper 创建的数据源上调用fetch 方法。

错误是 [Vue warn]: Error in mounted hook: "TypeError: this.$refs.datasource.fetch is not a function" 所以我的问题是如何从$refs 对象。

我做了一个stackblitz here

<body>
    <div id="vueapp" class="vue-app">
        <kendo-datasource ref="datasource" :data="dataSourceArray">
        </kendo-datasource>
    </div>
</body>

new Vue({
    el: '#vueapp',
    data: function() {
        return {
            dataSourceArray: [
                { text: 'Albania', value: '1' },
                { text: 'Andorra', value: '2' },
                { text: 'Armenia', value: '3' },
                { text: 'Austria', value: '4' },
                { text: 'Azerbaijan', value: '5' },
                { text: 'Belarus', value: '6' },
                { text: 'Belgium', value: '7' }
            ]
        }
    },
    mounted: function () {
      this.$refs.datasource.fetch()//<== error here
    }
})

【问题讨论】:

    标签: javascript vue.js kendo-ui kendo-ui-vue


    【解决方案1】:

    你得到的 ref 是整个 &lt;kendo-datasource&gt; 组件,它没有直接的 fetch() 方法。我相信您要做的是在组件中获取 kendoDataSource 并在其上调用 fetch 方法,这将是:

    this.$refs.remoteDatasource.kendoDataSource.fetch();
    

    修改您的示例以查看它的实际效果:

        <kendo-datasource 
            ref="datasource" 
            :data="dataSourceArray" 
            :type="'odata'"
            :transport-read-url="'https://demos.telerik.com/kendo-ui/service/Northwind.svc/Customers'">
        </kendo-datasource>
    

    this.$refs.remoteDatasource.kendoDataSource.fetch(function(){
        var data = this.data();
        console.log(data[0]);
        });
    }
    

    【讨论】:

    • 谢谢,效果很好。当我在谷歌上搜索时,我还找到了另一种方法来获取 kendoDataSource 对象,方法是调用 this.$refs.datasource.kendoWidget()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 2018-01-19
    • 2012-04-10
    相关资源
    最近更新 更多