Cannot access 'variable_name' before initialization

后端 未结 1 1996
盖世英雄少女心
盖世英雄少女心 2021-01-07 18:43

When using reactive variables by declaring them using the $: syntax, you get the following error.

Cannot access \'variable_name\' before initializa

相关标签:
1条回答
  • 2021-01-07 19:06

    When you assign variables using $: you cannot assign them as part of other variables declared using let, const, or var.

    When you do assignments using $:, you can only use them in other variables assigned using $:. In the code posted above, you need to change the following lines:

    let namesWithA = bandmembers.filter(d => {
                if (d.indexOf('a') > 0) {               
                    return true;
                }
            else {
                return false
            }
        })
    

    to the following:

    $: namesWithA = bandmembers.filter(d => {
                if (d.indexOf('a') > 0) {               
                    return true;
                }
            else {
                return false
            }
        })
    

    It took me a while to figure this out and just wanted to share this with any other Svelte newbies that are starting out in this new technology.

    0 讨论(0)
提交回复
热议问题