我已经使用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 应用程序,请执行以下操作:
通过向
config/initializers/hyperstack.rb
文件添加import 'hyperstack/component/jquery', client_only: true
来导入 Hypestack jquery 包装器。然后在你的资产中包含实际的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
。