您现在的位置是:首页 > Javascript
当前栏目
Vue 父子组件通信传值(子组件中使用父组件中的数据)
2023-03-07 09:42:18 时间
1. 父传子 props
父组件中的数据传递给子组件
props: ['movies']
props: {
movies: Array
},
props: {
movies: {
type: Array,
default: [],
required: true
}
},
props 的驼峰标识
<cpn :c-info="userinfo"></cpn>
props: {
cInfo: {
type: Object,
default: { name: 'liang' }
}
}
2. 使用示例
<div id="app">
<parent :article="art"></parent>
</div>
<script>
var child = {
template: `<p>{{ author }}</p>`,
props: ['author']
}
var par = {
template: `<div>
{{ article.title }}
<child :author="article.author"></child>
</div>
`,
props: ['article'],
components: {
child: child
}
}
let vm = new Vue({
el: '#app',
data: {
art: {
title: 'liang',
content: 'itqaq.com',
author: '辰风沐阳'
}
},
components: {
parent: par
}
})
</script>
3. 实战文章列表
<div id="app">
<art-list :article="arts"></art-list>
</div>
<script>
var artis = {
template: `
<ul>
<li v-for="art in article">
<h1>{{ art.title }}</h1>
<img :src="art.img" style="width:200px;">
<p>{{ art.content }}</p>
</li>
</ul>
`,props: ['article']
}
let vm = new Vue({
el: '#app',
data: {
arts: [
{
title: '01',
img: 'img/01vue.jpg',
content: '01content',
},
{
title: '02',
img: 'img/01vue.jpg',
content: '02content'
},
]
},
components: {
'art-list': artis
}
})
</script>
相关文章
- 鲜为人知但很有用的 HTML 属性
- 翻转再翻转!有意思的水平横向溢出滚动
- 自定义计数器小技巧!CSS 实现长按点赞累加动画
- 过五关!React高频面试题指南
- 软件开发中的十个认知偏差
- 不需要 JS!仅用 CSS 也能达到监听页面滚动的效果!
- 一文读懂TypeScript类型兼容性
- Vue 的响应式原则与双向数据绑定
- 快速掌握 TypeScript 新语法:Infer Extends
- JWT教你如何证明你是我的人!
- 一篇带给你 V8 GC 的实现
- 面试官:请使用JS完成一个LRU缓存?
- 通过可视化来学习JavaScript事件循环
- 新的跨域策略:使用 COOP、COEP 为浏览器创建更安全的环境
- 为什么有人说 vite 快,有人却说 vite 慢?
- 种草 Vue3 中几个好玩的插件和配置
- 超全面的前端工程化配置指南
- Vue 状态管理未来样子
- Volatile关键字能保证原子性么?
- 面试突击:SpringBoot 有几种读取配置文件的方法?