【发布时间】:2017-10-14 03:40:07
【问题描述】:
场景
我有一个通用的模态组件,我使用全局总线(空 VUE 实例)与使用它的任何其他组件的模态组件进行通信。
问题
在 Modal.VUE 组件的 Mounted() 或 Created() 钩子中,我试图覆盖默认的初始化值,我需要确定哪些内容要在模态中显示。
console.log("Selected action is : " + actionName)
...提示正确的actionName,所以总线功能就在那里..
但是当像这样设置变量时:
this.modalComponent == actionName
.. 并像这样使用它:
<h2 v-if="modalComponent == 'refund'">Refundér</h2>
<h2 v-if="modalComponent == 'empty'">Not defined</h2>
.. modalComponent 值始终为空(如初始化)
脚本代码:
<script>
import bus from '../global/bus.js'
export default {
name: "modal",
data(){
return {
modalComponent: "empty"
}
},
mounted() {
bus.$on('on-action', function (actionName) {
console.log("Selected action is : " + actionName)
this.modalComponent == actionName
})
}
}
那么我在这里做错了什么?这是我初始化的方式吗? mount() 或 created() 钩子有问题吗?或者..我设置新值的方式?
【问题讨论】:
-
尝试使用
this.$set('modelComponent', actionName); -
你能在控制台记录下挂载的内部总线中'ths'的值是多少。$on
-
@GONG :给我:未捕获的类型错误:无法在 Vue$3.set [as $set] 的字符串 'modelComponent' 上创建属性 'refund'
-
@user7814783 : 查看更新后的问题与 console.log(this) 的打印屏幕
-
@TerjeNygård 你在 bus.$on 中使用 double ==,分配一个值你应该使用 single =
标签: javascript vue.js vuejs2 vue-component