首页 > 其他分享 >Vue2 props

Vue2 props

时间:2022-10-03 13:22:38浏览次数:47  
标签:myAge name age props sex Vue2 TestProps

props

概述

我们的组件进行展示数据时,里面并不是都是写死的。我们需要在使用组件时,向组件内部传值,并展现我们需要的值。

数组形式接收 props

TestProps.vue

<template>
	<div>
		<h2>name: {{name}}</h2>
		<h2>sex: {{sex}}</h2>
		<h2>age: {{myAge}}</h2>
		<button @click="myAge++">myAge++</button>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				myAge: this.age
			}
		},
		props: ['name', 'age', 'sex']
	}
</script>

App.vue

<template>
	<div id="app">
		<testProps name="brokyz" sex="male" :age="21"></testProps>
	</div>
</template>

<script>
	import TestProps from './components/TestProps.vue'
	export default {
		name: 'App',
		components: {
			TestProps
		}
	}
</script>

注意:

  1. 使用 props 传入值时,默认传入都时字符串。如果需要传入数字型,则需要在传入属性前加冒号。如:age="21"传入的时数字 21,age="21"传入的是字符串 21。
  2. 在传入 props 后,如果组件需要对传入值进行修改操作,需要在 data 函数中定义,否则报错。在创建的时候是先准备好 props 之后才配置数据 data。

对象形式接收 props

TestProps.vue

<template>
	<div>
		<h2>name: {{name}}</h2>
		<h2>sex: {{sex}}</h2>
		<h2>age: {{myAge}}</h2>
		<button @click="myAge++">myAge++</button>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				myAge: this.age
			}
		},
		props: {
			name: String,
			age: {
				type: Number,
				required: true
			},
			sex: {
				type: String,
				default: "none"
			}
		}
	}
</script>

App.vue

<template>
	<div id="app">
		<testProps name="brokyz" :age="21"></testProps>
	</div>
</template>

<script>
	import TestProps from './components/TestProps.vue'
	export default {
		name: 'App',
		components: {
			TestProps
		}
	}
</script>
  • 对象的形式来声明 props 可以让 props 的接收更加具体,可以指定类型,默认值,和是否必传。
  • 一般来说 default 和 required 不同时出现,因为不传才会有默认值,而必传又不需要默认值。

标签:myAge,name,age,props,sex,Vue2,TestProps
From: https://www.cnblogs.com/brokyz/p/16750373.html

相关文章

  • 面试官:vue2和vue3的区别有哪些?
    一、Vue3与Vue2区别详述1.生命周期对于生命周期来说,整体上变化不大,只是大部分生命周期钩子名称上+“on”,功能上是类似的。不过有一点需要注意,Vue3在组合式API(Compo......
  • 【尚硅谷】Vue2.x组件化编码学习笔记--渐进式的JS框架
    Vue组件化编码​​一、使用Vue-cli创建项目​​​​1.1说明​​​​1.2创建Vue项目​​​​1.2.1如何修改端口以及自动运行​​​​1.3Vue-cli创建的项目的目录结构​​......
  • VUE2速成-5(插件及打包)
    文章目录​​一、Vue的插件大全​​​​二、Vue插件举例​​​​1.轮播图插件(vue-awesome-swiper)​​​​2.UI组件库(ElementUI)​​​​三、vue-cli打包部署​​一、Vue的......
  • VUE2速成-3(路由进一步及Ajax请求)
    文章目录​​一、路由进一步​​​​1.命名路由​​​​2.重定向和别名​​​​3.HTML5History模式​​​​4.导航守卫​​​​4.1全局前置守卫​​​​4.2全局后置......
  • props写法
    在eslint中写props,直接简写会有语法错误提示,要想去掉,可以加忽略检查的注释,但最好还是改为eslint认可的规范写法加注释:在上一行加上//eslint-disable-next-line,或者在......
  • vue2转vite判断当前运行环境
    vue2转vite判断当前运行环境vue2判断当前运行环境使用的是process.env.NODE_ENV==='development',而当转为vite项目时,process会报错,这里环境的判断应该改为import.meta.......
  • Vue2,Vue3,React,vue-cli和Vite创建和启动
    vue2-project创建项目vuecreatexxx名字下依赖包npminstall启动项目npmrunserve项目打包npmrunbuildvue3-project创建项目vuecreatexxx名字下依......
  • vue2/vue3+eslint文件格式化
    vue+javascript1.设置vscode保存时格式化文件2.打开settings.json3.设置settings.json文件{"editor.codeActionsOnSave":{"source.fixAll.eslint":t......
  • Vue2 中的键盘事件
    Vue2中的键盘事件@keydown:按下时触发@keyup:松手时触发,事件触发之后会有event对象,event.keyCode是该按键的编码@keyup.enter:回车按键类似的还有:delete,esc,space,......
  • vue2.x引入threejs
    @目录vue2.x引入threejsnpm安装使用指定版本:其他插件实例强调vue2.x引入threejsnpm安装npminstallthree使用指定版本:npminstallthree@<版本号>其他插件因为本......