gpt4 book ai didi

javascript - [Vue 警告] : Invalid prop: type check failed for prop "X". 预期,得到字符串

转载 作者:数据小太阳 更新时间:2023-10-29 04:52:04 24 4
gpt4 key购买 nike

给定这个 Vue 2 组件:

  Vue.component('read-more', {
props: {
'text': String,
'clamp': {
type: String,
default: 'Read More'
},
'less': {
type: String,
default: 'Read Less'
},
'length': {
type: Number,
default: 100
}
},
template: `
<p>
<span v-if="!show">{{truncate(text)}} <a href="javascript:;" v-if="text.length >= length" @click="toggle()">{{clamp}}</a></span>
<span v-if="show">{{text}} <a href="javascript:;" @click="toggle()" v-if="text.length >= length">{{less}}</a></span>
</p>
`,
methods: {
truncate(string) {
if (string) {
return string.toString().substring(0, this.length);
}

return '';
},
toggle() {
this.show = !this.show;
},
},
data() {
return {
show: false,
counter: this.length,
};
},
});

用法(HAML):

%read-more{v: {bind: '{text: some_object.property}' }}

一切正常,但我收到所有已声明 Prop 的 Vue 警告:

[Vue warn]: Invalid prop: type check failed for prop "text". Expected , got String.

[Vue warn]: Invalid prop: type check failed for prop "clamp". Expected , got String.

[Vue warn]: Invalid prop: type check failed for prop "less". Expected , got String.

[Vue warn]: Invalid prop: type check failed for prop "length". Expected , got Number.

我做错了什么?

编辑

我创建了一个工作正常的 fiddle :http://jsfiddle.net/oLt9wkxe/8/

不过在我的应用中,这个组件嵌套在其他几个组件中。它工作得很好,但显示这些烦人的警告!

最佳答案

此属性将发送一个字符串

myBoolValue=false

此属性将发送一个 bool 值

:myBoolValue="false"

这是强制类型并引发异常

props: { myBoolValue: Boolean }

不会报错但是你会得到一个字符串而不是 bool 值

props: [ "myBoolValue" ]

我所有的代码都是推荐的例子:

const MyTable = Vue.component("my-table", {
props: {
myBoolValue: Boolean // Force type
myStringValue: String,
myObject: Object
},

<my-table
ref="otbExpertTable_user"
:myBoolValue="false"
v-bind:is-mobile="isMobile"
v-on:hover-item="hoverItem"
:myObject=metaUsers
/>

关于javascript - [Vue 警告] : Invalid prop: type check failed for prop "X". 预期,得到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45023231/

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