更新通过Vue组件传递的数据对象

时间:2018-10-03 17:53:26

标签: javascript vue.js vuejs2

我是Vue的新手,并且继承了我需要修改的代码库。有一个名为data.js的文件,其中包含一些包含城市信息的对象,例如:

IF EXISTS(select * FROM sys.views where name = 'monthnotexict')
drop view monthnotexict
go
    create view monthnotexict as
        select 1 as month_table,0 as cstart ,0 as cend  from newdata union 
        select 2 as month_table,0 as cstart ,0 as cend from newdata union 
        select 3 as month_table,0 as cstart,0 as cend from newdata union 
        select  4 as month_table,0 as cstart,0 as cend from newdata union 
        select  5 as month_table,0 as cstart,0 as cend from newdata union 
        select  6 as month_table,0 as cstart,0 as cend from newdata union 
        select  7 as month_table,0 as cstart,0 as cend from newdata union 
        select  8 as month_table,0 as cstart,0 as cend  from newdata union 
        select  9 as month_table,0 as cstart,0 as cend from newdata union 
        select  10 as month_table,0 as cstart,0 as cend from newdata union 
        select  11 as month_table,0 as cstart,0 as cend from newdata union 
        select  12 as month_table,0 as cstart,0 as cend from newdata


    go
     SELECT DATENAME(month, DATEADD(month,M.month_table-1, CAST('2008-01-01' AS datetime)))as month_table
             , sum(cstart) as cstart 
             , sum(cend) as cend 
          from monthnotexict left join
             (
              (select 
                    , 1 as cstart
                    , 0 as cend 
                 from newdata

              ) 
      union all 
              ( 
                     , 0
                     , 1 
                  from newdata 

               ) 
    union all
    (
    select
    month_table
                 , sum(cstart) as cstart 
                 , sum(cend) as cend  
        from monthnotexict
group by month_table

    )


              ) dd on dd.cend=M.cend or dd.cstart=M.cstart

  group by M.month_table

在一个组件index.vue中,数据像其他任何JS对象一样被导入:

export default {
  nyc:
    cleaning: 3,
    maintenanceS: 1
  }
}

在另一个组件中,将其作为道具导入:

import data from '../components/logic/data'

我一直在阅读Vue指南,并且对如何将道具从父组件传递下来有一个较宽松的了解。我是否正确认为index.vue是将“数据”作为道具的任何其他组件的父组件?

我需要让用户通过文本框修改“数据”值:

export default {
  data () {
    return {}
},
props: ['data'],
computed: {
...

我是否认为v模型是更新这些值的正确方法,以便在所有组件中使用新值?我猜想我还需要在组件中编写一些Javascript来进行更新,但是我不确定该怎么做。如何更新此值,以便在使用“数据”对象的所有组件中使用该值?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果父组件有多个子组件,则可以使用props向其传递数据,并且该父组件中的任何更改都会自动影响所传递的数据,假设我们有一个包含两个父组件数字,并且有两个子元素,一个用于计算总和,另一个用于相乘:

Vue.component('sum', { 
props:["n1","n2"],
template:'<div>Sum => <h3>{{n1+n2}}</h3></div>'

})

Vue.component('mult', { 
props:["n1","n2"],
template:'<div>Mult => <h3>{{n1*n2}}</h3></div>'

})

new Vue({
  el: '#app',
data:{
      num1:0,
      num2:0       
     } 
})
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>

<div id="app">
     <input type="number" v-model.number.lazy="num1" class="form-control">
      <input type="number" v-model.number.lazy="num2" class="form-control">
     <span>Parent => ({{num1}},{{num2}})</span>
     <sum :n1="num1" :n2="num2"></sum>
    <mult :n1="num1" :n2="num2"></mult>
</div>

这是一个非常基本的示例,但它显示了该层次结构的用法和实用程序,并且v-model指令在处理表单和输入时非常有用