我寫了一個基本的vue js 2基本示例來測試嵌套組件。屬性或方法「value」未在vue js 2實例中定義嵌套組件
以下是組件和模板結構。
Vue.component('form-com', {
template: '#form',
props: ['value'],
methods: {
onInput: function (event) {
this.$emit('input', event.target.value);
}
}
});
Vue.component('message-com', {
template: '#message',
data: function() {
return {
msg: 'Hello'
}
},
props: ['user']
});
Vue.component('welcome-com', {
template: '#welcome',
data: function() {
return {
user: 'ahmad'
}
}
});
new Vue({
el: '#container'
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.js"></script>
<!--Form Template-->
<template id="form">
<div>
<div class="form-control">
<label>Enter Your Name:</label>
<input type="text" v-bind:value="value" :input="onInput">
</div>
</div>
</template>
<!--Hello Message Template-->
<template id="message">
<div>
<h3>{{msg}} {{user}}</h3>
</div>
</template>
<template id="welcome">
<div>
<form-com :value="value"></form-com>
<br>
<message-com :user="user"></message-com>
</div>
</template>
<div id="container">
<welcome-com></welcome-com>
</div>
但運行瀏覽應用程序時,會顯示此錯誤:
[Vue warn]: Property or method "value" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.
found in
---> <WelcomeCom>
<Root>
是什麼問題呢?
更新:
我剛剛從Learning Vue.js 2篇章之一重寫this Fiddle。我只是重命名一些參數和組件和模板名稱。但是當我將主要的小提琴複製到我的代碼時,所有的東西都奏效了。
問題已解決,並刪除了錯誤。但現在當輸入{msg}輸入一些字符不會改變時,這個問題是什麼? –
你從來沒有宣佈你的'味精'應該改變。也許你正試圖完成另一個問題。 –
我錯誤了prev評論。我的意思是{用戶}參數。我想通過輸入文本來改變'user' –