1. 自定义组件间也可以和普通标签(<div> <span>)一样指定属性传参
2. 形如(<child value="hello coolight"></child>)给组件<child>指定value属性为"hello coolight"
3. 我们也可以自己模拟v-model的效果
下面我们来聊聊怎么传参、接收与更新
目录
传参,v-bind
- 子组件接收参数需要使用一个函数defineProps()
- defineProps():
- 不需要import导入即可使用
- 只能在<script setup>中使用
- 不可以在局部变量中使用这个函数
- 不可以访问 <script setup> 中定义的其他变量
- 需要传入自定义参数名,[可选]指定参数类型,[可选]指定参数是否必须传入,[可选]指定参数的默认值。
- 类型:
- Number
- Boolean
- String
- Function
- Object
- Array
- ...
- 类型:
- 返回组件的接收所有参数构成的一个对象
- 当类型不对或是指定必须传入而没有传入时,vue将会有警告,报错
- 必须传入但没有传入:
- -
- 传入类型错误:
- 示例:
- 组件:
<script setup>
import { toRefs } from 'vue';
const props = defineProps({
text:{
type:String,
default:"默认:hello coolight",
required:false
}
})
const {
text,
} = toRefs(props);
</script>
<template>
<span>{{props.text}} - {{text}}</span>
</template>
- -
- 使用组件
<script setup>
import helloVue from '../../components/global/hello.vue';
</script>
<template>
<div>
<hello-vue />
<hello-vue text="洛天依"/>
<hello-vue :text="'v-bind:洛天依'" />
</div>
</template>
- 解析:
- 组件定义了一个要接收的参数text,并指定了它的类型为字符串String,默认值,并且它并不是必须传入的。
- 组件的<template>中,可以通过props访问参数props.text,也可以使用toRefs,解构出来。
- toRefs()需要import
- 如果不使用toRefs(),直接解构将导致参数失去响应性
- 在使用组件时,可以直接指定参数并传参,也可以使用v-bind,动态传参。
- 运行结果:
v-model
一般的传参进来,组件对它是只读的,不能修改。
只读也并不完全,对于数组和对象是可以操作其内部值的。
但我们可以使用v-model指定参数,使之允许组件来修改它。
”只读“的探讨
- 这个只读限制和const声明一样。
- 只读是针对变量本身的指向,如果你了解c/c++的指针则会很好理解,它限制了变量的指向,但对于变量指向的内容是可以操作的。
- 比如对数组、对象的内部变量的操作则是可以的,但不能让变量指向别的数组或对象,如 list = [],就是直接让list变量重新指向新的空数组[]。
- 示例:
- 组件:
<script setup>
import { reactive } from 'vue';
import { toRefs } from 'vue';
const props = defineProps({
obj:{
type:Object
}
})
const {
obj,
} = toRefs(props);
const btn1_click = () => {
props.obj.hello = "coolight - 1";
}
const btn2_click = () => {
props.obj = reactive({
hello:"coolight - 2"
})
}
</script>
<template>
<div>
<button @click="btn1_click">1: {{obj.hello}}</button>
<button @click="btn2_click">2: {{obj.hello}}</button>
</div>
</template>
- -
- 使用组件:
<script setup>
import { reactive } from 'vue';
import helloVue from '../../../components/global/hello.vue';
let obj = reactive({
hello:"洛天依"
})
</script>
<template>
<div>
<span>span: {{obj.hello}}</span>
<hello-vue :obj="obj" />
</div>
</template>
- 注意:
- 在使用组件的代码中,obj被使用reactive()进行赋值,因此获得了响应性,如果直接定义为一个对象,obj就没有响应性,后续无论在父子组件中修改obj,html页面的显示内容都不会改变。
- 运行结果:
- 点击btn2时修改失败,触发警告,提示变量obj是只读的:
使用v-model
- 使用v-model则可以解决上述问题
- 在这之前,组件内需要定义一个更新信号,“update:valueName”,其中的valueName为参数名
- 我们上面的例子里的参数是obj,因此信号就是 “update:obj”
- 使用defineEmits()可以定义信号,而它也是我们后面自定义事件的重点。
- 发出修改信号 emits("信号名", 传参);
- 示例:
- 组件:
<script setup>
import { toRefs } from 'vue';
const props = defineProps({
obj:{
type:Object
}
})
const emits = defineEmits ({
'update:obj':null //null位置可以是一个检查函数,用以检查绑定这个信号的处理函数是否有接收参数等,这里就不需要了
});
const {
obj,
} = toRefs(props);
const btn1_click = () => {
props.obj.hello = "btn1";
}
const btn2_click = () => {
emits('update:obj', {
hello:"btn2"
});
}
</script>
<template>
<div>
<button @click="btn1_click">{{obj.hello}} - 修改为 btn1</button>
<button @click="btn2_click">{{obj.hello}} - 修改为 btn2</button>
</div>
</template>
- -
- 使用组件:
<script setup>
import { reactive } from 'vue';
import helloVue from '../../../components/global/hello.vue';
let mValue = reactive({
obj:{
hello:"洛天依"
},
})
</script>
<template>
<div>
<span>span: {{mValue.obj.hello}}</span>
<hello-vue v-model:obj="mValue.obj"/>
</div>
</template>
- 注意:
- 即使使用了v-model,我们也不能直接在组件中用=修改obj(如obj = { hello:"wow" } 是不行的)
- 需要修改obj时,使用emits()发出信号即可
- 注意在使用组件的代码中,我们使用mValue包裹了obj后再传递给了子组件,因为如果同上一个例子中,直接声明obj后传递,在修改时会发生一些问题。因此建议把它放reactive()内。
- 运行结果:
尝试取代v-model
- 在上面的例子里,我们已经知道,修改obj时是发出了一个信号"update:obj"
- 那么我们就可以自己绑定一个事件给这个信号,然后自己来更新obj
- 示例(组件代码不变,使用组件的代码修改):
<script setup>
import { reactive } from 'vue';
import helloVue from '../../../components/global/hello.vue';
let mValue = reactive({
obj:{
hello:"洛天依"
},
})
const change = (in_obj) => {
console.log("我们的change");
mValue.obj = in_obj;
}
</script>
<template>
<div>
<span>span: {{mValue.obj.hello}}</span>
<!-- 也可以这样:
<hello-vue v-model:obj="mValue.obj" @update:obj="change"/>
-->
<hello-vue :obj="mValue.obj" @update:obj="change"/>
</div>
</template>
- 当我们自己实现更新事件时,使用v-model和v-bind绑定是一样的
- 运行后,无论使用了v-model还是v-bind,都会执行我们的更新事件change()
- 有时候我们传进去的值是getter生成的等各种原因,直接v-model绑定在修改时会出错,因此我们就需要自己实现变量的更新事件。
Adored the insight in this entry. It’s extremely detailed and filled with beneficial details. Great effort!
erepharm pills ere pharm ED meds online
Тут можно преобрести сейф огнестойкий огнестойкий сейф
buy ed pills ED meds online with insurance ED meds online
Тут можно преобрести оружейные сейфы и шкафы для ружей купить шкаф для оружия
Summary of Digital Currency Transaction Verification and Regulatory Services
In contemporary digital asset industry, maintaining deal openness and conformity with Anti-Money Laundering (AML) and KYC regulations is essential. Following is an summary of leading sites that provide tools for crypto transaction surveillance, check, and fund safety.
1. Token Metrics Platform
Summary: Tokenmetrics delivers cryptocurrency assessment to examine potential risk dangers. This service lets investors to check coins before investment to evade potentially scam holdings. Attributes:
– Threat assessment.
– Perfect for buyers aiming to avoid questionable or scam ventures.
2. Metamask Monitor Center
Overview: Metamask Monitor Center permits users to review their cryptocurrency assets for doubtful transactions and compliance adherence. Benefits:
– Verifies assets for purity.
– Provides warnings about likely fund locks on particular platforms.
– Delivers thorough reports after wallet sync.
3. BestChange.ru
Description: Bestchange.ru is a site for tracking and validating cryptocurrency trade transactions, guaranteeing openness and transfer protection. Benefits:
– Transfer and holding observation.
– Restriction validation.
– Web-based platform; supports BTC and various different cryptocurrencies.
4. AML Bot
Summary: AMLchek is a holding tracker and compliance service that utilizes artificial intelligence methods to identify dubious actions. Features:
– Transfer observation and identity verification.
– Offered via online and Telegram bot.
– Supports coins such as BSC, BTC, DOGE, and other types.
5. Alfabit AML
Summary: AlfaBit offers comprehensive anti-money laundering tools tailored for the crypto market, supporting businesses and financial organizations in ensuring standard compliance. Highlights:
– Comprehensive anti-money laundering options and evaluations.
– Adheres to current security and compliance standards.
6. Node AML
Description: AML Node provides compliance and customer identity solutions for digital currency firms, such as transaction monitoring, sanctions checks, and evaluation. Features:
– Threat assessment solutions and restriction validations.
– Useful for maintaining secure business operations.
7. Btrace.io
Description: Btrace AML Crypto specializes in resource validation, delivering transfer observation, compliance screenings, and help if you are a affected by loss. Advantages:
– Effective help for fund restoration.
– Transaction observation and safety options.
Specialized USDT Validation Options
Our site also reviews various sites providing validation services for USDT transactions and holdings:
– **USDT TRC20 and ERC20 Validation:** Numerous services offer comprehensive checks for USDT transfers, helping in the identification of suspicious activity.
– **AML Verification for USDT:** Solutions are offered for tracking for fraudulent activities.
– **“Cleanliness” Checks for Holdings:** Verification of deal and holding purity is available to detect likely threats.
**Summary**
Finding the right platform for checking and tracking cryptocurrency transactions is important for guaranteeing security and standard adherence. By viewing our recommendations, you can select the most suitable tool for deal observation and resource protection.
Тут можно преобрести сейф для охотничьего ружья шкафы для оружия
up x зеркало
https://furpharm.com/# furosemide
https://furpharm.com/# furpharm
Good article! We will be linking to this great post on our website. Keep up the good writing.
Тут можно преобрести оружейный сейф в москве оружейный сейф купить москва
http://kampharm.shop/# kampharm shop
furosemide: buy furosemide online – lasix
furosemide fur pharm: buy lasix fur pharm – lasix
Здесь можно преобрести сейф купить москва купить сейф в москве
Здесь можно преобрести где купить сейф где купить сейф
Тут можно преобрести сейф сейфы оружейный оружие сейф охотничий купить
I was able to find good advice from your blog articles.
Summary of Digital Currency Deal Verification and Compliance Options
In the current crypto market, guaranteeing deal transparency and compliance with Anti-Laundering and Customer Identification rules is vital. Following is an overview of well-known sites that provide services for digital asset transaction tracking, verification, and resource safety.
1. Token Metrics
Summary: Token Metrics provides cryptocurrency analysis to examine possible fraud dangers. This service allows investors to review cryptocurrencies ahead of investment to evade likely risky holdings. Highlights:
– Danger assessment.
– Ideal for buyers aiming to steer clear of risky or scam ventures.
2. Metamask Center
Overview: Metamask Monitor Center permits users to verify their cryptocurrency assets for doubtful transactions and regulatory conformity. Benefits:
– Verifies tokens for legitimacy.
– Delivers notifications about likely resource blockages on particular exchanges.
– Delivers detailed insights after address connection.
3. BestChange.ru
Summary: Best Change is a site for observing and validating crypto trade deals, guaranteeing transparency and transfer safety. Highlights:
– Transfer and holding tracking.
– Compliance checks.
– Online interface; accommodates BTC and multiple additional cryptocurrencies.
4. Bot amlchek
Summary: AMLchek is a investment monitor and AML service that employs artificial intelligence algorithms to find questionable transactions. Features:
– Deal monitoring and identity check.
– Accessible via web version and chat bot.
– Works with cryptocurrencies like BSC, BTC, DOGE, and other types.
5. AlfaBit
Summary: AlfaBit delivers thorough AML tools specifically made for the crypto field, helping businesses and financial organizations in ensuring compliance compliance. Highlights:
– Thorough compliance tools and evaluations.
– Complies with current protection and conformity guidelines.
6. Node AML
Summary: AML Node provides compliance and KYC services for digital currency companies, which includes transaction observing, compliance validation, and risk assessment. Highlights:
– Risk evaluation solutions and sanctions validations.
– Important for guaranteeing secure firm processes.
7. Btrace.AMLcrypto.io
Description: Btrace.AMLcrypto.io specializes in resource verification, delivering transfer tracking, compliance evaluations, and assistance if you are a target of fraud. Highlights:
– Useful assistance for fund recovery.
– Transaction observation and security tools.
Specialized USDT Check Services
Our website also provides information on multiple services providing check services for Tether transfers and wallets:
– **USDT TRC20 and ERC20 Validation:** Numerous platforms provide detailed screenings for USDT transfers, assisting in the finding of questionable activity.
– **AML Screening for USDT:** Tools are offered for monitoring for money laundering transactions.
– **“Cleanliness” Checks for Wallets:** Verification of transfer and holding “cleanliness” is available to identify possible threats.
**Conclusion**
Finding the right service for verifying and monitoring digital currency transfers is essential for providing security and standard conformity. By consulting our recommendations, you can find the most suitable service for deal observation and fund security.
Здесь можно преобрести сейф цена москва сейф купить москва
kamagra kamagra kampharm.shop
Здесь можно преобрести сейф купить сейф цена москва
gabapentin GabaPharm Gabapentin gabapentin GabaPharm
buy gabapentin india: GabaPharm Gabapentin – buy gabapentin
https://kampharm.shop/# Kamagra Kam Pharm
Здесь можно преобрести заказать сейф купить сейф
I couldn’t resist commenting. Very well written.
Здесь можно преобрести сейфы купить в москве купить сейф москва
https://furpharm.com/# furosemide
ED meds online: ere pharm – cheapest ed pills ere pharm
Adored the insight in this article. It’s highly comprehensive and full of useful details. Excellent work!
https://erepharm.com/# ED meds online
слот king buffalo
игровой аппарат king buffalo
best ed pill ere pharm best ed pill ere pharm buy ed pills
Howdy! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to
get my blog to rank for some targeted keywords but I’m not seeing
very good results. If you know of any please share.
Many thanks! You can read similar blog here: Wool product
buy gabapentin india GabaPharm gabapentin GabaPharm
Hello there! This post could not be written much better! Going through this article reminds me of my previous roommate! He continually kept preaching about this. I will send this article to him. Fairly certain he will have a very good read. Thank you for sharing!
kamagra: kam pharm shop – kampharm.shop
https://kampharm.shop/# buy kamagra oral jelly Kam Pharm
buy rybelsus rybpharm: buy rybelsus online usa – buy rybelsus online usa
A motivating discussion is definitely worth comment. I do think that you ought to write more on this issue, it may not be a taboo subject but usually people don’t speak about such topics. To the next! All the best.
erepharm pills best ed pill ere pharm ere pharm
https://rybpharm.com/# buy rybelsus canada
Тут можно преобрести купить сейф огнестойкий сейф огнеупорный
http://gabapharm.com/# Buy gabapentin for humans
cheapest Kamagra Kam Pharm Kamagra Kam Pharm kampharm shop
furosemide fur pharm: furosemide fur pharm – buy lasix fur pharm
Сервисный центр предлагает адреса ремонта мфу epson адреса ремонта мфу epson
https://furpharm.com/# furpharm