vue.js - 从 vue.js json 父组件继承子组件
问题描述
父类.Vue
msg:"",
parentData: {msg:[]},
methods: {
response(file, respone) {
this.msg = respone
console.log(respone)
},
}
Child.Vue
<template>
<div class="result">
<p>{{parentData.msg}}</p>
</div>
</template>
我想继承从父组件接收的json数据作为子组件。
json数据格式上传为照片。
解决方案
使用props将数据从父组件传递到子组件。
例如
Vue.component('Child', {
props: ['parentData'],
template: `<div>
<h2>Child</h2>
<pre>parentData = {{ parentData }}</pre>
</div>`
})
new Vue({
el: '#app',
data: () => ({ msg: {} }),
methods: {
response (file, response) {
this.msg = response
// console.log(response)
}
},
mounted () {
// simulate loading data
setTimeout(() => this.response(null, {
file_name: 'result.jpg',
font_color: 'red',
info_text: 'Clean'
}), 2000)
}
})
h1, h2 { margin: 0 }
div {
padding: 1em;
border: 1px solid #666;
}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
<div id="app">
<h1>Parent</h1>
<pre>msg = {{ msg }}</pre>
<!-- pass msg to child via the parent-data prop -->
<child :parent-data="msg"/>
</div>
请参阅https://vuejs.org/v2/guide/components.html#Passing-Data-to-Child-Components-with-Props
推荐阅读
- python - 计算dask数据帧中所有行差异的有效方法
- tsql - 不明白 'CREATE VIEW' 必须是查询批处理中的第一条语句
- android - 如何查找从画布创建的位图是否为空
- javascript - 如何在 React 中正确处理 cookie
- c++ - 指向 C++ 中成员的指针如何在内部工作?
- powershell - Get-Item Query 在位置结束时引发解析错误
- api - Youtube /v3/search API 不再返回直播视频
- python - Pip 未安装在 venv 中
- python-3.x - Google Cloud Platform Authentification:尽管使用服务帐户,但仍被识别为最终用户身份验证
- python - 就地监控现有管道