sauron
0.40.0
Sauron是一個多功能的Web框架和庫,用於構建客戶端和/或服務器端Web應用程序,重點是人體工程學,簡單性和優雅。這使您可以編寫最少的代碼,並更多地關注業務邏輯,而不是框架的內部細節。
Sauron受到Elm-Lang的啟發,並遵循Elm Architecture。
在Sauron應用程序中,只有模型,視圖和更新。該模型是您的應用狀態。該視圖描述瞭如何向用戶展示模型。更新功能描述瞭如何更新模型,它使用的消息包含更新模型所需的數據。
在您的src/lib.rs中
use sauron :: {
html :: text , html :: units :: px , jss , node , wasm_bindgen , Application , Cmd , Node , Program ,
} ;
enum Msg {
Increment ,
Decrement ,
Reset ,
}
struct App {
count : i32 ,
}
impl App {
fn new ( ) -> Self {
App { count : 0 }
}
}
impl Application for App {
type MSG = Msg ;
fn view ( & self ) -> Node < Msg > {
node ! {
<main>
<input type = "button"
value= "+"
on_click=|_| {
Msg :: Increment
}
/>
<button class= "count" on_click=|_| { Msg :: Reset } > { text ( self . count ) } </button>
<input type = "button"
value= "-"
on_click=|_| {
Msg :: Decrement
}
/>
</main>
}
}
fn update ( & mut self , msg : Msg ) -> Cmd < Msg > {
match msg {
Msg :: Increment => self . count += 1 ,
Msg :: Decrement => self . count -= 1 ,
Msg :: Reset => self . count = 0 ,
}
Cmd :: none ( )
}
fn stylesheet ( ) -> Vec < String > {
vec ! [ jss! {
"body" : {
font_family : "verdana, arial, monospace" ,
} ,
"main" : {
width : px ( 30 ) ,
height : px ( 100 ) ,
margin : "auto" ,
text_align : "center" ,
} ,
"input, .count" : {
font_size : px ( 40 ) ,
padding : px ( 30 ) ,
margin : px ( 30 ) ,
}
} ]
}
}
# [ wasm_bindgen ( start ) ]
pub fn start ( ) {
Program :: mount_to_body ( App :: new ( ) ) ;
} index.html
<!doctype html >
< html >
< head >
< meta content =" text/html;charset=utf-8 " http-equiv =" Content-Type " />
< title > Counter </ title >
< script type = module >
import init from './pkg/counter.js' ;
await init ( ) . catch ( console . error ) ;
</ script >
</ head >
< body >
</ body >
</ html >在Cargo.toml中,指定板條箱類型為cdylib
[ package ]
name = " counter "
version = " 0.1.0 "
edition = " 2021 "
[ lib ]
crate-type = [ " cdylib " ]
[ dependencies ]
sauron = " 0.61.0 " cargo install wasm-pack
cargo install basic-http-server使用
wasm-pack build --target web --release使用
basic-http-server -a 0.0.0.0:4000然後導航到http:// localhost:4000
前往getting-started.md進行完整的教程。
有關構建和服務命令的更多詳細信息,請查看此存儲庫中的示例,每個都有有關如何構建和運行它們的腳本。
許可證:麻省理工學院