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进行完整的教程。
有关构建和服务命令的更多详细信息,请查看此存储库中的示例,每个都有有关如何构建和运行它们的脚本。
许可证:麻省理工学院