React技術(shù)棧支援Vue項(xiàng)目,你需要提前了解的
作者:京東云開發(fā)者
父組件中有一個(gè)表單日期組件,子組件是一個(gè)彈層(彈層中有日期組件,默認(rèn)值取父組件選中的日期),父組件更改日期范圍后,子組件打開默認(rèn)日期也需要更新。
寫在前面
- react整體是函數(shù)式的思想,把組件設(shè)計(jì)成純組件,狀態(tài)和邏輯通過參數(shù)傳入,而vue的思想是響應(yīng)式的,也就是基于是數(shù)據(jù)可變的,通過對(duì)每一個(gè)屬性建立Watcher來(lái)監(jiān)聽, 當(dāng)屬性變化的時(shí)候,響應(yīng)式的更新對(duì)應(yīng)的虛擬dom
- react的思路通過js來(lái)生成html, 所以設(shè)計(jì)了jsx,還有通過js來(lái)操作css。vue是自己寫了一套模板編譯的邏輯,可以把js css html糅合到一個(gè)模板里邊
- react可以通過高階組件來(lái)擴(kuò)展,而vue需要通過mixins來(lái)擴(kuò)展
頻繁用到的場(chǎng)景
1. 數(shù)據(jù)傳遞:父?jìng)髯樱父伦尤绾稳〉眯聰?shù)據(jù)
父組件中有一個(gè)表單日期組件,子組件是一個(gè)彈層(彈層中有日期組件,默認(rèn)值取父組件選中的日期),父組件更改日期范圍后,子組件打開默認(rèn)日期也需要更新。如下:
// 父組件
<template>
<div>
<date-span style="flex-grow: 1" ref="dateSpanE" :noCache="true" :startDate="startDate"
:endDate="endDate" type="weekrange" @change="changeDate"></date-span>
<!-- 子彈層組件 -->
<ActiveTakeEffect ref="activeModal" :timeRange="makeActiveTime" />
</div>
</template>
<script>
import DateSpan from '@/components/DateSpanE'
export default {
components: { DateSpan },
// ...
data: () => {
return {
makeActiveTime: {
startDate: '',
endDate: ''
},
}
},
computed: {
startDate() {
return this.makeActiveTime.startDate
},
endDate() {
return this.makeActiveTime.endDate
}
},
methods: {
// 父組件表單日期修改時(shí)更新了傳入的日期
changeDate(dateInfo) {
const { start: startDate, end: endDate } = dateInfo
this.makeActiveTime = {
startDate,
endDate
}
}
}
}
</script>
// 子組件
<template>
<Modal v-model="showModal" width="680" title="XXX" :mask-closable="false" @on-visible-change="visibleChange"
:loading="loading">
<div class="single-effect-modal">
<div class="form-wrapper">
<date-span style="flex-grow: 1" ref="dateSpanE" :noCache="true" :startDate="startDate" :endDate="endDate"
type="weekrange" @change="changeDate"></date-span>
</div>
</div>
</Modal>
</template>
<script>
import Api from '@/api_axios'
import DateSpan from '@/components/DateSpanE'
import { formatDate } from '@/common/util'
import moment from 'moment'
export default {
components: {
DateSpan
},
props: {
// 定義父組件傳入的prop
timeRange: {
type: Object,
default: () => {
return {
startDate: new Date(),
endDate: moment().add(17, 'w').toDate()
}
}
}
},
data() {
return {
loading: true,
showModal: false,
// data中定義子組件日期范圍組件所需的展示數(shù)據(jù),默認(rèn)取props中定義的值
timeRangeFromProps: this.timeRange
}
},
computed: {
startDate() {
return this.timeRangeFromProps.startDate
},
endDate() {
return this.timeRangeFromProps.endDate
}
},
watch: {
// 監(jiān)聽傳入的props值,props值更改時(shí)更新子組件數(shù)據(jù)
// 若無(wú)此監(jiān)聽,父組件修改日期后,子組件的日期范圍得不到更新
timeRange() {
this.timeRangeFromProps = this.timeRange
}
},
methods: {
changeDate(dateInfo) {
const { start: startDate, end: endDate } = dateInfo
this.timeRangeFromProps = {
startDate,
endDate
}
},
toggle(isShow) {
this.showModal = isShow
},
// ...
}
}
</script>
<style lang="less">
.single-effect-modal {
.form-wrapper {
min-height: 100px;
}
.item-label {
min-width: 60px;
}
}
</style>
2. $parent$refs$emit
2.1 $refs訪問子組件中的方法或?qū)傩?/h2><ActiveTakeEffect ref="activeModal" :timeRange="makeActiveTime" />
<script>
this.$refs.activeModal.timeRangeFromProps // timeRangeFromProps是子組件中的屬性
this.$refs.activeModal.toggle(true) // toggle是子組件中的方法名
</script>
2.1 $parent訪問父組件中的方法或?qū)傩?$emit觸發(fā)父組件中定義的方法
// 子組件
<script>
this.$parent.makeActiveTime // makeActiveTime是父組件中的屬性
this.$parent.changeDate({startDate:xxx, endDate: xxx}) // changeDate是父組件中的方法名
</script>
// 父組件,忽略其他項(xiàng)
<date-span @conditionChange="conditionChange"></date-span>
<scipt>
// ...
methods: {
conditionChange(controlName) {
// ...
}
}
// ...
</script>
<script>
// 子組件中調(diào)用
this.$emit('conditionChange', 'dateSpan')
</script>
3. mixins擴(kuò)展使用
// itemList就是來(lái)自treeSelectMixin中定義的數(shù)據(jù)
<SwitchButton :itemList="itemList" @change="toggleSelectAll"></SwitchButton>
<script>
import mixin from './treeSelectMixin'
export default {
mixins: [mixin],
components: {
Treeselect,
SwitchButton
},
// ...
}
</script>
4. 樣式的兩種寫法
// 同一個(gè).vue文件中可以出現(xiàn)以下兩個(gè)style標(biāo)簽
<style lang="less">
</style>
// 當(dāng) `<style>` 標(biāo)簽有 `scoped` 屬性時(shí),它的 CSS 只作用于當(dāng)前組件中的元素。
<style lang="less" scoped>
</style>
<ActiveTakeEffect ref="activeModal" :timeRange="makeActiveTime" />
<script>
this.$refs.activeModal.timeRangeFromProps // timeRangeFromProps是子組件中的屬性
this.$refs.activeModal.toggle(true) // toggle是子組件中的方法名
</script>
// 子組件
<script>
this.$parent.makeActiveTime // makeActiveTime是父組件中的屬性
this.$parent.changeDate({startDate:xxx, endDate: xxx}) // changeDate是父組件中的方法名
</script>
// 父組件,忽略其他項(xiàng)
<date-span @conditionChange="conditionChange"></date-span>
<scipt>
// ...
methods: {
conditionChange(controlName) {
// ...
}
}
// ...
</script>
<script>
// 子組件中調(diào)用
this.$emit('conditionChange', 'dateSpan')
</script>
// itemList就是來(lái)自treeSelectMixin中定義的數(shù)據(jù)
<SwitchButton :itemList="itemList" @change="toggleSelectAll"></SwitchButton>
<script>
import mixin from './treeSelectMixin'
export default {
mixins: [mixin],
components: {
Treeselect,
SwitchButton
},
// ...
}
</script>
// 同一個(gè).vue文件中可以出現(xiàn)以下兩個(gè)style標(biāo)簽
<style lang="less">
</style>
// 當(dāng) `<style>` 標(biāo)簽有 `scoped` 屬性時(shí),它的 CSS 只作用于當(dāng)前組件中的元素。
<style lang="less" scoped>
</style>
以上就是入門時(shí)困擾較多的地方~祝換乘順利
作者:京東零售 黃曉麗
來(lái)源:京東云開發(fā)者社區(qū) 轉(zhuǎn)載請(qǐng)注明來(lái)源
責(zé)任編輯:武曉燕
來(lái)源:
今日頭條