简体   繁体   English

为Vue组件中的数据分配prop值

[英]Assigning a prop value to data in a Vue Component

<template>
    <div class="container p-2">
        <form id="Lookup">
            <div class="row p-2">
                <div class="col-12 input-group ">
                    <input type="text" name="postcode"   :placeholder="initial" v-model="location" class="form-control p-3"><div class="input-group-append"><i class="material-icons input-group-text" @click="$emit('findlocation', location)">location_searching</i></div>
                </div>
            </div>
        </form>
    </div>
</template>

<script>
    export default{
        props: ['initial'],
        data: function () {
            return {
              location : this.initial
            }
        }
    }
</script>

The above is my Vue Component which is passed a string value called initial This value is passed from the template below 上面是我的Vue组件,该组件传递了一个名为initial的字符串值。该值从下面的模板传递

<practicesearch-component @findlocation="getlocation" :initial=postalTown" />

where postalTown is a data property of the main vue 其中postalTown是主Vue的数据属性

but instead of getting the string value of postalTown in location I get 但是我没有在位置中获取postalTown的字符串值,而是

function String() { [native code] } 函数String(){[本地代码]}

the prop initial in the Vue Component shows the correct string value but location has been assigned a function What am I doing wrong? Vue组件中的prop缩写显示正确的字符串值,但是位置已分配了功能我在做什么错?

When Vue initializes your component, the data function does not have access to the View Model this . 当Vue初始化您的组件时, data功能无权访问View Model this You can use the mounted hook to assign the value 您可以使用安装的挂钩分配值

export default {
    props: ['initial'],
    data: () => ({
        location: undefined
    }),
    mounted() {
        this.location = this.initial;
    }
}

Note that this way, whenever initial changes in the parent, location will not be updated . 请注意 ,以这种方式,无论何时父级中的initial更改, location都不会更新

Here is a quick sample: 这是一个快速示例:

 Vue.productionTip = false; Vue.component('child', { template: ` <div class="child"> Child component: <br/> location: {{ location }} <br/> initial: {{ initial }} </div> `, props: { initial: String }, data: () => ({ location: undefined }), mounted () { this.location = this.initial; } }); new Vue({ el: '#app', template: ` <div class="parent"> Parent Component <br/> location: {{ location }} <child :initial="location" /> </div> `, data: () => ({ location: 'US' }) }); 
 .parent { background-color: darkgray; padding: 1em; border: solid 1px black; color: white; } .child { background-color: gray; padding: 1em; border: solid 1px black; color: white; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"></div> 

After much analysis I found the problem that was causing this behaviour. 经过大量分析,我发现了导致这种现象的问题。

<practicesearch-component @findlocation="getlocation" :initial=postalTown" />

postalTown in the above was not set initially in the Vue instance but within mounted therefore the component was being passed an uninitialised string object and not the initialised value of postalTown 上面的postalTown最初不是在Vue实例中设置的,而是在挂载中设置的,因此该组件正在传递未初始化的字符串对象,而不是postalTown的初始化值

I added the :key attribute on postalTown as below which causes the component to re-render with the initialised value of postalTown Perhaps this is not the best option but it works 我在postalTown上添加了:key属性,如下所示,该属性导致组件以postalTown的初始值重新渲染。也许这不是最佳选择,但它可以工作

<practicesearch-component @findlocation="getlocation" :key=postalTown :initial=postalTown ></practicesearch-component>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM