vuejs2 - VueJs如何从Vue.component获取数据

标签 vuejs2

我想知道如何从Vue.component获取数据并将其发送到
这个>> var app = new Vue({})<<
这是我的代码

Vue.component('my-form', {
template: '#my-form',
props:['ddTechtemp'],
     data: function () {
     return {
        isCores: app.testCorres,
        activeClass: 'isCorrespon',
        errorClass: 'isTech',
        tempData : {cell1 : "",
                    cell2 : "",
                    cell3 : "",
                    cell4 : "",
                    cell5 : "",
                    cell6 : ""
        },
    }
},
watch:{
    tempData:{
        handler:function(newVal,oldVal){

            app.ddTechtemp = newVal;

        },
        deep:true,
    }

},
methods:{

}});

我想从上面的代码中获取数据并发送到此代码var app = new Vue({ data: Vue.component.data}) 有人了解我,请帮忙。谢谢

最佳答案

在Vue.js中,父子关系由

1)将 Prop 从 parent 传递给 child

2)从 child 向 parent 发出自定义事件

因此,如果您需要将某些数据从子级传递给父级,请使用this.$emit随数据一起发送自定义事件,然后使用v-on:myevent@myevent速记在父级组件中监听此事件。与事件一起传递的数据可在$event变量中找到。

示例:https://jsfiddle.net/wostex/63t082p2/51/

<div id="app">
  <myform @newdata="handleData($event)"></myform>
  <p>name: {{ user.name }}</p>
  <p>age: {{ user.age }}</p>
</div>

new Vue({
  el: '#app',
  data: {
    user: { name: '', age: 0 }
  },
  methods: {
    handleData: function(e) {
      [this.user.name, this.user.age] = e;
    }
  },
  components: {
    'myform': {
      template: `
      <div>
      <input type="text" v-model="formData.name" placeholder="Your name">
      <input type="number" v-model.number="formData.age">
      </div>`,
      data: function() {
        return { formData: { name: '', age: 0 } }
      },
      watch: {
        formData: {
            handler: function() {
              this.$emit('newdata', [this.formData.name, this.formData.age]);
          },
            deep: true
        }
      }
    }
  }
});

关于vuejs2 - VueJs如何从Vue.component获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43677645/

相关文章:

javascript - 在 vue computed 中使用箭头函数不起作用

javascript - Vue.Draggable (sortable.js) 中的 ondragover 等价物

javascript - Vue.js - vue-router 的基本 url 区分大小写吗?

vue.js - 在 v-for 中选择/取消选择单个元素

javascript - 如何在 VueJS 应用程序中使用 Google Web 字体?

javascript - 使用 django REST 和 Vue.js 传播不可迭代实例的无效尝试

vue.js - Vue Prop 未定义

javascript - 如何重用方法: define in main object and call from all component in VueJS

vue.js - Vue Tables 2自定义行样式

javascript - 在Vue js中将组件模板拆分成多个部分有什么可行的解决方案吗?