【问题标题】:Accessing props in vue component data function在 vue 组件数据函数中访问 props
【发布时间】:2017-03-10 15:49:21
【问题描述】:

我将 props 传递给组件:

    <template>
       {{messageId}}
       // other html code
    </template>
    
    <script>
       export default {
          props: ['messageId'],
          data: function(){
             var theData={
                // below line gives ReferenceError messageId is not defined
                somevar: messageId,
                // other object attributes
             }
          }
       }
    </script>

在上面的代码中,我已经注释了给出错误的行。如果我删除该行,它会正常工作并且模板会正确呈现(我也可以看到 {{messageId}} 的预期值)。因此传递数据的逻辑是正确的。

看来data()中访问messageId的方式不对。 那么如何访问data中的props messageId呢?

【问题讨论】:

  • this.messageId
  • 另外,你的数据函数需要返回数据对象。

标签: vue.js vue-loader


【解决方案1】:

编辑:根据 Ryans 的帖子,可以使用如下箭头函数引用实例:

data: (instance) => ({
  somevar: instance.messageId 
}),

上一篇文章:

请注意,如果您使用箭头函数来分配数据,这将不起作用:

data: () => ({
  somevar: this.messageId // undefined
}),

因为this 不会指向组件。相反,请使用普通函数:

data: function() {
  return { somevar: this.messageId }
},

或者按照 Siva Tumma 的建议使用 ES6 对象方法简写:

data() {
    return { somevar: this.messageId }
}

【讨论】:

  • 为什么不使用data() {..}
  • 我曾经被这个烫伤过。这是一个微妙的区别,但是将函数从箭头切换到 data() {return {}} 就可以了。谢谢木法沙。
  • 非常感谢,正在寻找。
  • 您实际上可以使用箭头函数——第一个参数将是对实例的引用。 data: vm =&gt; ({ somevar: vm.messageId })
  • @jvndev 是的,底部的两个是相同的,顶部是一个箭头函数,可以不同地处理上下文
【解决方案2】:

通过data() 方法,您可以使用this 引用组件的属性。

所以在你的情况下:

data: function() {
  var theData = {
    somevar: this.messageId,
    // other object attributes
  }

  return theData;
}

【讨论】:

  • 数据(){返回{打开:this.open,关闭:this.close}},
【解决方案3】:

正如@Saurabh 所述,我想添加更多细节。

如果您正在渲染一个Child 组件,并且想通过props 设置data 变量,则必须同时使用this 和watch 函数:

步骤 1。使用this 访问props 变量。

<script>
export default {

    data () {
      id: 0
    },
    
  
    props: ['propId'],
    methods: {
      init() { 
        this.id = this.propId  // step1. assign propId to id
      }
    }
}
</script>

步骤 2。观看props 变量

<script>
export default {

    data () {
      id: 0
    },
    props: ['propId'],
    methods: {
      init() { 
        this.id = this.propId  // step1. assign propId to id
      }
    },
    // add this to your code , this is a MUST. 
    // otherwise you won't SEE your data variable assigned by property
    watch { 
      propId: function(new_value) { 
        this.init()
      }
    }
}
</script>

步骤 3。了解渲染子组件的过程

假设您有两个组件:Parent 和 Child(Child 有一个名为 propId 的属性,并且Child 也有一个“异步”操作,例如读取数据库。

// Parent's View

<Child propId='parent_var_id'></Child>

3.1 Parent 被渲染

3.2 Child 被渲染,在本例中,parent_var_id 为空白

3.3 10ms 之后,parent_var_id 改为100

3.4 Child 属性 propId 也更改为绑定。

3.5 Child 的watch 函数被调用,data 中定义的变量id 发生了变化。

【讨论】:

    【解决方案4】:

    要分配一个等于props的数据属性,可以使用watcher,如下:

    <script>
       export default {
          props: ['messageId'],
          data: function(){
             var theData={
                somevar: "",
                // other object attributes
             }
          },
          watch: {
            messageId: function(newVal) { 
               this.somevar = newVal
            }
          }
       }
    

    【讨论】:

    • 或者使用computed
    • @RoyJ 是的,这也可以,但是如果你想更新数据变量,你必须使用 getter 计算。
    • 我的意思是如果somevar 不是由观察者分配的,你应该用计算的数据项替换。
    • 它使代码更清晰。计算对象实际上是一个观察者加上一个数据项。
    • 我会完全同意你that。
    【解决方案5】:
    <template>
       {{messaged}}
       // other HTML code
    </template>
    
    <script>
       export default {
          props: ['messaged'],
          data: function(){
             return () {
               some_var: this.messaged
             }
          },
          methods: {
          post_order: function () {
            console.log({
              some_var: this.some_var.id
            })
          }
        }
    
       }
    </script>
    

    【讨论】:

    • 请解释为什么您的代码是正确答案。
    • 因为我测试过了
    • Return 后面不会有括号 ()。正如你所做的return () { some_var: this.messaged }
    【解决方案6】:

    自几个月前发布该问题以来,我认为您已经完成了解决方案。我昨天遇到了同样的问题,所以没有运气就尝试了上述解决方案。但是,我想为这种情况分享一个替代解决方案,这对某人有很大帮助。 watch 有一些属性来处理这些类型的案例。下面的脚本显示了我们如何接受道具值是数据。

    <script>
       export default {
          props: {
                messageId: {
                    type: String,
                    required: true
                }
            }
          data: function(){
             var theData= {
                somevar: "",
                // other object attributes
             }
    
             return theData;
          },
          watch: {
            messageId: {
                    // Run as soon as the component loads
                    immediate: true,
                    handler() {
                        // Set the 'somevar' value as props
                        this.somevar = this.messageId;
                    }
                }
          }
       }
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-02
      • 2018-08-03
      • 2017-10-17
      • 2018-08-31
      • 2020-09-22
      • 2016-08-14
      • 2019-11-07
      • 1970-01-01
      相关资源
      最近更新 更多