我有這個在我的客戶端/模板/ main.html中:訪問外部腳本Template.events
<head>
<title>app boil</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<script src="https://cdnjs.cloudflare.com/ajax/libs/quickblox/2.4.0/quickblox.min.js"></script>
</head>
因此,我調用API quickblox。這提供了一個QB對象。
我現在的客戶端/模板/ quickblox/quickbloxcall.js其中有這樣的代碼:
import { Template } from 'meteor/templating';
import './quickbloxcall.html'
Template.quickbloxcall.onRendered(function() {
console.log(QB.createSession);
});
Template.quickbloxcall.events({
'submit .quickblox-form'(event) {
var user = {
id: 4448514,
name: 'chatuserweb1',
login: 'chatuserweb1',
pass: 'chatuserweb1'
};
QB.createSession({login: user.login, password: user.pass}, function(err, res) {
if (res) {
QB.chat.connect({userId: user.id, password: user.pass}, function(err, roster) {
if (err) {
console.log(err);
} else {
/*
* (Object) roster - The user contact list
* roster = {
* '1126541': {subscription: 'both', ask: null}, // you and user with ID 1126541 subscribed to each other.
* '1126542': {subscription: 'none', ask: null}, // you don't have subscription but user maybe has
* '1126543': {subscription: 'none', ask: 'subscribe'}, // you haven't had subscription earlier but now you asked for it
* };
*/
}
});
}else{
console.log(err);
}
});
},
});
在上面的代碼中,當我提交表單,我得到這個錯誤控制檯:
Uncaught TypeError: Cannot read property 'createSession' of undefined(…)
所以這意味着QB對象在Template.quickblox.events提交事件處理程序中無法訪問。
然而,在執行console.log()我得到這樣的:
function (params, callback) {
this.auth.createSession(params, callback);
}
所以這意味着Template.quickbloxcall.onRendered正確加載QB對象。
如何在Template.quickblox.events中訪問此外部腳本?
你試過[$ .getScript](https://api.jquery.com/jquery.getscript/)嗎? –
爲什麼不使用QuickBlox的NPM版本?也許它解決了你的問題 – Khang