I have a Yew component whose html output is completely defined by the data contained in a local file on the server disk.
I can’t read this local file (is the server sandboxed?)
Is there a way to read a file from a Yew component?
struct MyComponent {
config: Option<String>,
}
enum Msg {
LoadConfig,
ConfigLoaded(String),
}
impl Component for MyComponent {
type Message = Msg;
type Properties = ();
fn create(ctx: &Context<Self>) -> Self {
let config = None;
ctx.link().send_message(Msg::LoadConfig);
Self { config }
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::LoadConfig => {
let link = ctx.link().clone();
spawn_local(async move {
let response = Request::get("/static/config.json")
.send()
.await
.unwrap()
.text()
.await
.unwrap();
link.send_message(Msg::ConfigLoaded(response));
});
false
}
Msg::ConfigLoaded(config) => {
self.config = Some(config);
true
}
}
}
fn view(&self, _: &Context<Self>) -> Html {
match &self.config {
Some(config) => html! { <div>{ config }</div> },
None => html! { <div>{ "Loading..." }</div> },
}
}
}
The above code fails. It shows me the contents of the index.html file. I tried different urls, but nothing worked. I need some help, please
5