【问题标题】:How to Initialize Data Properties with Prop Values如何使用道具值初始化数据属性
【发布时间】:2017-08-29 16:05:09
【问题描述】:

在 VueJS 中仍然有点年轻,但我喜欢它的每一点。但现在,固定在某个地方。
我想使用通过props 传递的值初始化data() 中的一些值。这样我就可以在以后改变它们,因为不建议在组件内改变 props。事实上official docs 推荐使用prop 值初始化这个属性,如下所示:

{
props: ['initialCounter'],
data: function () {
  return { counter: this.initialCounter }
}

我有类似下面的东西:

<template>
<div class="well">
    <!-- Use Prop value directly on the template: works (but of no help in initializing data) -->
    Department: {{department.name}}
    <!-- Use prop value but gotten via computed property: Works inside the template but not in the initialization -->
    Department: {{fetchDepartment.name}}
    <!-- Use the array I initialized with the prop value: Does not work -->
    Department: {{this_department.name}}
</div>
</template>

<script>
    export default {
        name: 'test',
        props: ['department'],
        data() {
            return {
                this_department: this.department
                // below does not work either
                //this_department: this.fetchDepartment
            }
        },
        created() {
            // shows empty array
            console.log(this.department)
        },
        mounted() {
            // shows empty array
            console.log(this.department)
        },
        computed: {
            fetchDepartment() {
                return this.department
            }
        }
    }
</script>

从上面的注释部分可以看出,初始化不成功。 this.department 的值也不会出现在 created()mounted() 挂钩中。请注意,我可以看到它是使用 Chrome Vue Devtools 定义的。所以我的问题是,我应该如何使用 props 值初始化 data() 属性,或者哪种是解决这个问题的最佳方法?

【问题讨论】:

  • department 是异步填充的吗?你在做什么是正确的。但如果它是异步的,那么初始化的值将是 null 并且不会被更新。在这种情况下,计算是正确的方法。
  • &lt;test :department="department"&gt;&lt;/test&gt; ,其中department 定义明确。虽然不确定你所说的异步是什么意思
  • 你是否从 API 中获得价值?
  • 好提示。让我检查一下,我会回来的:-)

标签: vue.js vuejs2


【解决方案1】:

我知道我的答案来晚了,但它对我有帮助,希望其他人来这里。当 props 的数据异步时:

// in the parent component
<template>
    <child :foo="bar" v-if="bar" />
</template>

这样,您可以在 props 可用时渲染组件,从而更安全地按照指南推荐的方法使用 props 初始化数据值:

props: ['initialCounter'],
data: function () {
    return {
       counter: this.initialCounter
    }
}

编码愉快!

【讨论】:

    【解决方案2】:

    计算属性是提供可变版本道具的最简单方法,但您可能不希望在道具更新时丢失数据。您可以使用显式手表。

    观察者

    虽然计算属性在大多数情况下更合适,但有 是需要自定义观察者的时候。这就是为什么 Vue 提供 通过 watch 选项对数据更改做出反应的更通用的方式。 当您想要执行异步或昂贵时,这是最有用的 响应数据变化的操作。

    这在你想执行异步或昂贵的时候最有用 响应数据变化的操作。

    【讨论】:

      【解决方案3】:

      您可以修改道具。使用'.sync' 修饰符。我经常使用它,因为它既方便又直观。这需要发出一个事件来更新父级的值。我不确定它如何导致维护问题的警告。

      如果我想修改一个值而不更新父级,我使用的另一种方法是使用 Lodash 克隆。例如(假设它在挂载时可用)

      mounted(){
          this_department = _.clone(this.department)
      }
      

      如果你一直想改变 prop 并让它随父元素一起改变,那么使用计算属性。但是,在大多数情况下,您将希望依赖组件中该数据的状态并使用其他函数对其进行更改,因此计算属性将不是您所需要的。

      【讨论】:

      • 这不处理初始渲染。
      • @MichaelCole 通过修改道具,在初始渲染时,您将收到来自父组件的道具数据。当然,如果您在父组件上选择或更改它,您可以在 mount 或 created 中对其进行修改。
      猜你喜欢
      • 2017-01-09
      • 1970-01-01
      • 2023-01-29
      • 1970-01-01
      • 2017-06-13
      • 2018-08-31
      • 2019-05-10
      • 1970-01-01
      • 2021-02-06
      相关资源
      最近更新 更多