Vue中data數(shù)據(jù)初始化方法詳解
當組件的根元素使用了v-if的時候, 并不會初始化data中的數(shù)據(jù) 如果想完全銷毀該組件并且初始化數(shù)據(jù),需要在使用該組件的本身添加v-if 或者是手動初始化該組件中的數(shù)據(jù)
初始化化數(shù)據(jù)的一些方法
Object.assign(this.$data, this.$options.data()) this.$data:當前的data數(shù)據(jù)(修改過后的); this.$options.data():初始化的data數(shù)據(jù); Object.assign的作用就是把this.$options.data()的值賦值給this.$data; // 表單初始化 this.form = this.$options.data().form // vue在創(chuàng)建頁面是會把data數(shù)據(jù)綁定到option屬性里,恢復只需要調(diào)用就可以了
下面詳細說說Object.assign的用法:
ES6的官方文檔的解釋是:Object.assign() 方法用于將所有可枚舉屬性的值從一個或多個源對象復制到目標對象。它將返回目標對象
方法一:
this.數(shù)據(jù)名 = this.$options.data().數(shù)據(jù)名;//重置某一個指定的數(shù)據(jù)
方法二:
this. data = this. data = this. data=this.options.data(); //初始化data里面的所有數(shù)據(jù)
方法三:
Object.assign(this. d a t a , t h i s . data, this. data,this.options.data()) //獲取data源對象,覆蓋當前data對象狀態(tài)

以下是一個簡單的例子
未銷毀數(shù)據(jù)的 直接在根元素上使用v-if 只是銷毀了el-dialog組件及其中的數(shù)據(jù) 并沒有銷毀當前組件的數(shù)據(jù)
父組件
<template>
<div>
<el-button @click="handleOpen">顯示</el-button>
<el-button @click="handleCls">隱藏</el-button>
<Children ref="children" />
</div>
</template>
<script>
import Children from './children.vue'
export default {
name: 'Father',
components: {
Children
},
props: {
},
data() {
return {
}
},
methods: {
handleOpen() {
this.$refs.children.dialogFormVisible = true
self.console.log(this.$refs.children.dialogFormVisible)
},
handleCls() {
this.$refs.children.dialogFormVisible = false
}
}
}
</script>
<style lang="scss" scoped>
</style>子組件
<template>
<el-dialog v-if="dialogFormVisible" title="收貨地址" :visible.sync="dialogFormVisible">
<el-form :model="form">
<el-form-item label="活動名稱" :label-width="formLabelWidth">
<el-input v-model="form.name" autocomplete="off" />
</el-form-item>
<el-form-item label="活動區(qū)域" :label-width="formLabelWidth">
<el-select v-model="form.region" placeholder="請選擇活動區(qū)域">
<el-option label="區(qū)域一" value="shanghai" />
<el-option label="區(qū)域二" value="beijing" />
</el-select>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取 消</el-button>
</div>
</el-dialog>
</template>
<script>
export default {
components: {
},
data() {
return {
dialogFormVisible: false,
form: {
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
desc: ''
},
formLabelWidth: '120px'
}
}
}
</script>
<style lang="scss" scoped>
</style>顯示效果
第一次填寫數(shù)據(jù)

第二次打開

銷毀數(shù)據(jù)的
父組件
<template>
<div>
<el-button @click="handleOpen">顯示</el-button>
<el-button @click="handleCls">隱藏</el-button>
<Children v-if="dialogShow" ref="children" @handleClose="handleClose" />
</div>
</template>
<script>
import Children from './children.vue'
export default {
name: 'Father',
components: {
Children
},
props: {
},
data() {
return {
dialogShow: false
}
},
methods: {
handleOpen() {
// this.$refs.children.dialogFormVisible = true
this.dialogShow = true
// self.console.log(this.$refs.children.dialogFormVisible)
},
handleCls() {
this.dialogShow = false
// this.$refs.children.dialogFormVisible = false
},
handleClose() {
this.dialogShow = false
}
}
}
</script>
<style lang="scss" scoped>
</style>子組件
<template>
<el-dialog title="收貨地址" :visible="true">
<el-form :model="form">
<el-form-item label="活動名稱" :label-width="formLabelWidth">
<el-input v-model="form.name" autocomplete="off" />
</el-form-item>
<el-form-item label="活動區(qū)域" :label-width="formLabelWidth">
<el-select v-model="form.region" placeholder="請選擇活動區(qū)域">
<el-option label="區(qū)域一" value="shanghai" />
<el-option label="區(qū)域二" value="beijing" />
</el-select>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="handleClose">取 消</el-button>
</div>
</el-dialog>
</template>
<script>
export default {
components: {
},
data() {
return {
dialogFormVisible: false,
form: {
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
desc: ''
},
formLabelWidth: '120px'
}
},
methods: {
handleClose() {
this.$emit('handleClose')
}
}
}
</script>
<style lang="scss" scoped>
</style>顯示效果
第一次填寫

第二次打開數(shù)據(jù)已經(jīng)清空了

到此這篇關(guān)于Vue中data數(shù)據(jù)初始化方法詳解的文章就介紹到這了,更多相關(guān)Vue data數(shù)據(jù)初始化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用拖拽方式創(chuàng)建結(jié)構(gòu)樹
這篇文章主要為大家詳細介紹了vue使用拖拽方式創(chuàng)建結(jié)構(gòu)樹,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
Vue中filter使用及根據(jù)id刪除數(shù)組元素方式
這篇文章主要介紹了Vue中filter使用及根據(jù)id刪除數(shù)組元素方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
解決elementui上傳組件el-upload無法第二次上傳問題
這篇文章主要介紹了解決elementui上傳組件el-upload無法第二次上傳問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
vue2使用ts?vue-class-component的過程
vue-property-decorator?是一個?Vue.js?的裝飾器庫,它提供了一些裝飾器來讓你在?Vue?組件中定義屬性、計算屬性、方法、事件等,本文給大家介紹vue2使用ts?vue-class-component的相關(guān)知識,感興趣的朋友一起看看吧2023-11-11
vue?+?element-ui?季度選擇器組件?el-quarter-picker示例詳解
本文介紹如何在Vue項目中使用基于Element-UI的季度選擇器組件ElQuarterPicker,通過引用并調(diào)用ElQuarterPicker.vue組件來實現(xiàn)季度選擇功能,感興趣的朋友跟隨小編一起看看吧2024-09-09

