- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个类似网格的结构,其中有一个来自父对象的指针对象。现在通过一些操作,指针正在更新,但更改没有反射(reflect)在子组件中,DOM 也没有更新。
家长:
<script>
import Boxes from "./Boxes.svelte";
let boxes = [
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""]
];
let activeBox = {
x: 0,
y: 0
};
function handleKeydown(keyEvent) {
let i = activeBox.x;
let j = activeBox.y;
const width = boxes[i].length,
height = boxes.length,
left = 37,
up = 38,
right = 39,
down = 40,
tab = 9,
backspace = 8;
// Loop around single row with right and left arrows
if (keyEvent.keyCode == right) {
activeBox.x = activeBox.x + 1;
if(activeBox.x === boxes[i].length) activeBox.x = 0;
return;
}
$: console.log("^^^ activeBox &&", activeBox);
</script>
<style>
main {
display: flex;
align-items: center;
justify-content: center;
}
@media (min-width: 640px) {
main {
max-width: none;
}
}
</style>
<svelte:window on:keydown="{handleKeydown}" />
<main>
<Boxes boxes={boxes} activeBox={activeBox} />
</main>
子组件正在使用 activeBox 变量来呈现选定的框。但这似乎不起作用,因为当第一个渲染器完美运行时,盒子不会随之更新。
child :
<script>
export let boxes;
export let activeBox;
$: console.log("^^^ active Box updated");
function getSelectedClass(i, j) {
if (activeBox.x === j && activeBox.y === i) {
return "selected";
}
return "";
}
</script>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto auto auto auto auto auto;
background-color: #2196f3;
padding: 0px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
width: 40px;
height: 40px;
padding: 20px;
font-size: 30px;
text-align: center;
}
.selected {
border: 1px solid red;
}
</style>
<main>
<div class="grid-container">
{#each boxes as row, i}
{#each row as column, j}
<div class=" grid-item {getSelectedClass(i, j)}">{boxes[i][j]}</div>
{/each}
{/each}
</div>
</main>
我真的需要一些洞察力,因为我还没有正确理解 Svelte 的概念。任何帮助将不胜感激。
最佳答案
使用您的代码,activeBox
的值实际上已传播给子项,但您看不到它,因为子项中没有使用它的 react 代码。
当它所依赖的变量的值发生变化时,响应式(Reactive)代码会重新执行和重新评估,但仅限于立即出现在响应式(Reactive)代码中的变量。
因此,在这段代码中:
$: console.log("^^^ active Box updated");
...根本没有变数。所以这个 block 最初会运行一次,然后再也不会运行。
这个:
{#each boxes as row, i}
...
{/each}
只要 boxes
变量发生变化,就会重新计算。
在这方面:
<div class=" grid-item {getSelectedClass(i, j)}">...</div>
... react 值{getSelectedClass(i, j)}
仅“看到”变量getSelectedClass
、i
和j
。 i
和 j
是局部变量,因此 Svelte 不会跟踪它们。 getSelectedClass
是一个顶级变量,因此它会被跟踪并且如果它的值改变了 block 重新评估(即 getSelectedClass = 'foo'
),但这里从来不是这种情况。
那么...如何解决?
只需在响应式表达式中添加 activeBox
即可让编译器在其值更改时重新计算它(即使它实际上并未在函数中使用)。因此,这将按原样修复您的代码:
<div class=" grid-item {getSelectedClass(i, j, activeBox)}">...</div>
另一个选择是使用另一个基于实际包含 activeBox
变量的表达式的结构:
<div class="grid-item" class:selected={activeBox.x === j && activeBox.y === i}>{boxes[i][j]}</div>
这是一个中间立场:
<script>
// NOTE this block contains activeBox variable, so isSelected is recreated when
// activeBox changes
$: isSelected = (i, j) => activeBox.x === j && activeBox.y === i
</script>
<main>
<div class="grid-container">
{#each boxes as row, i}
{#each row as column, j}
<div class="grid-item" class:selected={isSelected(i, j)}>{boxes[i][j]}</div>
{/each}
{/each}
</div>
</main>
关于svelte - 如何在 Svelte 中的 Prop 更改时刷新 DOM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60154209/
我看了你的文章 https://svelte.dev/blog/virtual-dom-is-pure-overhead和其他一些博客,但它们解释了与虚拟 dom 相比,svelte 如何更好。而我想
我正在尝试制作一个 Svelte 应用程序,其中我有一个创建 audioSource 并管理它的类(不是 svelte 组件)。 我想跨多个组件获取此类实例。 现在,我发现的唯一方法是这样做:
我有一个案例,我打开了两个选项卡 localhost:5000/和localhost:5000/stream并想更新 / 中的变量路径并查看 stream 中的更改实时路径。存储不是这样工作的,如果我
我正在尝试创建一个带有客户端文件输入的网站,其中一些数据完全在客户端上处理,并且不会发送到服务器。 我有以下 Svelte 组件 (App.svelte): let data; function h
我正在尝试创建一个带有客户端文件输入的网站,其中一些数据完全在客户端上处理,并且不会发送到服务器。 我有以下 Svelte 组件 (App.svelte): let data; function h
我正在研究 Svelte,并从其 Github 存储库中克隆了它。我可以从站点目录运行 REPL,但它使用来自 unpkg url 的 Svelte。我想尝试我本地构建的编译器并使用它而不是 unpk
我想将样式添加到 在纤细路由中标记,但我不能。 我试图添加一个有一些样式的类,但它没有用。 该类包含: .link { text-decoration: none; } 有没有人对此有解决方案
目前,当我在浏览器中打开开发人员工具时,我可以看到我的原始 svelte 组件,我想隐藏此信息并仅发送丑陋的代码。 这是我打开开发者工具时得到的。我正在构建一个单页应用程序。 这是我的 rollup.
我目前正在学习 Svelte,我想为这个副项目创建一个身份验证流程。通常,在 React 中,我习惯使用 Contexts 保存身份验证信息。 我想知道 Svelte 是否也可以执行类似的
调用 new Component({ target }) 将组件附加到目标,我想用新组件替换目标的所有旧内容。我该怎么做? 最佳答案 如果您正在混合服务器呈现的标记,最好使用 hydrate: tru
我有一个简单的 REPL example用于简单的列表详细信息编辑器。它由三个部分组成: Annotation是细节 Annotations遍历数据并创建 Annotation实例 App是创建 An
我正尝试在 Svelte 商店中维护实时股票报价,以便屏幕上的出价和要价实时更新。我不确定商店的结构如何来保持数据的 react 性和效率。数据来自 websocket,如下所示: {'symbol'
下面是一个非常简单的 Svelte 组件。 当您单击该按钮时,它会更新 pages[2].name。 Svelte 如何处理这个问题?它是否重新评估所有 3 个 DIV?或者它是否知道只修改第 3 个
我制作了一个用于输入财务数字的组件,用于我的输入表单。它现在工作得很好,除了一个奇怪的行为:当我点击输入字段时,它会按预期获得焦点,但是当它已经有焦点时点击它会带走焦点。 组件中只有一个 on:cli
我尝试实现一个自定义的 Svelte 商店,它允许我在订阅页面上“抛出”一个单一的状态对象。这是我准备的示例代码: import { derived, writable } from 'svelte/
我正在尝试安装带有 Svelte 的 Mermaid 来制作图表。所以我做了以下事情: npm 安装美人鱼 然后我得到以下内容: npm WARN svelte-app@1.0.0 No reposi
如何渲染使用 svelte.compile() 手动编译的组件?我正在编译这样的组件: import * as svelte from 'svelte/compiler'; var
我正在学习 Svelte 以及如何使用它制作自定义商店。我遇到了一个问题,当我尝试使用 update 更新可写数组时,它会在订阅该数组的组件中导致未定义的错误。 这是我的商店,我有一个简单的数组,其中
我正在尝试遍历数组以呈现具有 type 值的组件。 import One from './One.svelte'; import Two from './Two.svelte'; import
如何将数据(例如:导航栏标题)传递给父元素中使用的组件? import Nav from "../components/Nav.svelte"; let navTitle = "MyApp
我是一名优秀的程序员,十分优秀!