我无法将数据从父级传递给子级.我正在使用道具,也试过返回数据 - 没有运气.我有一个面板组件(它是父组件)与数据和panelBody组件(子)
小组如下:
<template>
<div id="panel">
<div class="panel">
<ul>
<li v-for="shelf in shelfs">
<panel-body :shelf="shelf" :selected.sync="selected"></panel-body>
</li>
</ul>
</div>
</div>
</template>
<script>
import PanelBody from '../components/PanelBody'
export default {
name: 'panel-body',
components: {
'panel-body': PanelBody
},
data: () => ({
shelfs: [{
name: 'shelf 1',
books: [{
title: 'Lorem ipum'
}, {
title: 'Dolor sit amet'
}]
}, {
name: 'shelf 2',
books: [{
title: 'Ipsum lorem'
}, {
title: 'Amet sit dolor'
}]
}],
selected: {}
})
}
</script>
<style scoped>
a {
color: #42b983;
}
</style>
Run Code Online (Sandbox Code Playgroud)
我的面板是:
<template>
<div id="panel-body">
<a href="#" v-on:click.prevent.stop="select">{{ shelf.name }}</a>
<ul v-show="isSelected">
<li v-for="book in shelf.books">{{ book.title }}</li>
</ul>
</div>
</template>
<script>
export default {
name: 'panel-body',
props: ['shelf', 'selected'],
computed: {
isSelected: function () {
return this.selected === this.shelf
}
},
methods: {
select: function () {
this.selected = this.shelf
}
}
}
</script>
<style scoped>
a {
color: #42b983;
}
</style>
Run Code Online (Sandbox Code Playgroud)
请帮忙!无法找出错误"vue.esm.js?65d7:3877 Uncaught RangeError:超出最大调用堆栈大小".当我删除数据时,一切都像它应该的那样.
Ber*_*ert 26
你有错误的原因
超出最大调用堆栈大小
是因为这个
import PanelBody from '../components/PanelBody'
export default {
name: 'panel-body',
components: {
'panel-body': PanelBody
},
Run Code Online (Sandbox Code Playgroud)
您定义了Panel组件name: 'panel-body'.将其更改为name: 'panel',您将删除循环引用.
评论中提到的其他问题和其他答案通常也适用.以下是组件的工作版本.
Panel.vue
<template>
<div id="panel">
<div class="panel">
<ul>
<li v-for="shelf in shelfs">
<panel-body :shelf="shelf" :key="shelf.name" :selected.sync="selected"></panel-body>
</li>
</ul>
</div>
{{selected}}
</div>
</template>
<script>
import PanelBody from './PanelBody.vue'
export default {
name: 'panel',
components: {
'panel-body': PanelBody
},
data(){
return {
shelfs: [{
name: 'shelf 1',
books: [{
title: 'Lorem ipum'
}, {
title: 'Dolor sit amet'
}]
}, {
name: 'shelf 2',
books: [{
title: 'Ipsum lorem'
}, {
title: 'Amet sit dolor'
}]
}],
selected: {}
}
}
}
</script>
<style scoped>
a {
color: #42b983;
}
</style>
Run Code Online (Sandbox Code Playgroud)
PanelBody.vue
<template>
<div id="panel-body">
<a href="#" v-on:click.prevent.stop="select">{{ shelf.name }}</a>
<ul v-show="isSelected">
<li v-for="book in shelf.books">{{ book.title }}</li>
</ul>
</div>
</template>
<script>
export default {
name: 'panel-body',
props: ['shelf', 'selected'],
data(){
return {
internalSelected: null
}
},
computed: {
isSelected: function () {
return this.internalSelected === this.shelf
}
},
methods: {
select: function () {
this.internalSelected = this.shelf
this.$emit("update:selected", this.internalSelected)
}
}
}
</script>
<style scoped>
a {
color: #42b983;
}
</style>
Run Code Online (Sandbox Code Playgroud)
我想再注意一件事.由于PanelBody中的这一行,Vue会发出一个警告,指出你正在直接改变道具.通常,您应该存储要变异的属性的本地副本.我已经更新了上面的代码来做到这一点.this.selected = this.shelf
您所在的 Vue 的名称不应与您要导入的组件的名称相同。
就我而言
<template>
<div>
<SingleStandard></SingleStandard>
</div>
</template>
<script>
import SingleStandard from '../components/SingleStandard';
export default {
name: 'SingleStandard',
components: { SingleStandard },
};
</script>
Run Code Online (Sandbox Code Playgroud)
上面的代码导致了同样的问题,但是当我更改导出组件的名称时它起作用了。
<template>
<div>
<SingleStandard></SingleStandard>
</div>
</template>
<script>
import SingleStandard from '../components/SingleStandard';
export default {
name: 'SingleStandardView', <-------------
components: { SingleStandard },
};
</script>
Run Code Online (Sandbox Code Playgroud)
请检查所有未来人的命名约定:)
| 归档时间: |
|
| 查看次数: |
34554 次 |
| 最近记录: |