在构造函数vuejs中访问道具

时间:2018-09-19 15:05:46

标签: vue.js constructor

我正在寻找VueJS中的构造函数访问道具。

我尝试了如下所示的反应方式,但当时没有任何成功。

父母:

<nb-add-card
 test="test props">
</nb-add-card>

孩子:

<template>
 <div>
  test : {{ test }}
 </div>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';

@Component({
 components: { },
 props: {
  test: {
   type: String,
   default: "",
   required: true
  },
 },
})
export default class AddCard extends Vue {
 constructor(props: any) {
  super(props);
  console.log('TEST :', props); // => Undefined
  console.log('TEST1 :', this); // => Refer to AddCard
 }
}
</script>

日志“ TEST”仍然“未定义”。 现在被困了几个小时,在反应时,我们只是在构造函数args中传递“ props”,但是在vuejs中,这似乎不起作用...:/

我还测试了通过输入道具名称 来访问道具值,但是它也不起作用...

 constructor(test: any) {
  super(test);
  console.log('TEST :', test); // => Undefined
  console.log('TEST1 :', this); // => Refer to AddCard
 }

有人知道吗?谢谢

1 个答案:

答案 0 :(得分:0)

构造函数与vue-class-component无关。您要导出的是构造函数本身。您的“测试”变量可立即在该组件内使用。使用“创建的”或“安装的”钩子进行记录。

相关问题