我想知道我是否能够从一个普通的 .js 文件访问我的Svelte存储值。
我正在尝试编写返回基于存储值的动态值的函数,以将它们导入任何组件。但在一个普通的 .js 文件中,我不能只用 $ 符号访问存储值..
使用存储值并可用于多个组件的基本函数的快速示例:
//in .svelte
function add() {
$counter = $counter + 1;
}
Run Code Online (Sandbox Code Playgroud)
编辑:改写一下
编辑: 找到了一个解决方案,但我真的不知道它是否真的优化了..
//in .js file
import { get } from "svelte/store";
import { counter } from "./stores";
export function add() {
var counterRef = get(counter);
counter.set(counterRef + 1);
}
Run Code Online (Sandbox Code Playgroud) 我有一系列像这样的玩家对象:
var players = [{
id: "thisIsID1",
name: "William",
otherProps
},
{
id: "thisIsID2",
name: "Shakespeare",
otherProps
},
{
id: "thisIsID3",
name: "Lola",
otherProps
}]
Run Code Online (Sandbox Code Playgroud)
我已经整理了他们的 ID 数组,如下所示:
var shuffledIDs = ["thisIsID2", "thisIsID3", "thisIsID1"]
Run Code Online (Sandbox Code Playgroud)
如何对playersvar 进行排序,以便对象的顺序与 的相应 ID 的顺序相同shuffledIDs?
编辑:不同的名字只是为了让玩家不同