特别函数:
1、设置组组件内所有特别,形参
2、组件调用的时候,传入所谓的实参?
<!-- 3.所谓调用函数:传实参 -->
<cpt-4 title="超级好吃的口水鸡" price="50" num="8"></cpt-4>
<cpt-4 title="泰国榴莲" price="288" num="6"></cpt-4>
props:["title","price","num"]//1. 设置形参,语法固定
components/04-cpt.vue
子组件文件,子组件中, 通过props属性接收。<template>
<div class="box">
<h3>标题:{{ title }}h3>
<p>价格:{{price}}元p>
<p>开业大酬宾,全场{{num}}折p>
div>
template>
<script>
export default {
// 特别封装:如何设置所谓的形参
props:["title","price","num"]//1. 设置形参,语法固定
}
script>
<style scoped>
.box{
border:1px solid black;
}
style>
App.vue
父组件里面调用cpt4子组件,父组件通过给子组件加属性传值。<template>
<div>
<h1>超市商品列表:h1>
<cpt-4 title="超级好吃的口水鸡" price="50" num="8">cpt-4>
<cpt-4 title="泰国榴莲" price="288" num="6">cpt-4>
div>
template>
<script>
import cpt4 from "./components/04-cpt.vue"
export default {
components:{
cpt4,
}
}
script>
<template>
<div>
<h1>超市商品列表:h1>
<cpt-4 :title="title" :price="price" :num="num">cpt-4>
div>
template>
<script>
import cpt4 from "./components/04-cpt.vue"
export default {
components:{
cpt4,
},
data(){
return{
title:"火鸡面",
price:10,
num:5
}
}
}
script>
list:[
{id:1,title:"good-1",price:10,num:8},
{id:2,title:"good-2",price:88,num:8},
{id:3,title:"good-3",price:99,num:8},
]
<template>
<div>
<h1>超市商品列表:h1>
<cpt-4 v-for="item in list" :key="item.id" :title="item.title" :price="item.price" :num="item.num">cpt-4>
div>
template>
<script>
import cpt4 from "./components/04-cpt.vue"
export default {
components:{
cpt4,
},
data(){
return{
title:"火鸡面",
price:10,
num:5,
list:[
{id:1,title:"good-1",price:10,num:8},
{id:2,title:"good-2",price:88,num:8},
{id:3,title:"good-3",price:99,num:8},
]
}
}
}
script>
在vue中需要遵循单向数据流原则
1. 父组件的数据发生了改变,子组件会自动跟着变
2. 子组件不能直接修改父组件传递过来的props props是只读的
components/04-cpt.vue
子组件文件。<template>
<div class="box">
<h3>标题:{{ title }}h3>
<p>价格:{{price}}元p>
<p>开业大酬宾,全场{{num}}折p>
<h4>库存:{{ kc }}件h4>
<button @click="kc--">卖一件button>
div>
template>
<script>
export default {
props:["title","price","num","kc"]//1.设置形参,语法固定
}
script>
<style scoped>
.box{
border:1px solid black;
}
style>
App.vue
父组件里面调用cpt4子组件,父组件通过给子组件加属性传值。<template>
<div>
<h1>超市商品列表:h1>
<p v-for="item in list" :key="item.id" >{{ item.title }} 库存:{{ item.kc }}p>
<hr>
<cpt-4
v-for="item in list" :key="item.id" :title="item.title" :price="item.price" :num="item.num" :kc="item.kc">cpt-4>
div>
template>
<script>
import cpt4 from "./components/04-cpt.vue";
export default {
components:{
cpt4,
cpt5,
},
data(){
return{
title:"火鸡面",
price:10,
num:5,
list:[
{id:1,title:"good-1",price:10,num:8,kc:10,},
{id:2,title:"good-2",price:88,num:8,kc:33,},
{id:3,title:"good-3",price:99,num:8,kc:66,},
]
}
}
}
script>
this.$emit('事件名', 参数1, 参数2, ...)
触发事件的同时传参的04-cpt.vue
子组件,内置封装好方法emit发射this.$emit("通道名称,要发送的数据,...)
<template>
<div class="box">
<h3>标题:{{ title }}</h3>
<p>价格:{{price}}元</p>
<p>开业大酬宾,全场{{num}}折</p>
<h4>库存:{{ kc }}件</h4>
<button @click="fn">卖一件</button>
</div>
</template>
<script>
export default {
// 特别封装:如何设置所谓的形参
props:["title","price","num","kc","id"],//1.设置形参id
//接受父级给的id、唯一标识、身份证
//发信号给父级:告诉父级自己是挪个!
methods:{
fn(){
// 2. 发送信号:内置封装好方法emit发射this.$emit("通道名称,要发送的数据,...)
this.$emit("aaa",this.id);
}
}
}
</script>
<style scoped>
.box{
border:1px solid black;
}
</style>
<template>
<div>
<h1>超市商品列表:h1>
<p v-for="item in list" :key="item.id" >{{ item.title }} 库存:{{ item.kc }}p>
<hr>
<cpt-4
v-for="item in list"
:key="item.id"
:title="item.title"
:price="item.price"
:num="item.num"
:kc="item.kc"
:id="item.id"
@aaa="fnn"
>cpt-4>
div>
template>
<script>
export default {
methods:{
fnn(id){
//
console.log(id);
}
},
}
script>
App.vue
父组件里面调用cpt4子组件,父组件通过给子组件加属性传值。<template>
<div>
<h1>超市商品列表:h1>
<p v-for="item in list" :key="item.id" >{{ item.title }} 库存:{{ item.kc }}p>
<hr>
<cpt-4
v-for="item in list"
:key="item.id"
:title="item.title"
:price="item.price"
:num="item.num"
:kc="item.kc"
:id="item.id"
@aaa="fnn"
>cpt-4>
div>
template>
<script>
import cpt4 from "./components/04-cpt.vue";
export default {
components:{
cpt4,
},
methods:{
fnn(id){
console.log(id);
//业务:find把符合条件成员单独跳出来,新数组
let i = this.list.findIndex(ele=>ele.id==id);
this.list[i].kc--;//找到到对应对象库存-1
}
},
data(){
return{
title:"火鸡面",
price:10,
num:5,
list:[
{id:1,title:"good-1",price:10,num:8,kc:10,},
{id:2,title:"good-2",price:88,num:8,kc:33,},
{id:3,title:"good-3",price:99,num:8,kc:66,},
]
}
}
}
script>
04-cptPlus.vue
文件,形参一会要接受一个对象<template>
<div class="box">
<h3>标题:{{ item.title }}h3>
<p>价格:{{item.price}}元p>
<p>开业大酬宾,全场{{item.num}}折p>
<h4>库存:{{ item.kc }}件h4>
<button @click="item.kc--">卖一件button>
div>
template>
<script>
export default {
//item:语义化知道,形参一会要接受一个对象
props:["item"],
}
script>
<style scoped>
.box{
border:1px solid black;
}
style>
App.vue
里面直接调用<template>
<div>
<h1>超市商品列表:h1>
<p v-for="item in list" :key="item.id" >{{ item.title }} 库存:{{ item.kc }}p>
<hr>
<cptPlus-4
v-for="item in list"
:key="item.id"
:item="item"
>cptPlus-4>
div>
template>
<script>
import cptPlus4from "./components/04-cptPlus.vue";
export default {
components:{
cptPlus4,
},
methods:{
fnn(id){
//业务:find把符合条件成员单独跳出来,新数组
let i = this.list.findIndex(ele=>ele.id==id);
this.list[i].kc--;//找到到对应对象库存-1
}
},
data(){
return{
title:"火鸡面",
price:10,
num:5,
list:[
{id:1,title:"good-1",price:10,num:8,kc:10,},
{id:2,title:"good-2",price:88,num:8,kc:33,},
{id:3,title:"good-3",price:99,num:8,kc:66,},
]
}
}
}
script>
05-cpt.vue
子组件,设置形参以及对象配置的要求。<template>
<div>
<h1>数量:{{ num }}h1>
div>
template>
<script>
export default {
props:{
//形参名称:对象配置有要求
num:{
type:Number,//数字类型
require:true,//必传项
default:100,//默认值
}
}
}
script>
App.vue
父组件里面调用cpt5子组件,父组件通过给子组件加属性传值。<template>
<div>
<h1>超市商品列表:h1>
<cpt-5 num="800">cpt-5>
div>
template>
<script>
import cpt5 from "./components/05-cpt.vue";
export default {
components:{
cpt5,
},
}
script>