使用vite+vue3搭建项目导入roslibjs后报错
After using vite+vue3 to build the project and importing roslibjs, an error is reported
我用的是vue3,用vite创建项目时,导入roslibjs库,使用其中一个函数报错,如下图。但是当我尝试使用 vuecli 创建项目时,导入相同的库后,一切都很好。
// vite created 'vite-app' project
npm init vite@latest
cd vite-app
npm instal --save roslib
// vuecli created 'vuecli-app' project
vue create vuecli-app
cd vuecli-app
npm install --save roslib
<script setup>
import ROSLIB from "roslib";
let ws_address = '127.0.0.1:9090'
let connected = false;
const ros = new ROSLIB.Ros({
url: ws_address,
})
const connect = () => {
ros.on("connection", () => {
connected = true;
console.log("Connected!");
});
ros.on("error", (error) => {
console.log("Error connecting to websocket server: ", error);
});
ros.on("close", () => {
connected = false;
console.log("Connection to websocket server closed.");
});
}
const disconnect = () => {
ros.close();
}
</script>
<template>
<div>
<p class="text-success" v-if="connected">Connected!</p>
<p class="text-danger" v-else>Not connected!</p>
<input type="text" v-model="ws_address" />
<br />
<button @click="disconnect" class="btn btn-danger" v-if="connected">Disconnect!</button>
<button @click="connect" class="btn btn-success" v-else>Connect!</button>
</div>
</template>
我不确定 Vue CLI + Webpack 对 roslib
在浏览器中的工作有何不同。
但是,在浏览器中,您应该根据 docs:
使用 roslib
的预构建版本(其中转译了特定于节点的构造)
If you use roslib in a browser, all the classes will be exported to a global variable called ROSLIB. If you use nodejs, this is the variable you get when you require('roslib')
导入 roslib/build/roslib.js
脚本,设置 window.ROSLIB
:
<script setup>
import 'roslib/build/roslib';
const ros = new window.ROSLIB.Ros({
url: ws_address,
});
⋮
</script>
我用的是vue3,用vite创建项目时,导入roslibjs库,使用其中一个函数报错,如下图。但是当我尝试使用 vuecli 创建项目时,导入相同的库后,一切都很好。
// vite created 'vite-app' project
npm init vite@latest
cd vite-app
npm instal --save roslib
// vuecli created 'vuecli-app' project
vue create vuecli-app
cd vuecli-app
npm install --save roslib
<script setup>
import ROSLIB from "roslib";
let ws_address = '127.0.0.1:9090'
let connected = false;
const ros = new ROSLIB.Ros({
url: ws_address,
})
const connect = () => {
ros.on("connection", () => {
connected = true;
console.log("Connected!");
});
ros.on("error", (error) => {
console.log("Error connecting to websocket server: ", error);
});
ros.on("close", () => {
connected = false;
console.log("Connection to websocket server closed.");
});
}
const disconnect = () => {
ros.close();
}
</script>
<template>
<div>
<p class="text-success" v-if="connected">Connected!</p>
<p class="text-danger" v-else>Not connected!</p>
<input type="text" v-model="ws_address" />
<br />
<button @click="disconnect" class="btn btn-danger" v-if="connected">Disconnect!</button>
<button @click="connect" class="btn btn-success" v-else>Connect!</button>
</div>
</template>
我不确定 Vue CLI + Webpack 对 roslib
在浏览器中的工作有何不同。
但是,在浏览器中,您应该根据 docs:
使用roslib
的预构建版本(其中转译了特定于节点的构造)
If you use roslib in a browser, all the classes will be exported to a global variable called ROSLIB. If you use nodejs, this is the variable you get when you require('roslib')
导入 roslib/build/roslib.js
脚本,设置 window.ROSLIB
:
<script setup>
import 'roslib/build/roslib';
const ros = new window.ROSLIB.Ros({
url: ws_address,
});
⋮
</script>