简体   繁体   English

svelte 组件的输出

[英]Output of the svelte component

When i add svelte component on page, i create the new exemplar of class.当我在页面上添加 svelte 组件时,我创建了新的类示例。

import ComponentA from "./Component.svelte";
let component = new ComponentA({...});


<ComponentA/>

And every time component destroying and created again.并且每次组件销毁并再次创建。
But I need to save the state of the component.但是我需要保存组件的状态。
(example: the display of blocks, position of blocks, text, and much more) (例如:块的显示、块的位置、文本等等)
When I create new exemplar, I can put it in global variable.当我创建新的范例时,我可以把它放在全局变量中。 Can I output it from global?我可以从全局输出吗? Is that real?那是真的吗?

First of all, learn about stores .首先,了解店铺

Example例子

for example, to save the scrolling position in your App.svelte component (and not lose them if you change the component or ...):例如,要在 App.svelte 组件中保存滚动位置(并且如果您更改组件或...,则不会丢失它们):

export a variable in your stores.js在你的 stores.js 中导出一个变量

  export const AppY = writable(0);

then bind your Y position (of your App.svelte component with svelte:scrollY) to a variable.然后将您的 Y 位置(您的 App.svelte 组件的 svelte:scrollY)绑定到一个变量。
in your App.svelte :在你的 App.svelte 中:

  import { AppY } from "../stores.js";
  // some code
  let Y = ... // bind your svelte:scrollY  here
  $: $AppY = Y;

Now you have the scrollY position in your writable store.现在您在可写存储中拥有了 scrollY 位置。 to save this position for long time, you can use localStorage .要长时间保存此位置,可以使用localStorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM