- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 Svelte(带有 Astro 的 SSR)的应用,并且我正在尝试将我的数据保存到 localStorage
。
关于这个 IMO 的文档还不够,因为我似乎经常遇到砖墙。
到目前为止我已经定义了这个商店:
import { writable } from 'svelte/store'
function createCart() {
const { subscribe, set, update } = writable([]);
return {
subscribe,
update: (item) => update(value => [...value, item]),
remove: (item) => update(n => {
if(n.length > 0) n.pop() // only remove last item
return n // update the store value
}),
empty: () => set([])
};
}
export const cart = createCart()
为了保存到localStorage
(并且不会弄乱我的SSR),我已经实现了这个:
import { writable } from 'svelte/store'
function createCart() {
const defaultCartValue = []
const isClient = typeof window !== 'undefined'
const initialValue = isClient ? window.localStorage.getItem('cart') : defaultCartValue;
const { subscribe, set, update } = writable(
isClient
? window.localStorage.setItem('cart', initialValue)
: defaultCartValue
);
return {
subscribe: subscribe(value => localStorage.setItem('cart', ([
value,
...initialValue
]))),
update: (value) => isClient
? window.localStorage.setItem(
'cart',
JSON.stringify([...value, item])
)
: update(value => [...value, item]),
// TODO: fix remove items from localStorage
remove: (item => update(n => {
if(n.length > 0) n.pop()
return n
})
),
empty: () => isClient
? window.localStorage.setItem('cart', defaultCartValue)
: set(defaultCartValue)
};
}
export const cart = createCart()
我不断收到无法读取未定义的属性“取消订阅”
,然后是stop不是一个函数
。
任何人都可以帮助我理解为什么我可能会看到这两个错误吗?
最佳答案
您收到的错误:
Cannot read property 'unsubscribe' of undefined followed by stop is not a function.
那是因为 subscribe
商店的方法必须返回unsubscribe
(又名 stop
)函数。文档中并没有过多强调这一点,但它显示在方法的签名中:
subscribe: (subscription: (value: any) => void) => (() => void)
但是您自己的代码并没有做到这一点,而且,就其值(value)而言,它可能并没有真正实现您的预期:
subscribe: subscribe(value => localStorage.setItem('cart', ([
value,
...initialValue
]))),
您正在调用原始 subscribe
创建商店时立即,而不是注册新订阅者时。自原创subscribe
确实返回 unsubscribe
函数,您在尝试订阅时不会收到“不是函数”错误。但您也没有获得订阅,并且 unsubscribe
函数不返回另一个函数,这解释了您所看到的错误。
你想要的是这样的:
subscribe: subscriber => {
// do something on every new subscriber
// subscribing
const unsubscribe = originalSubscribe(value => {
// do something every time the value change FOR EVERY SUBSCRIBER
subscriber(value) // notify the subscriber of the new value
}
// custom unsubscribe function
return () => {
// do something each time a subscriber unsubscribes
unsubcribe() // unsubscribe from the original store
}
}
如果您遵循上一个片段中的注释,您应该意识到包装 subscribe
可能不是你想要坚持的localStorage
,因为通过这种方式获得的“ Hook ”适用于每个订阅者,而不是每个商店一次。
实现您想要的效果的一种方法是覆盖 set
和update
可写存储的方法,如 svelte-local-storage-store
does :
const {subscribe, set} = store
stores[key] = {
set(value: T) {
updateStorage(key, value)
set(value)
},
update(updater: Updater<T>) {
const value = updater(get(store))
updateStorage(key, value)
set(value)
},
subscribe
}
另一种极其简单且方便的模块化方法就是订阅您的商店。
// a guard because we only want this in the clients
if (!isSSR() && typeof localStorage !== 'undefined') {
originalStore.subscribe(value => {
localStorage.setItem('key', JSON.stringify(value))
})
}
这仅适用于您不介意让您的商店“始终处于事件状态”的情况;也就是说,您可能不希望对“丰富”的商店执行此操作,例如,它在获得第一个订阅者时会发出请求。
利用原始商店 subscribe
的另一种方式在避免急切订阅它的陷阱的同时,将上述逻辑包装在可读的存储中。
const createPersistedStore = initialValue => {
const internal = writable(initialValue)
const persisting = readable(initialValue, set => {
// called when the store becomes active (i.e. first subscriber arrives)
const unsubscribe = internal.subscribe(value => {
updateLocalStorage(value) // <== will see each change once
set(value)
})
return () => {
// called when the store becomes inactive (i.e. last subscriber leaves)
unsubscribe()
}
})
return {
set: internal.set,
update: internal.update,
subscribe: persisting.subscribe,
}
}
关于local-storage - 将 Svelte 存储持久保存到 localStorage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71639868/
嗨,当尝试将图像上传到 firebase 存储时,我正在使用 firebase 文档,但是出现此错误。 在范围内找不到“存储” let storage = Storage.storage() le
我最近在使用 Firebase 存储时遇到了一些问题。 当我们尝试访问刚刚上传的文件时,浏览器中出现此错误消息 { "error": { "code": 400,
这些是在不同版本的 NuGet 包之间迁移的重要指南: https://github.com/Azure/azure-sdk-for-net/blob/Azure.Storage.Blobs_12.6
警告: Warning: Can't resolve all parameters for Storage in /Users/zzm/Desktop/minan/node_modules/@ioni
我在圆形立方体中收到此错误(“连接到存储服务器失败”)行。我已经检查了所有内容,配置和数据库用户名密码,服务器详细信息都是干净的。谁能告诉我可能是什么问题。这里我给出了整个配置文件。
我希望能够限制容器的大小,但是使用默认的存储驱动程序aufs(对于Ubuntu 14.04),当我尝试使用--storage-opt参数时出现错误 $ docker create -it --name
我希望能够支持对使用 Google Cloud Storage 托管的静态 Assets 进行 Brotli 和 Gzip 编码。为此,我想在将文件上传为 之前对其进行编码, .gz和 .br .问
场景 我有几个由 Google Cloud Storage object.finalize 事件触发的 Google Cloud Functions。为此,我使用两个存储桶并使用“同步选项:覆盖目标位
我在 Google Cloud Storage 中有一个存储桶和一个网站。人们目前可以通过网站上传到存储桶(使用 Google 身份验证)。 但是,我需要设置它以便任何人都可以查看上传的文件(并且不能
如果文件被放入 Google Cloud 存储并公开,但该文件的网址在另一个网页上不存在,那么 Google 是否会在其搜索结果中将其编入索引?有人知道吗? 最佳答案 Google 的搜索索引独立于其
截至今天早上,我无法访问我的存储桶。 当我在导航上选择 Google Cloud Storage 选项卡时,一切都按预期加载,但不是显示我的两个存储桶,而是显示一个警告栏说: We were unab
我想弄清楚是否可以在 Windows 平台上使用 gsutil 的 cp 命令将文件上传到 Google Cloud Storage。我的本地计算机上有 6 个文件夹,每天都会向其中添加新的 pdf
我最近开始使用 Google Cloud Storage。最初我在安装 Cloud SDK 时创建了一个虚拟项目。现在我正在做另一个项目。 gsutil 仍然指向我以前的项目。我如何使它指向我的新项目
我目前正在这样做,但它非常慢,因为我的存储桶中有几 TB 的数据: gsutil du -sh gs://my-bucket-1/ 对于子文件夹也是如此: gsutil du -sh gs://my-
这可能看起来很天真,我知道我们可以在 blob 中创建文件夹,并且这些文件夹仍然存储在容器中。我们仍然可以对这些“blob 中包含的文件夹”执行通常对文件存储中的文件夹执行的所有操作。 我们仍然可以像
将文件上传到 Google Cloud Storage 时,有一个自定义数据字段元数据。 Google's example相当短: var metadata = { contentType: 'a
这可能看起来很天真,我知道我们可以在 blob 中创建文件夹,并且这些文件夹仍然存储在容器中。我们仍然可以对这些“blob 中包含的文件夹”执行通常对文件存储中的文件夹执行的所有操作。 我们仍然可以像
我有一个包含超过 2 万个文件名的 Google Storage 存储桶。有没有办法在短时间内列出存储桶中的所有文件名? 最佳答案 这取决于您所说的“短”是什么意思,但是: 您可以做的一件事来加快列出
有谁知道如果文件不存在,您是否需要为 Google Cloud Storage 中的文件请求付费?换句话说,有人访问您存储桶中不存在的文件是否计入您的请求?还是仅适用于存在的文件? 最佳答案 客户无需
在每一分钟结束时,我的代码总共会上传 20 到 40 个文件(从多台机器上并行上传大约 5 个文件,直到全部上传完毕)到 Google Cloud Storage。我经常收到 429 - Too Ma
我是一名优秀的程序员,十分优秀!