ฉันมีส่วนประกอบที่มีชุดข้อมูลเริ่มต้นเฉพาะ:
data: function (){
    return {
        modalBodyDisplay: 'getUserInput', // possible values: 'getUserInput', 'confirmGeocodedValue'
        submitButtonText: 'Lookup', // possible values 'Lookup', 'Yes'
        addressToConfirm: null,
        bestViewedByTheseBounds: null,
        location:{
            name: null,
            address: null,
            position: null
        }
}
นี่เป็นข้อมูลสำหรับหน้าต่างโมดอลดังนั้นเมื่อแสดงว่าฉันต้องการให้เริ่มต้นด้วยข้อมูลนี้ หากผู้ใช้ยกเลิกจากหน้าต่างฉันต้องการรีเซ็ตข้อมูลทั้งหมดเป็นสิ่งนี้
ฉันรู้ว่าฉันสามารถสร้างวิธีการรีเซ็ตข้อมูลและตั้งค่าคุณสมบัติของข้อมูลทั้งหมดด้วยตนเองกลับไปที่เดิม:
reset: function (){
    this.modalBodyDisplay = 'getUserInput';
    this.submitButtonText = 'Lookup';
    this.addressToConfirm = null;
    this.bestViewedByTheseBounds = null;
    this.location = {
        name: null,
        address: null,
        position: null
    };
}
แต่ดูเหมือนว่าจะเลอะเทอะจริงๆ หมายความว่าหากฉันทำการเปลี่ยนแปลงคุณสมบัติข้อมูลของคอมโพเนนต์ฉันจะต้องแน่ใจว่าฉันจำอัปเดตโครงสร้างของวิธีการรีเซ็ต นั่นไม่ได้น่ากลัวอย่างแน่นอนเนื่องจากเป็นส่วนประกอบแบบแยกส่วนขนาดเล็ก แต่มันทำให้สมองของฉันกรีดร้อง
วิธีแก้ปัญหาที่ฉันคิดว่าจะได้ผลคือการคว้าคุณสมบัติข้อมูลเริ่มต้นด้วยreadyวิธีการจากนั้นใช้ข้อมูลที่บันทึกไว้เพื่อรีเซ็ตส่วนประกอบ:
data: function (){
    return {
        modalBodyDisplay: 'getUserInput', 
        submitButtonText: 'Lookup', 
        addressToConfirm: null,
        bestViewedByTheseBounds: null,
        location:{
            name: null,
            address: null,
            position: null
        },
        // new property for holding the initial component configuration
        initialDataConfiguration: null
    }
},
ready: function (){
    // grabbing this here so that we can reset the data when we close the window.
    this.initialDataConfiguration = this.$data;
},
methods:{
    resetWindow: function (){
        // set the data for the component back to the original configuration
        this.$data = this.initialDataConfiguration;
    }
}
แต่initialDataConfigurationวัตถุกำลังเปลี่ยนไปพร้อมกับข้อมูล (ซึ่งสมเหตุสมผลเพราะในวิธีการอ่านเราinitialDataConfigurationได้รับขอบเขตของฟังก์ชันข้อมูล
มีวิธีการดึงข้อมูลการกำหนดค่าเริ่มต้นโดยไม่สืบทอดขอบเขตหรือไม่?
ฉันคิดเรื่องนี้มากเกินไปและมีวิธีที่ดีกว่า / ง่ายกว่านี้ไหม
การเข้ารหัสข้อมูลเริ่มต้นเป็นทางเลือกเดียวหรือไม่