与Flask一起使用React时同时包含数据

时间:2018-12-29 03:28:35

标签: reactjs flask architecture ssr

现在,我有一个Flask服务器,它既有API端点又提供文件。在提供文件时,它基本上会提供一个html模板,该模板包括一个根据路径而不同的脚本(React,由webpack捆绑在一起),例如:

Pattern "^\$(\d+\,\d+)\s\-\s\$(\d+\,\d+)"

在我的React中,主要组件具有componentDidMount()方法,该方法使用fetch()调用同一Flask服务器的API端点,如下所示:

Option Explicit
Private Sub Example()
    Dim RegExp As New RegExp
    Dim Pattern As String
    Dim CelValue As String
    Dim rng As Range
    Dim Cel As Range

    Set rng = ActiveWorkbook.Sheets("Sheet1" _
                            ).Range("A2", Range("A9999" _
                            ).End(xlUp))

    For Each Cel In rng
        DoEvents
        Pattern = "^\$(\d+\,\d+)\s\-\s\$(\d+\,\d+)"

        If Pattern <> "" Then
            With RegExp
                .Global = True
                .MultiLine = True
                .IgnoreCase = False
                .Pattern = Pattern
            End With

            If RegExp.Test(Cel.Value) Then
'                Debug.Print Cel.Value

                Debug.Print RegExp.Replace(CStr(Cel), "$1")
                Debug.Print RegExp.Replace(CStr(Cel), "$2")

            End If
        End If
    Next
End Sub

基本上,它正在调用的API端点会将数据发送回服务文件:

@bp.route('/getData/', methods=('GET',))
def getData():
    return render_template('base.html', scriptName="getData.js")

@bp.route('/postData/', methods=('GET',))
def postData():
    return render_template('base.html', scriptName="postData.js")

有没有一种方法可以有效地做到这一点,我什至可以做到这一点?例如,有没有一种方法可以通过html / React文件发送数据?现在,我提供html / React,然后再次调用API,这似乎是不必要的步骤。我知道,可以使用EJS或其他类似的模板引擎,将数据包含在文件中。有没有办法用React做到这一点?

0 个答案:

没有答案