如何解决使用Yew Framwork进行Webassembly的未发现的错误
我正在使用Yew编程主题切换器,方法是单击不同主题的循环。 这是我的更新功能。它会获取存储在共享状态中的当前主题,具体取决于在theme_cycle中下一步将在共享状态内设置主题值。
Role_Based :
services.AddAuthorization(options =>
{
options.AddPolicy("Over18",policy =>
{
policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
policy.Requirements.Add(new MinimumAgeRequirement());
});
});
JWT :
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Audience = "https://localhost:5000/";
options.Authority = "https://localhost:5000/identity/";
});
但是,如果共享状态内的主题val为“生锈”,并且我再次单击调用Msg :: ChangeTheme的按钮,则该主题应设置为“ light”,但是我的代码出现了恐慌,并且出现了“未捕获的错误:未定义”。
解决方法
我找到了解决方法;而不是使用数组和访问值,我尝试执行相同的任务,但仅使用迭代器,并确保update函数在函数本身之外没有任何变量的所有权(我真的不知道这是否是虽然确实有必要...)
fn update(&mut self,msg: Self::Message) -> ShouldRender {
match msg {
Msg::ChangeTheme => {
let theme_cycle = ["light".to_string(),"dark".to_string(),"rust".to_string()];
let current_theme = self.props.handle.state().theme.clone();
let indexof_current_theme = match theme_cycle.iter().position(|x| x.to_string() == current_theme) {
None => 0,Some(x) => x.clone(),};
let next_theme = match theme_cycle.iter().nth(indexof_current_theme + 1) {
None => theme_cycle.iter().nth(0).unwrap().clone(),};
self.props.handle.reduce(move |state| state.theme = next_theme.to_string());
},Msg::ToggleLangDropdown => self.show_lang_dropdown = !self.show_lang_dropdown,Msg::ToggleThemeDropdown => self.show_theme_dropdown = !self.show_theme_dropdown,};
true
}
如果有人知道我第一次尝试做错了什么,那仍然很酷。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。