如何在超堆栈中使用 HTTP

  • 本文关键字:HTTP 堆栈 hyperstack
  • 更新时间 :
  • 英文 :


我已经使用hyperstack.org的安装说明对hyperstack rails应用程序进行了基本安装,尝试在after_mount回调中添加HTTP.get请求。

不太确定我还能尝试什么,认为HTTP将是一个标准选项

class App < HyperComponent
include Hyperstack::Router
after_mount do
HTTP.get('/example.json')
end
render do
DIV() do
'App'
# NodeDisplay
# define routes using the Route psuedo component.  Examples:
# Route('/foo', mounts: Foo)                : match the path beginning with /foo and mount component Foo here
# Route('/foo') { Foo(...) }                : display the contents of the block
# Route('/', exact: true, mounts: Home)     : match the exact path / and mount the Home component
# Route('/user/:id/name', mounts: UserName) : path segments beginning with a colon will be captured in the match param
# see the hyper-router gem documentation for more details
end
end
end

收到的错误是:

Uncaught error: HTTP: uninitialized constant App::HTTP
in App (created by Hyperstack::Internal::Component::TopLevelRailsComponent)
in Hyperstack::Internal::Component::TopLevelRailsComponent

简单的答案:默认情况下,HTTP 库不包含在 Opal 或 Hyperstack 中。

您可以将其作为 Opal jQuery 包装器的一部分,或者与最小的 OpalBrowser::HTTP库一起包含。

要将 jQuery 包装器添加到 Hyperstack 应用程序,请执行以下操作:

  1. 通过向config/initializers/hyperstack.rb文件添加
    import 'hyperstack/component/jquery', client_only: true
    来导入 Hypestack jquery 包装器。

  2. 然后在你的资产中包含实际的jquery javascript代码:

    如果使用webpacker,请在终端中运行yarn add jquery,然后将这一行添加到javascripts/packs/client_only.js文件中:jQuery = require('jquery');如果不使用webpacker,请将import 'jquery', client_only: true添加到超堆栈初始值设定项文件中。

如果您只想使用更小的Browser::HTTP模块,请添加

import 'browser/http

到您的config/initializers/hyperstack.rb文件。

更改 hyperstack.rb 后,您必须通过运行rm -rf tmp/cache来清除 rails tmp 缓存

注意:使用浏览器版本时,您需要使用Browser::HTTP而不是简单地HTTP

最新更新