javascript - Vue JS:在组件的组件内传递道具
问题描述
我有这个简单的脚本,里面有来自其他组件的道具,当我安慰它时它工作正常。但是我怎样才能在我的line-chart
组件下传递道具呢?
export default {
props: ['dataset'],
components:{
'line-chart': {
extends: Bar,
beforeMount () {
try{
this.addPlugin(horizonalLinePlugin)
//console.log(this.$props);
console.log($this.$props.dataset); <- How can show it here?
}catch(err){
}
},
mounted () {
this.renderChart(chartOption, chartSettings
)
}
}
},
created(){
console.log(this.$props) <- Working fine
},
mounted(){
}
}
解决方案
您不能props
直接从子组件访问父组件;您需要在子组件中声明 prop,然后从父组件向其传递数据。
export default {
props: ['dataset'],
components:{
'line-chart': {
extends: Bar,
props: ['dataset'], // declare the prop
beforeMount () {
try {
this.addPlugin(horizonalLinePlugin)
console.log(this.dataset); // access with this.dataset
} catch(err) {
}
},
mounted () {
this.renderChart(chartOption, chartSettings)
}
}
}
然后在您的模板中,将dataset
父组件传递给子组件:
<line-chart :dataset="dataset"></line-chart>
推荐阅读
- javascript - 通过加载 geoJSON 更改标记样式
- angular - Firestore 允许在一个集合中创建两个具有相同 ID 的单独文档
- pandas - 根据 df.A 和 df.B 中的比较值填充 df.A 中的列
- html - 当我将文件托管到我的网站时,媒体查询将不起作用
- modx-revolution - 使用 getImageList 片段将电视值传递给占位符
- dictionary - Haskell Data.Map 查找并同时删除
- mysql - 我已将 MySql 数据库与 SQL Developer 连接,但无法更新表或存储过程
- javascript - 是否可以限制 keyup 的最大日期?
- android - iOS 和 Android 应用旧版本 - 禁止使用
- char - 如何通过公式计算 Apache OpenOffice Calc 中单元格大写字母的长度?