简体   繁体   English

将羊驼导入 React.js

[英]Importing alpaca into React.js

I am trying to use an Alpaca form within a React app:我正在尝试在 React 应用程序中使用 Alpaca 表单:

import React, { useEffect, useRef } from "react";
import $ from "jquery";
import * as popper from "popper.js";
import * as bootstrap from "bootstrap";
import * as handlebars from "handlebars";
import alpaca from "alpaca";

var jp = require('jsonpath');

export default function Form() {
    useEffect(() => {
        $("#form").alpaca({
        });
    }, []);

    return <>
        <h2>Alpaca Form</h2>
        <div id="form"/>
    </>;
}

webpack compiles this but in the browser I see this message: webpack 编译了这个,但在浏览器中我看到了这个消息:

jquery__WEBPACK_IMPORTED_MODULE_1___default(...)(...).alpaca is not a function

While running "npm start", the browser error shows: ReferenceError: jQuery is not defined.运行“npm start”时,浏览器错误显示:ReferenceError: jQuery is not defined。

Alpaca requires handlebars.js so you need to import it before importing alpaca.js. Alpaca 需要 handlebars.js,因此您需要在导入 alpaca.js 之前先导入它。 Can you try it and tell if this works for you?你能试一试并判断这是否适合你吗?

Looks like I have found a solution that works in a Spring Boor application.看起来我找到了一个适用于 Spring Boor 应用程序的解决方案。 First, I create a static js script with only one function:首先,我创建了一个只有一个函数的静态 js 脚本:

function alpacaForm(tag, config) {
    $(tag).alpaca(config);
}

Then I include this script in index.html, following all Alpaca dependencies:然后我将这个脚本包含在 index.html 中,遵循所有 Alpaca 依赖项:

    <!-- dependencies (jquery, handlebars and bootstrap) -->
    <script type="text/javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>
    <link type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
    <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>

    <!-- alpaca -->
    <link type="text/css" href="//cdn.jsdelivr.net/npm/alpaca@1.5.27/dist/alpaca/bootstrap/alpaca.min.css" rel="stylesheet"/>
    <script type="text/javascript" src="//cdn.jsdelivr.net/npm/alpaca@1.5.27/dist/alpaca/bootstrap/alpaca.min.js"></script>
    <script type="text/javascript" src="./js/alpacaForm.js"></script>

Then I just call alpacaForm in my React app:然后我只是在我的 React 应用程序中调用 alpacaForm:

export default function Form() {
    useEffect(() => {
        alpacaForm("#form", {
               "schema": {
                   ...
               },
               "options": {
                   ...
               }
           }
        );
    }, []);

    return <>
        <h2>Alpaca Form</h2>
        <div id="form"/>
    </>;
}

See the source code here: https://gitlab.com/AlbertGevorgyan/bootreact在此处查看源代码: https : //gitlab.com/AlbertGevorgyan/bootreact

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM