gpt4 book ai didi

javascript - vue-cli 中的 react 性属性错误

转载 作者:行者123 更新时间:2023-11-30 20:52:29 25 4
gpt4 key购买 nike

你好,

我在使用 vue-cli 时遇到了一些问题。我尝试显示(在主组件中)输入的文本(在子组件中)。它的工作(很奇怪)但有一条错误消息:

vue.esm.js?efeb:578 [Vue warn]: Property or method "test" is not 
defined on the instance but referenced during render. Make sure that
this property is reactive, either in the data option, or for class-
based components, by initializing the property. See:
https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-
Properties.

found in

---> <Signup> at src/components/auth/Signup.vue
<App> at src/App.vue
<Root>

我在互联网上搜索,有很多解决此错误的示例,但不是使用架构 vue-cli。我不明白...

一步一步:

我写了一个组件:

<template>
<div class="container">
<p>{{ data.test }}</p>
<form @submit.prevent="signup">
<v-customInput v-model="test" @onChangeValue="onChange"></v-customInput>
</form>
</div>
</template>

<script>
import CustomInput from '../shared/CustomInput'

export default {
name: 'HelloWorld',
components: {
'v-customInput': CustomInput,
},
data() {
return {
data: {
test: '',
first_name: '',
last_name: '',
email: '',
password: '',
vat: 0,
creation_company_date: new Date(),
phone: '',
},
error: false,
};
},
methods: {
onChange(variable) {
const data = this.data;
for (let value in data) {
if (value === 'test') {
data[value] = variable;
}
}
}
},
};
</script>

和一个子组件:

<template>
<div class="customInput">
<input v-model="value" type="text">
<label>First Name</label>

<script>
export default {
name: 'CustomInput',
data() {
return {
value: '',
};
},
watch: {
value: function(val, oldVal) {
this.$emit('onChangeValue', this.value);
}
},
};
</script>

最佳答案

在 vue-2.x 中,如果您使用 v-model 绑定(bind)一个属性并且该属性不存在(在这种情况下为 test),那么您将收到错误。

试试这个:添加 test 属性。

<template>
<div class="container">
<p>{{ data.test }}</p>
<form @submit.prevent="signup">
<v-customInput v-model="test" @onChangeValue="onChange"></v-customInput>
</form>
</div>
</template>

<script>
import CustomInput from '../shared/CustomInput'

export default {
name: 'HelloWorld',
components: {
'v-customInput': CustomInput,
},
data() {
return {
test: '', // <===== initialize test with a default value
data: {
test: '',
first_name: '',
last_name: '',
email: '',
password: '',
vat: 0,
creation_company_date: new Date(),
phone: '',
},
error: false,
};
},
methods: {
onChange(variable) {
const data = this.data;
for (let value in data) {
if (value === 'test') {
data[value] = variable;
}
}
}
},
};
</script>

关于javascript - vue-cli 中的 react 性属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48022934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com