vue.js - 更新数据时Vue2组件不呈现
问题描述
我有一个案例,当我更新它的数据时,Vue 2 组件没有呈现:
Vue.component('framer-deal', {
data: function(name,src) {
return {
name : "Image1",
src : "https://via.placeholder.com/250"
}
},
created: function () {
eventHub.$on('clickedImage', this.updateimage)
},
methods:{
updateimage : function(imgsrc) {
this.src = imgsrc;
this.name = imgsrc;
console.log("thumbnail", this.$data.name)
}
},
template: '<div><h3>Hello {{name}} {{src}}</h3><img v-bind:src="this.src" /></div>'
})
JS Bin 示例:https ://jsbin.com/wokiqozipa/edit?js,output
(我正在使用 eventthub 来触发组件上的方法,但这不应该是不触发渲染的原因。)
解决方案
问题是您的示例中有 2 个 Vue 实例:
var vm = new Vue({
el: '#app',
data: {
json: mydata
},
methods: {
open: function(e){
imageframe = e.target.src;
eventHub.$emit('clickedImage', imageframe)
}
}
});
new Vue({ el: '#frame' }) // remove this to make it work
删除new Vue({ el: '#frame' })
以使其工作...
也不要this
在模板中使用。
改变
<div><h3>Hello {{name}} {{src}}</h3><img v-bind:src="this.src" /></div>
至
<div><h3>Hello {{name}} {{src}}</h3><img v-bind:src="src" /></div>
推荐阅读
- coq - 是否可以在不使用反转的情况下在 Coq 中证明 `forall n: nat, le n 0 -> n = 0.`?
- sql - 在 2 个表 SQL 之间的列中查找具有不同值的所有行
- sql - Redshift/SQL 中前 20 个百分位数的计算
- eclipse - 如何将 zip 文件添加到 Eclipse 中的 Junit 测试?
- apache-kafka - 命令式与函数式 kafka 流
- python - 是否有比较两个 DataFrame 并输出不同元素的功能?
- php - 颤振图像上传在真实设备上不起作用它在模拟器上工作
- javascript - 如何使用 JavaScript 在 Microsoft Edge(Ctrl + Shift + T)中禁用“重新打开最后一个选项卡”?
- json - 为什么 Spatie laravel-webhook-client 有效负载仅在生产服务器上为空?
- azure - 如何将 VPN 站点连接到 Azure vWAN 上的 vHub?