就是正常的引入mqtt.min.js文件,然后在nvue页面调用它。

- 发布:2021-04-28 15:22
- 更新:2021-04-28 15:22
- 阅读:898
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: 10
HBuilderX类型: 正式
HBuilderX版本号: 3.1.11
手机系统: Android
手机系统版本号: Android 10
手机厂商: 华为
手机机型: 华为mate30Pro 5G
页面类型: nvue
打包方式: 云端
项目创建方式: HBuilderX
示例代码:
操作步骤:
import mqtt from '../../utils/mqtt.js';
var client;
onShow: function() {
this.connect();
},
methods: {
// mqtt与服务端连接
connect: function() {
let hosts = 'wx://' + this.serve.host + ':' + this.serve.wsport + this.serve.path;
client = mqtt.connect(
hosts,
this.options
);
let that = this;
client.on('connect', function() {
console.log('连接成功', client)
if (client == null) {
connect();
return
}
client.subscribe(that.onTopic, {
qos: 0
}, function(res) {
if (!res) {
console.log(res, '订阅成功');
that.onMessage();
}
})
}).on('reconnect', function(error) {
console.log('正在重连......')
}).on('error', function(error) {
console.log('连接失败...', error)
}).on('end', function() {
console.log('连接断开')
})
},
}
import mqtt from '../../utils/mqtt.js';
var client;
onShow: function() {
this.connect();
},
methods: {
// mqtt与服务端连接
connect: function() {
let hosts = 'wx://' + this.serve.host + ':' + this.serve.wsport + this.serve.path;
client = mqtt.connect(
hosts,
this.options
);
let that = this;
client.on('connect', function() {
console.log('连接成功', client)
if (client == null) {
connect();
return
}
client.subscribe(that.onTopic, {
qos: 0
}, function(res) {
if (!res) {
console.log(res, '订阅成功');
that.onMessage();
}
})
}).on('reconnect', function(error) {
console.log('正在重连......')
}).on('error', function(error) {
console.log('连接失败...', error)
}).on('end', function() {
console.log('连接断开')
})
},
}
预期结果:
连接成功并且不会报错。
连接成功并且不会报错。
实际结果:
Uncaught TypeError: Illegal invocation 一直报这个错误代码提示。
Uncaught TypeError: Illegal invocation 一直报这个错误代码提示。
bug描述:
nvue页面下mqtt无法使用,总是报错```javascript
