gpt4 book ai didi

javascript - 在vuejs组件中调用外部函数

转载 作者:行者123 更新时间:2023-11-30 20:36:15 26 4
gpt4 key购买 nike

我想使用 jsgrid.min.js 在 vue 组件中创建表。我应该在 jsgrid.min.jsfunction 中调用它

jsGrid(..)

我该怎么做?(在组件中调用放置在 vue 组件外部的函数)

组件.vue

<template>
<div id="jsGrid">

</div>
</template>

<script>
export default {
data: () => ({
name: "mja"
}),


mounted () {

$("#jsGrid").jsGrid({
width: "100%",
height: "500px"

});

}
}
</script>

<style scoped>

</style>

错误

[Vue warn]: Error in mounted hook: "TypeError: $(...).jsGrid is not a function"

我其实是嵌入了src文件

<script type="text/javascript" src="{{ asset('plugins/jsgrid/dist/jsgrid.min.js')}}"></script>

最佳答案

您需要使用 Jquery 将 Jquery 包含为 jsGrid,这是工作演示 :)

var app = new Vue({
el: '#app',
mounted:function(){
jQuery("#jsGrid").jsGrid({
width: "100%",
height: "400px",
inserting: true,
editing: true,
sorting: true,
paging: true,
data: this.clients,
fields: [
{ name: "Name", type: "text", width: 150, validate: "required" },
{ name: "Age", type: "number", width: 50 },
{ name: "Address", type: "text", width: 200 },
{ name: "Country", type: "select", items: this.countries, valueField: "Id", textField: "Name" },
{ name: "Married", type: "checkbox", title: "Is Married", sorting: false },
{ type: "control" }
]
});
},
data: {
message: 'Hello Vue App!',
clients : [
{ "Name": "Otto Clay", "Age": 25, "Country": 1, "Address": "Ap #897-1459 Quam Avenue", "Married": false },
{ "Name": "Connor Johnston", "Age": 45, "Country": 2, "Address": "Ap #370-4647 Dis Av.", "Married": true },
{ "Name": "Lacey Hess", "Age": 29, "Country": 3, "Address": "Ap #365-8835 Integer St.", "Married": false },
{ "Name": "Timothy Henson", "Age": 56, "Country": 1, "Address": "911-5143 Luctus Ave", "Married": true },
{ "Name": "Ramona Benton", "Age": 32, "Country": 3, "Address": "Ap #614-689 Vehicula Street", "Married": false }
],
countries :[
{ Name: "", Id: 0 },
{ Name: "United States", Id: 1 },
{ Name: "Canada", Id: 2 },
{ Name: "United Kingdom", Id: 3 }
],

}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css" />
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css" />

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
{{ message }}
<div id="jsGrid"></div>
</div>

关于javascript - 在vuejs组件中调用外部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49813355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com