- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 BlogPost 对象和一个 Comment 对象,大致如下所示:
class Comment {
constructor (blogId, text) {
this.blogId = id
this.text = text
}
}
class BlogPost {
constructor (id, text) {
this.id = id
this.text = text
}
get comments () {
return commentCollection.filter(comment => comment.blogId === this.id)
}
}
我希望 comments
getter 看起来像一个响应式属性。给定一个像这样的 vue...
<template>
<div>
<h1>The post has {{myBlogPost.comments.length}} comments</h1>
<v-btn @click="addComment()">Add Comment</v-btn>
</div>
</template>
<script>
export default {
data () {
return {
myBlogPost: null
}
},
methods: {
let newComment = new Comment('myBlogId_0', 'This is a comment on hello world')
commentCollection.splice(0, 0, newComment)
},
mounted () {
this.myBlogPost = new BlogPost('myBlogId_0', 'Hello World')
}
}
</script>
...我希望在用户添加评论时更新评论计数。这是怎么做到的?我无法使 BlogPost
评论集合具有反应性,因为它并不是真正的属性。
我尝试向 vue 添加一个 compulated
方法,该方法调用 BlogPost 上的“getter”,但这似乎并未设置与评论集合的依赖关系。而且 Vue.set()
似乎没有提供帮助。我可以在哪里“设置”一些东西来让 vue 使用react?
我唯一的想法(我认为可行)是在评论集合上设置一个观察者,并让该观察者通过调用 comments
getter 来更新数据中的其他值,但我可能会在同一对象和其他对象中存在数十种这样的情况。我想避免编写这么多的观察者并在数据中保留这么多额外的状态。有任何想法吗?谢谢/
最佳答案
这可能会有所帮助:
<template>
<div>
<h1>The post has {{myBlogPost.comments.length}} comments</h1>
<v-btn @click="addComment">Add Comment</v-btn>
</div>
</template>
<script>
export default {
data () {
return {
myBlogPost: {
comments: []
}
}
},
methods: {
addComment() {
let newComment = new Comment('myBlogId_0', 'This is a comment on hello world')
// commentCollection is not defined in this snippet
commentCollection.splice(0, 0, newComment)
this.myBlogPost.comments.push( newComment )
}
// let newComment = new Comment('myBlogId_0', 'This is a comment on hello world')
// commentCollection.splice(0, 0, newComment)
},
mounted () {
this.myBlogPost = new BlogPost('myBlogId_0', 'Hello World')
}
}
</script>
关于javascript - 如何在 VueJS 中使类的 "virtual"getter 响应式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55498623/
进程虚拟机和系统虚拟机有什么区别? 我的猜测是,进程 VM 没有为该操作系统的整个应用程序提供一种操作系统,而是为某些特定应用程序提供环境。 系统虚拟机为操作系统提供了一个安装环境,就像 Virtua
我在成员函数的上下文中理解 virtual,例如 virtual void frob()。但它在类声明的上下文中意味着什么,例如 class Foo : public virtual Bar? 对于给
根据 react-virtualized 文档,“AutoSizer 组件装饰 React 元素并自动管理宽度和高度属性,以便装饰元素填充可用空间”。 建议通常是加上height: 100%;或 fl
根据 this类似 StackOverflow 问题和其他文章,默认情况下 C# 方法是“非虚拟的”,我认为这意味着您不能在派生类中覆盖它们。 如果那是真的,能否请您向我解释一下,在下面的示例中,我如
我有一个基类Media和几个派生类,即DVD、Book等...基类写成: class Media{ private: int id; string title;
我搜索了一些关于虚函数声明的帖子,相信 =0 在 virtual void test()=0; 是固定句法所以 virtual void test()=NULL; virtual void test(
我正在使用 RV 列表加载具有自定义格式的大型文档。它非常有效,但我遇到了以下两个问题: 我目前在 cellmeasurer 中设置了一个列表 based on this计算行的动态高度(宽度是固定的
我一直在努力制作 this react virtualized table example工作 & 开始严重怀疑我的理智。我创建了一个 react 应用程序,我只是想在 App.js 中使用以下内容呈
我在Windows 7 Pro计算机上安装了Windows Virtual PC和Windows XP Mode。运行XP模式会在Virtual PC上自动安装XP。我想创建第二台与第一台相同的虚拟P
我使用 Virtual PC 来创建新的环境来测试我的安装程序。但我一定是做错了什么,因为内部装有 Vista 或 XP 的 VPC 镜像占用了大约 15GB 的磁盘空间(包括安装在其中的 VS200
是否可以为 Ubuntu 虚拟机动态分配处理器和内存?例如。进程在主机系统上运行,导致处理器的使用率从 30%-70% 上下波动,这些进程还占用 8GB 内存中 3GB-7GB 之间的波动量,即 1G
我正在使用“react-virtualized”来创建一个表。在该表中,一些数据可能显示为 'Brian Vaughn1'。 .此表格单元格应具有 font-weight: bold并且只应呈现文本,
我正在使用“react-virtualized”来创建一个表。在该表中,一些数据可能显示为 'Brian Vaughn1'。 .此表格单元格应具有 font-weight: bold并且只应呈现文本,
我一直在努力理解一段这样的代码: class A { // some class definition } class B { public: virtual A *s
基于 http://en.wikipedia.org/wiki/Virtual_inheritance class Animal { ... }; // Two classes virtually i
我看到 C++ 中的某些函数被声明为 virtual const int getNumber(); 但是如果函数声明如下有什么区别呢? const virtual int getNumber(); 这
问题来自C++ faq。 http://www.parashift.com/c++-faq-lite/protected-virtuals.html 使用公共(public)重载虚拟的代码: clas
这个问题在这里已经有了答案: How is "=default" different from "{}" for default constructor and destructor? (3 个答案
virtual final 函数(final 在基类)是否有任何 vtable/virtual 成本? class B{ public: virtual void fFinal() final
我有一个只包含 exe 文件(没有源代码)的 hello 工具。 你好工具结构: bin helloBin.exe helloRoot.exe conanfile.py conanfile.py
我是一名优秀的程序员,十分优秀!