I am building a simple web app with Rust and I am trying to display an image to the website. But I can't add that image.
I am using Rust with a framework called Yew and with a tool Trunk.
I have successfully linked the .scss file to my website with Trunk. As they described in their documentation.
index.html
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Yew Development</title>
<link data-trunk="" rel="scss" href="main.scss"> <!-- Successfull linked to this file -->
</head>
<body></body>
</html>
The .scss file does its work successfully.
But how can I display an image with Rust file with the html!
macro?
main.rs
use yew::prelude::*;
fn main() {
yew::start_app::<App>();
}
#[function_component(App)]
fn app() -> Html {
html! {
<>
<h1> {"Hello world!"}</h1>
<link data-trunk="true" rel="copy-file" href="img/rust.png"/> // doesn't work
<img data-trunk="true" src="img/rust.png" alt="rust image"/> // doesn't work
<img src="img/rust.png" alt="rust image"/> // doesn't work
</>
}
}
Trunk's docs about how to add image.
But the doc wasn't helpful for me.
What you must do is instruct trunk to copy your static assets into the
dist
directory.Let's suppose you have a directory called
img
with all the images, next to yoursrc
directory at the root of your project:Add this line to your
index.html
header:This line won't be present in the production
index.html
file, it's only an instruction for trunk.Now the files of the
img
directory will be served by trunk (and copied into thedist
directory for production) so you can have this link in yourhtml!
macro:data-trunk
attributes here would make no sense: trunk only reads and parses theindex.html
file, it doesn't see what's generated client side by the wasm code produced by thehtml!
macro.Your dist directory will look like this: