使用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
}

如果有人知道我第一次尝试做错了什么,那仍然很酷。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...