vue之父组件向子组件传值

news/2024/7/10 0:51:06 标签: vue, 前端

vue_0">vue之父组件向子组件传值

vue中pros官方解释

问题描述:在做视频网站过程中发现每个视频的样式其实是大致相同的,所以就想着直接写个组件,但是又看不懂官网的传值,所以自己找了个视频看明白了。

  • 想实现的效果:
    在这里插入图片描述

具体实现代码:

  • 父组件的代码:
    <!-- 注释的部分是之前没有用组件的代码 -->
	<!-- <ul class="videoList">
					<li v-for="item in videoList" :key="item.id" class="videoItem">
						 <el-card :body-style="{ padding: '0px' }">
							<img :src="item.src" class="image">
							<div style="padding: 14px;">
								<span class="videoTitle">{{ item.title }}</span>
								<div class="bottom clearfix">
									<span class="left" style="cursor: pointer;" @click="anchorDetail">{{ item.author }}</span>
									<span class="right">{{ item.count }}人在看</span>
								</div>
							</div>
						</el-card> 
						
					</li>
				</ul> -->
				<!-- 用组件之后的代码 -->
				<Video v-bind:newlists="videoList"></Video>

父组件中要定义好videoList

import Video from '@/components/frontend/videoItem'
	export default {
		components: {
			Video
		},
		data(){
		return {
		videoList: [{
						id: 0,
						title: "最美的官方",
						author: "贝壳官方",
						count: 300,
						src: require('../../../assets/img/homepage/1.png')
					},
					{
						id: 1,
						title: "最美的官方哈哈哈啊哈哈哈哈哈哈美的官方哈哈哈啊哈哈哈哈哈哈",
						author: "贝壳官方",
						count: 300,
						src: require('../../../assets/img/homepage/1.png')
					}, {
						id: 2,
						title: "最美的官方",
						author: "贝壳官方",
						count: 300,
						src: require('../../../assets/img/homepage/1.png')
					}, {
						id: 3,
						title: "最美的官方",
						author: "贝壳官方",
						count: 300,
						src: require('../../../assets/img/homepage/1.png')
					}, {
						id: 4,
						title: "最美的官方",
						author: "贝壳官方",
						count: 300,
						src: require('../../../assets/img/homepage/1.png')
					}, {
						id: 5,
						title: "最美的官方",
						author: "贝壳官方",
						count: 300,
						src: require('../../../assets/img/homepage/1.png')
					}, {
						id: 6,
						title: "最美的官方",
						author: "贝壳官方",
						count: 300,
						src: require('../../../assets/img/homepage/1.png')
					}, {
						id: 7,
						title: "最美的官方",
						author: "贝壳官方",
						count: 300,
						src: require('../../../assets/img/homepage/1.png')
					}
				]
		}
		}
  • 子组件代码:
<template>
	<ul class="videoList">
		<li v-for="item in newlists" :key="item.id" class="videoItem">
			<el-card :body-style="{ padding: '0px' }">
				<img :src="item.src" class="image">
				<div style="padding: 14px;">
					<span class="videoTitle">{{ item.title }}</span>
					<div class="bottom clearfix">
						<span class="left" style="cursor: pointer;" @click="anchorDetail">{{ item.author }}</span>
						<span class="right">{{ item.count }}人在看</span>
					</div>
				</div>
			</el-card>
		</li>
	</ul>
</template>

<script>
	export default {
		// 父组件传过来的数据
		props: [
			"newlists"
		],
		// 自己的数据
		data() {
			return {

			}
		},
		methods: {
			anchorDetail() {
				this.$router.push('/anchor')
			}
		}
	}
</script>

<style scoped="scoped">
	/deep/.el-card.is-always-shadow,
	.el-card.is-hover-shadow:focus,
	.el-card.is-hover-shadow:hover {
		box-shadow: none;
	}

	.videoList {
		display: flex;
		flex-flow: wrap;
		justify-content: space-between;
	}

	.videoList .videoItem {
		width: 17.1875rem;
		margin-bottom: 10px;
	}

	.videoItem .image {
		width: 17.1875rem;
		height: 12.5rem;
	}

	.videoTitle {
		font-size: 14px;
		font-weight: bold;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		display: inline-block;
		width: 245px;
	}

	.bottom {
		font-size: 14px;
	}

	.bottom .left {
		float: left;
	}

	.bottom .right {
		float: right;
	}
</style>

父组件中只要定义好<Video v-bind:newlists="videoList"></Video>中的videoList,并且把子组件导入进来就ok。

子组件需要props: [ "newlists" ],并且将v-for中的list改为newlists 即可。


http://www.niftyadmin.cn/n/855374.html

相关文章

vue中父组件改变子组件的样式

vue中父组件改变子组件的样式 问题描述&#xff1a;有时候我们可以需要将某些经常运用的部分抽成组件&#xff0c;但是有很少的部分样式是不同的&#xff0c;这时候就需要更改父组件中子组件的样式。 1. 直接将style标签上的scoped属性去掉【不推荐&#xff0c;有可能影响全局样…

AS3.0基础学习笔记(1):开始第一程序

As3.0早已发布一段时间了&#xff0c;Flash cs3后加入了As3.0 可以说这些大变动是一次质的飞越&#xff0c;As2.0和As 3.0的代码风格完全不一样。在学习AS2.0的同时&#xff0c;如果和3.0一起学习&#xff0c;慢慢会遇到很多的尴尬的事情&#xff0c;但是3.0的优势也会慢慢体现…

弹性布局多列换行居左布局,flex布局下两端对齐,不满左对齐

弹性布局多列换行居左布局,flex布局下两端对齐&#xff0c;不满左对齐 问题情境&#xff1a; 在flex布局下&#xff0c;多行排列&#xff0c;如何让flex布局最后一行没有排满时&#xff0c;向左对齐排列且与上面的行间距相同。 实现效果&#xff1a; <!DOCTYPE html> &…

vue项目出现问题

vue项目出现问题 1.vueelementUi项目中 饿了么组件上不能直接添加事件&#xff0c;要加上.native <el-card :body-style"{ padding: 0px }" class"singleItem" :style"{width:newItem.itemWidthpx}" click.native"navVideoPlay(newIte…

JS小游戏_能坚持几秒

http://test.douzhao.com/test.htm 规则很简单&#xff1a;移动方块&#xff0c;不要被4个长方形碰到&#xff0c;会加速的。 我只有22.219秒&#xff0c;玩过的人把成绩贴一下吧。 xjyggd22.219 昨晚快睡觉时玩了一下&#xff0c;到24秒了&#xff0c;发现这4个长方形的移动有…

var、let、const在使用中的区别

var、let、const在使用中的区别 此文章非原创文章&#xff0c;是借鉴文章&#xff0c;转载请注意&#xff01;&#xff01;&#xff01; <!DOCTYPE html> <html><head><meta charset"utf-8"><title></title></head><bo…

组件之间传值(父子组件、非父子组件)

组件之间传值&#xff08;父子组件、非父子组件&#xff09; 一、父子组件之间传值 1.父向子传值 &#xff08;1&#xff09;.父组件 父组件里需引用子组件&#xff0c;并传值给子组件&#xff0c;代码如下&#xff1a; <template><div><span>哈哈哈哈<…

Vue实时刷新数组

Vue实时刷新数组 1.第一种&#xff0c;直接拼接数组 <template><div style"border: 1px solid #ccc; width: 500px;height:200px; margin: auto;"><div><button click"addItem" style"float: right;" >添加新的项<…