如何安装svelte_Svelte的可读写商店入门

如何安装svelte

If you’re familiar with Redux or Vuex, then the Svelte stores offer a similar feature for state management. If your app is getting complicated, then it becomes difficult for components to relay data between themselves. Moving it to a global data store is a better option. Here, we’ll look at two store options that Svelte makes available: writable stores and readable stores.

如果您熟悉Redux或Vuex,那么Svelte商店提供了类似的状态管理功能。 如果您的应用变得越来越复杂,则组件之间很难在它们之间中继数据。 将其移动到全局数据存储是一个更好的选择。 在这里,我们将看看Svelte提供的两个商店选项: 可写商店和可读商店 。

可写存储 (Writable Store)

Let’s go ahead and create a global state management file in our Svelte project - let’s call it store.js and import the writable function.

让我们继续在Svelte项目中创建一个全局状态管理文件-我们将其store.js并导入writable函数。

import { writable } from "svelte/store";

let counter = writable(1);

export {counter}

We’ve created a variable called counter, which is a writable store. counter now has the following self-explanatory methods:

我们创建了一个名为counter的变量,这是一个可写的存储区。 counter现在具有以下不言自明的方法:

  • set

    set

  • update

    update

Let’s create a custom component called Nested.svelte and use the counter store we just created.

让我们创建一个名为Nested.svelte的自定义组件,并使用刚刚创建的counter存储。

<script>
import { counter } from "./store.js";
</script>

<div>
 counter value: {$counter}
</div>

Notice that during usage, we prefix the the variable with $, since this is a named import.

注意,在使用过程中,我们给变量加上$前缀,因为这是一个命名的import。

Let’s wrap it up by importing the component in the App.svelte file and create a method to write the counter variable to observe reactivity across nested components.

让我们通过将组件导入App.svelte文件中来进行App.svelte并创建一种方法来编写counter变量,以观察嵌套组件之间的React性。

<script>
import Nested from "./Nested.svelte";
import { counter } from "./store.js";

function incrementCounter() {
  counter.update(n => n + 1);
}
</script>

<div>
<button on:click={incrementCounter}>Update</button>
<Nested />
</div>

the counter uses an update method that takes a function whose parameter is the current value of the writable store and returns the modified value. If we run this app, we should be able to see the value inside the Nested component getting updated as we click on the button.

counter使用一种update方法,该方法采用一个函数,该函数的参数是可写存储的当前值,并返回修改后的值。 如果运行此应用程序,则单击按钮时,我们应该能够看到Nested组件中的值正在更新。

While we’re at it, let’s go ahead and add a reset button to App.svelte.

在此过程中,让我们继续,向App.svelte添加一个reset按钮。

function resetCounter() {
  counter.set(1);
}
<button on:click={resetCounter}>Reset</button>

The resetCounter uses the set method of our writable store.

resetCounter使用我们可写存储的set方法。

Now, the writable function also supports a second argument which is also a function. Here’s the signature for that function:

现在, writable函数还支持第二个参数,它也是一个函数。 这是该功能的签名:

writable(value: any, (set: (value: any) => void) => () => void)

This function is fired when the first subscriber is created and it returns another function that is fired when the last subscription to the variable is destroyed. Let’s see that in action.

当创建第一个订阅者时将触发此函数,并返回在最后一次对该变量的订阅被销毁时将触发的另一个函数。 让我们看看实际情况。

In our store.js, add the second argument to the writable function:

在我们的store.js ,将第二个参数添加到可写函数中:

let counter = writable(1, () => {
  console.log("First subscriber added!");
  return () => {
    console.log("Last subscriber deleted :(");
  };
});

To test this, we’ll mount and unmount the Nested component to observe the behavior, in App.svelte:

为了测试这一点,我们将在App.svelte安装和卸载Nested组件以观察其行为:

<script>
// ...
let flag = false;
function toggleMount() {
    flag = !flag;
}
</script>

  <!-- ... -->

  <button on:click={toggleMount}>Mount/Unmount</button>

  {#if flag}
    <Nested />
  {/if}
</div>

可读商店 (Readable Store)

Svelte also offers the readable function, which allows for creating readable stores whose values cannot be updated from other components. The value has to set from within the store. Let’s try this out, modify the store.js -

Svelte还提供了readable功能,该功能允许创建其值无法从其他组件更新的可读存储。 该值必须在商店内set 。 让我们尝试一下,修改store.js

import { readable } from "svelte/store";

let initialVal = Math.floor(Math.random()*100);

let counter = readable(initialVal, (set) => {
  let incrementCounter = setInterval( () => {
    let newVal = Math.floor(Math.random()*100);
    set(newVal);
  }, 1000);
  return () => {
    clearInterval(incrementCounter);
  };
});

export {counter}

Here the readable counter is set with the initialVal, which is being passed as the first argument. The second argument is the same as with writable stores, but this time it’s a required parameter since without it there would be no other way to access the counter value to reset it.

在这里, readable计数器设置为initialVal ,它将作为第一个参数传递。 第二个参数与可写存储区相同,但这一次它是必需的参数,因为如果没有它,将没有其他方法来访问counter值以将其重置。

In this example, we generate random numbers between 0 to 100 and assign this to counter by using the set method. update is not available. This is a simple demo, but in real apps readable stores can use the second argument to make API calls and, based on some logic, set the value. This will render the components that are subscribed to this store.

在此示例中,我们生成0到100之间的随机数,并使用set方法将其分配给counterupdate不可用。 这是一个简单的演示,但是在实际应用中,可读存储可以使用第二个参数进行API调用,并根据某些逻辑set该值。 这将呈现已订阅此商店的组件。



As you saw, by using writable and readable stores in Svelte, we can achieve a basic form of global state management pretty easily! ✨

如您所见,通过在Svelte中使用writablereadable存储,我们可以轻松实现全局状态管理的基本形式! ✨

翻译自: https://www.digitalocean.com/community/tutorials/svelte-svelte-store

如何安装svelte

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值