在node-red 中使用vue和plus

在node-red节点编辑框中嵌入vue框架与plus组件库

效果截图

参考步骤

  1. 注册节点demo : index.js   index.html 
  2. 暴露资源 server.js
  3. 引入资源 index.html 
  4. 注册vue和plus   index.html  edit.js
  5. 写你的编辑器    index.html edit.js edit.css
  6. 导出组件  package.json

目录结构

主要代码

package.json

{
  "name": "node-red-demo",
  "version": "1.0.0",
  "description": "node-red vue and plus",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "码代码的小公举",
  "license": "ISC",
  "node-red": {
    "version": ">=1.0.0",
    "nodes": {
      "server": "server.js",
      "index": "index.js"
    }
  }
}

server.js

  • 也可以直接用node暴露links文件夹

module.exports = function (RED) {
    "use strict";
    const fs = require('fs');
    const path = require('path');
    const app = RED.httpNode || RED.httpAdmin;
    app.get("/vue.js", function (_, res) {
        fs.readFile(path.join(__dirname, 'links/vue.js'), (_, js) => {
            res.send(js);
        });
    });
    app.get("/plus.css", function (_, res) {
        res.set('Content-Type', 'text/css');
        fs.readFile(path.join(__dirname, 'links/plus.css'), (_, js) => {
            res.send(js);
        });
    });
    app.get("/plus.js", function (_, res) {
        fs.readFile(path.join(__dirname, 'links/plus.js'), (_, js) => {
            res.send(js);
        });
    });
    app.get("/edit.css", function (_, res) {
        res.set('Content-Type', 'text/css');
        fs.readFile(path.join(__dirname, 'nodes/edit.css'), (_, js) => {
            res.send(js);
        });
    });
    app.get("/edit.js", function (_, res) {
        fs.readFile(path.join(__dirname, 'nodes/edit.js'), (_, js) => {
            res.send(js);
        });
    });
}

index.html

  • 这里提供的是参考逻辑,也可以有其他办法,作为前端,能执行js能获取到dom,我相信可以有非常多的方案~
<!-- 引入vue框架-->
<script src="vue.js"></script>
<!-- 引入vue编辑页面代码-->
<script src="edit.js"></script>
<!-- 引入vue编辑页面样式-->
<link rel="stylesheet" href="edit.css" />
<!-- 引入element 的样式-->
<link rel="stylesheet" href="plus.css" />
<!-- 引入element 的组件库-->
<script src="plus.js"></script>
<!-- node-red注册 -->
<script type="text/javascript">
    var app, nodeData;
    function close(save) {
        app && app.unmount(save); // 卸载掉
    }
    RED.nodes.registerType('demo', {
        category: 'demo',
        defaults: {
            name: { value: "demo" },
            sex: { value: '' },
            age: { value: '' },
        },
        label: function () { return this.name },
        inputs: 1, // 上游:输入 0 或者 1
        outputs: 1, // 输出至下游 0 或者 more
        icon: 'font-awesome/fa-toggle-on', // 标签<i class="fa-sharp fa-solid fa-toggle-on"></i>
        oneditcancel: function () {
            close();
        },
        oneditdelete: function () {
            close();
        },
        oneditsave: function () {
            close();
            console.log(nodeData, 'oneditsave')
            this.age = nodeData.age;
            this.name = nodeData.name;
            this.sex = nodeData.sex;
        },
        oneditprepare: function () {
            nodeData = {
                age: this.age,
                name: this.name,
                sex: this.sex,
            };
            app = mount(nodeData, (data) => {
                nodeData = data;
            })
        },
    });
</script>
<!-- 配置窗口 -->
<script type="text/html" data-template-name="demo">
    <div id="demo-root">
        <div>
            demo for vue and plus
        </div>
        <el-input v-model="name" style="width: 100%; margin-top: 10px;"></el-input>
        <el-select v-model="sex" style="width: 100%; margin-top: 10px;">
            <el-option value="男">男</el-option>
            <el-option value="女">女</el-option>
        </el-select>
        <el-input v-model="age" type="number" style="width: 100%; margin-top: 10px;"></el-input>
    </div>
</script>
<!-- hover提示 -->
<script type="text/html" data-template-name="demo">
    <div class='form-row'>
        demo 提示
    </div>
</script>

index.js

  • node-red组件的应用就不多写了
module.exports = function (RED) {
    function demo(config) {
        RED.nodes.createNode(this, config);
        const node = this;
        Object.keys(config).forEach(i => {
            node[i] = config[i];
        });
        node.on('input', (data) => {
            // 这里数据派发
        })
    }
    RED.nodes.registerType("demo", demo);
}

edit.js

  • 注意需要app.use(组件库)
  • 注意传递修改后的属性值(也有很多其他方式)
function mount(nodeData, saveData) {
    const { createApp } = Vue;
    const app = createApp({
        data() {
            return {
                name: nodeData.name,
                age: nodeData.age,
                sex: nodeData.sex,
            }
        },
        beforeUnmount() {
            saveData({
                name: this.name,
                age: this.age,
                sex: this.sex,
            })
            console.log('beforeUnmount')
        },
        mounted() {
            console.log('mounted');
        },
        methods: {

        },
    });
    app.use(ElementPlus);
    app.mount('#demo-root');
    return app;
}

edit.css

  • 注意node-red样式覆盖, 也可以考虑直接挂载在body下
#demo-root {
    border: 1px solid rgb(0, 119, 255);
    border-radius: 4px;
    padding: 10px;
    width: 280px;
}

/* node-red对input有样式设置,这里清理一下 */
#demo-root input {
    border: 0;
    color: inherit;
    background: inherit; 
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ag-grid-vue3 是一个用于 Vue3 的数据表格组件,而 element-plus 是一个基于 Vue3 的 UI 组件库。虽然这两个组件库可以独立使用,但是它们可以一起使用以增强 UI 功能。 首先,你需要在项目安装 element-plus,可以使用 npm 或 yarn 安装,如下所示: ``` npm install element-plus --save ``` 或 ``` yarn add element-plus ``` 安装完成后,在 Vue3 使用 element-plus 组件,你可以在 main.js 文件使用以下代码: ```javascript import { createApp } from 'vue' import App from './App.vue' import ElementPlus from 'element-plus' import 'element-plus/lib/theme-chalk/index.css' const app = createApp(App) app.use(ElementPlus) app.mount('#app') ``` 这里我们在 main.js 导入了 ElementPlus 组件,并使用 app.use(ElementPlus) 将组件注册到 Vue3 。注意,为了让样式生效,你需要在 main.js 引入 element-plus/lib/theme-chalk/index.css。 接下来,在 ag-grid-vue3 使用 element-plus 组件,你可以在 ag-grid-vue3 的 columnDefs 使用 element-plus 的组件,例如: ```javascript { headerName: '姓名', field: 'name', cellRendererFramework: { template: '<el-button>{{ value }}</el-button>', data() { return { value: '' } }, mounted() { this.value = this.params.value } } } ``` 这里我们在 ag-grid-vue3 的 columnDefs 使用了 element-plus 的 el-button 组件作为单元格渲染器,这样我们就可以在 ag-grid-vue3 使用 element-plus 的组件了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值