admin管理员组文章数量:1026900
My view is like this :
<div class="col-md-8">
...
<star :value="{{ $data['rating'] }}" :user="{{ $data['user_id']></star>
...
</div>
My star ponent is like this :
<template>
<span class="rating">
<template v-for="item in items">
<label class="radio-inline input-star" :class="{'is-selected': ((value >= item.value) && value != null), 'is-disabled': disabled}">
<input type="radio" class="input-rating" name="input-rating" v-bind:value="item.value" v-model="value" :disabled="disabled" @click="rate(item.value)">
</label>
</template>
</span>
</template>
<script>
export default{
props: [{'value': null,'disabled': Boolean}, 'user'],
data(){
return{
items: [
{value: 5},
{value: 4},
{value: 3},
{value: 2},
{value: 1}
],
temp_value: null,
}
},
methods:{
rate: function (star) {
var self = this;
if (!this.disabled) {
this.$http.post(window.BaseUrl + '/star', {star: star}).then(function (response) {
console.log('submitted');
});
this.temp_value = star;
return this.value = star;
}
},
}
}
</script>
My css is like this :
span.rating {
direction: rtl;
display: inline-block;
}
span.rating .input-star {
background: url("../img/star.png") 0 -16px;
padding-left: 0;
margin-left: 0;
width: 16px;
height: 16px;
}
span.rating .input-star:hover, span.rating .input-star:hover ~ .input-star {
background-position: 0 0;
}
span.rating .is-selected{
background-position: 0 0;
}
span.rating .is-disabled{
cursor: default;
}
span.rating .input-star .input-rating {
display: none;
}
When I click the star, there exist error on the console like this :
[Vue warn]: props must be strings when using array syntax.
[Vue warn]: Property or method "star" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in at C:\xampp\htdocs\myshop\resources\assets\js\ponents\Star.vue)
[Vue warn]: Property or method "disabled" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in at C:\xampp\htdocs\myshop\resources\assets\js\ponents\Star.vue)
How can I solve it?
My view is like this :
<div class="col-md-8">
...
<star :value="{{ $data['rating'] }}" :user="{{ $data['user_id']></star>
...
</div>
My star ponent is like this :
<template>
<span class="rating">
<template v-for="item in items">
<label class="radio-inline input-star" :class="{'is-selected': ((value >= item.value) && value != null), 'is-disabled': disabled}">
<input type="radio" class="input-rating" name="input-rating" v-bind:value="item.value" v-model="value" :disabled="disabled" @click="rate(item.value)">
</label>
</template>
</span>
</template>
<script>
export default{
props: [{'value': null,'disabled': Boolean}, 'user'],
data(){
return{
items: [
{value: 5},
{value: 4},
{value: 3},
{value: 2},
{value: 1}
],
temp_value: null,
}
},
methods:{
rate: function (star) {
var self = this;
if (!this.disabled) {
this.$http.post(window.BaseUrl + '/star', {star: star}).then(function (response) {
console.log('submitted');
});
this.temp_value = star;
return this.value = star;
}
},
}
}
</script>
My css is like this :
span.rating {
direction: rtl;
display: inline-block;
}
span.rating .input-star {
background: url("../img/star.png") 0 -16px;
padding-left: 0;
margin-left: 0;
width: 16px;
height: 16px;
}
span.rating .input-star:hover, span.rating .input-star:hover ~ .input-star {
background-position: 0 0;
}
span.rating .is-selected{
background-position: 0 0;
}
span.rating .is-disabled{
cursor: default;
}
span.rating .input-star .input-rating {
display: none;
}
When I click the star, there exist error on the console like this :
[Vue warn]: props must be strings when using array syntax.
[Vue warn]: Property or method "star" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in at C:\xampp\htdocs\myshop\resources\assets\js\ponents\Star.vue)
[Vue warn]: Property or method "disabled" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in at C:\xampp\htdocs\myshop\resources\assets\js\ponents\Star.vue)
How can I solve it?
Share Improve this question asked Mar 1, 2017 at 5:35 samuel tohsamuel toh 7,08624 gold badges76 silver badges110 bronze badges 1-
props
should be an object when you need to validate. And don't use{{ }}
on prop bindings. – Mat J Commented Mar 1, 2017 at 6:39
1 Answer
Reset to default 4There are several errors in your code.
First, you don't need the braces in props:
<star :value="{{ $data['rating'] }}" :user="{{ $data['user_id'] }}"></star>
<!-- ^^ syntax error -->
just:
<star :value="$data['rating']" :user="$data['user_id']"></star>
and this can be simplified to:
<star :value="rating" :user="user_id"></star>
Another error is the declaration of props. That's the reason why Vue told you it can't recognize disabled
and value
.
The simplest way is props: ['value', 'disabled', 'user'],
If you want to add validations, follow the documentation here: https://v2.vuejs/v2/guide/ponents.html#Prop-Validation
Another problem is that you are mutating props directly.
<input ... v-model="value" ...>
The value
is a prop. The flow of props is one way down. A ponent should not mutate its props.
If you want to send the value back to parent, use events. Here is the documentation of events: https://v2.vuejs/v2/guide/ponents.html#Custom-Events
or see my previous answer: Update parent model from child ponent Vue
My view is like this :
<div class="col-md-8">
...
<star :value="{{ $data['rating'] }}" :user="{{ $data['user_id']></star>
...
</div>
My star ponent is like this :
<template>
<span class="rating">
<template v-for="item in items">
<label class="radio-inline input-star" :class="{'is-selected': ((value >= item.value) && value != null), 'is-disabled': disabled}">
<input type="radio" class="input-rating" name="input-rating" v-bind:value="item.value" v-model="value" :disabled="disabled" @click="rate(item.value)">
</label>
</template>
</span>
</template>
<script>
export default{
props: [{'value': null,'disabled': Boolean}, 'user'],
data(){
return{
items: [
{value: 5},
{value: 4},
{value: 3},
{value: 2},
{value: 1}
],
temp_value: null,
}
},
methods:{
rate: function (star) {
var self = this;
if (!this.disabled) {
this.$http.post(window.BaseUrl + '/star', {star: star}).then(function (response) {
console.log('submitted');
});
this.temp_value = star;
return this.value = star;
}
},
}
}
</script>
My css is like this :
span.rating {
direction: rtl;
display: inline-block;
}
span.rating .input-star {
background: url("../img/star.png") 0 -16px;
padding-left: 0;
margin-left: 0;
width: 16px;
height: 16px;
}
span.rating .input-star:hover, span.rating .input-star:hover ~ .input-star {
background-position: 0 0;
}
span.rating .is-selected{
background-position: 0 0;
}
span.rating .is-disabled{
cursor: default;
}
span.rating .input-star .input-rating {
display: none;
}
When I click the star, there exist error on the console like this :
[Vue warn]: props must be strings when using array syntax.
[Vue warn]: Property or method "star" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in at C:\xampp\htdocs\myshop\resources\assets\js\ponents\Star.vue)
[Vue warn]: Property or method "disabled" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in at C:\xampp\htdocs\myshop\resources\assets\js\ponents\Star.vue)
How can I solve it?
My view is like this :
<div class="col-md-8">
...
<star :value="{{ $data['rating'] }}" :user="{{ $data['user_id']></star>
...
</div>
My star ponent is like this :
<template>
<span class="rating">
<template v-for="item in items">
<label class="radio-inline input-star" :class="{'is-selected': ((value >= item.value) && value != null), 'is-disabled': disabled}">
<input type="radio" class="input-rating" name="input-rating" v-bind:value="item.value" v-model="value" :disabled="disabled" @click="rate(item.value)">
</label>
</template>
</span>
</template>
<script>
export default{
props: [{'value': null,'disabled': Boolean}, 'user'],
data(){
return{
items: [
{value: 5},
{value: 4},
{value: 3},
{value: 2},
{value: 1}
],
temp_value: null,
}
},
methods:{
rate: function (star) {
var self = this;
if (!this.disabled) {
this.$http.post(window.BaseUrl + '/star', {star: star}).then(function (response) {
console.log('submitted');
});
this.temp_value = star;
return this.value = star;
}
},
}
}
</script>
My css is like this :
span.rating {
direction: rtl;
display: inline-block;
}
span.rating .input-star {
background: url("../img/star.png") 0 -16px;
padding-left: 0;
margin-left: 0;
width: 16px;
height: 16px;
}
span.rating .input-star:hover, span.rating .input-star:hover ~ .input-star {
background-position: 0 0;
}
span.rating .is-selected{
background-position: 0 0;
}
span.rating .is-disabled{
cursor: default;
}
span.rating .input-star .input-rating {
display: none;
}
When I click the star, there exist error on the console like this :
[Vue warn]: props must be strings when using array syntax.
[Vue warn]: Property or method "star" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in at C:\xampp\htdocs\myshop\resources\assets\js\ponents\Star.vue)
[Vue warn]: Property or method "disabled" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in at C:\xampp\htdocs\myshop\resources\assets\js\ponents\Star.vue)
How can I solve it?
Share Improve this question asked Mar 1, 2017 at 5:35 samuel tohsamuel toh 7,08624 gold badges76 silver badges110 bronze badges 1-
props
should be an object when you need to validate. And don't use{{ }}
on prop bindings. – Mat J Commented Mar 1, 2017 at 6:39
1 Answer
Reset to default 4There are several errors in your code.
First, you don't need the braces in props:
<star :value="{{ $data['rating'] }}" :user="{{ $data['user_id'] }}"></star>
<!-- ^^ syntax error -->
just:
<star :value="$data['rating']" :user="$data['user_id']"></star>
and this can be simplified to:
<star :value="rating" :user="user_id"></star>
Another error is the declaration of props. That's the reason why Vue told you it can't recognize disabled
and value
.
The simplest way is props: ['value', 'disabled', 'user'],
If you want to add validations, follow the documentation here: https://v2.vuejs/v2/guide/ponents.html#Prop-Validation
Another problem is that you are mutating props directly.
<input ... v-model="value" ...>
The value
is a prop. The flow of props is one way down. A ponent should not mutate its props.
If you want to send the value back to parent, use events. Here is the documentation of events: https://v2.vuejs/v2/guide/ponents.html#Custom-Events
or see my previous answer: Update parent model from child ponent Vue
本文标签: javascriptHow to solve Vue warn props must be strings when using array syntaxStack Overflow
版权声明:本文标题:javascript - How to solve [Vue warn]: props must be strings when using array syntax? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745655389a2161569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论