简体   繁体   English

将 props 传递给 vue2 组件

[英]passing props into vue2 component

I have a very simple vue component I'm using that I want to pass information into via props.我正在使用一个非常简单的vue组件,我想通过道具将信息传递给它。 In the HTML it looks like:在 HTML 中,它看起来像:

<myapp v-bind:source-key="some_key" v-bind:destination-key="some_other_key"></myapp>

The component looks like this:该组件如下所示:

<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card card-default">
                    <div class="card-header">Example Component</div>

                    <div class="card-body">
                        I'm an example component.
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        props: ['sourceKey', 'destinationKey'],
        mounted() {
            this.$http.get('/map/' + sourceKey + '/' + destinationKey)
                .then(function (response) {
                    console.dir(response)
                })
                .catch(function (error) {
                    console.dir(error);
                });
            console.log('got here')
        }
    }
</script>

I'd expect this would set the props sourceKey equal to some_key and destinationKey equal to some_other_key in the component, but I'm getting errors:我希望这会将道具sourceKey设置为组件中的some_keydestinationKey等于some_other_key ,但我收到错误:

[Vue warn]: Property or method "some_key" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

So it seems like the expected value is being treated as the key?所以看起来期望值被视为关键? Then there's more errors saying that the sourceKey variable was never defined:然后有更多错误说从未定义sourceKey变量:

[Vue warn]: Error in mounted hook: "ReferenceError: sourceKey is not defined"

Where do I define the props, if not in the props block?如果不在props块中,我在哪里定义道具?

Here: this.$http.get('/map/' + sourceKey + '/' + destinationKey)这里: this.$http.get('/map/' + sourceKey + '/' + destinationKey)

In <script> , you need to access this.sourceKey and this.destinationKey , to specify you want the properties off the Vue instance (and not variables defined elsewhere).<script>中,您需要访问this.sourceKeythis.destinationKey ,以指定您希望 Vue 实例的属性(而不是其他地方定义的变量)。 You can only leave off this.你只能离开this. in templates.在模板中。

As for your first error, make sure some_key and some_other_key are variables defined in your Vue instance, under data() { ... } , computed properties, and so on, as the error message suggests.至于您的第一个错误,请确保some_keysome_other_key是在您的 Vue 实例中定义的变量,在data() { ... } 、计算属性等下,如错误消息所示。

In parent's template:在父母的模板中:

<myapp v-bind:source-key="some_key" v-bind:destination-key="some_other_key"></myapp>

You get the error:你得到错误:

[Vue warn]: Property or method "some_key" is not defined on the instance but referenced during render. [Vue 警告]:属性或方法“some_key”未在实例上定义,但在渲染期间被引用。 Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.通过初始化该属性,确保该属性是反应性的,无论是在数据选项中,还是对于基于类的组件。 See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties .请参阅: https ://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties。

Because the parent does not have some_key in its data() .因为父级的data()中没有some_key

In child:在儿童中:

[Vue warn]: Error in mounted hook: "ReferenceError: sourceKey is not defined" [Vue 警告]:挂载钩子错误:“ReferenceError: sourceKey is not defined”

You get an error because inside the Vue instance functions, the prop s must be referenced using this .您会收到一个错误,因为在 Vue 实例函数内部,必须使用this引用prop

See a demo correcting both errors below.请参阅纠正以下两个错误的演示。

 Vue.component('myapp', { template: "#myAppTemplate", props: ['sourceKey', 'destinationKey'], mounted() { this.$http.get('/map/' + this.sourceKey + '/' + this.destinationKey) // this fixes "ReferenceError: sourceKey is not defined" .then(function(response) { console.dir(response) }) .catch(function(error) { console.dir(error); }); console.log('got here') } }) new Vue({ el: '#app', data: { some_key: 'aaa', // this fixes [Vue warn]: Property or method "some_key" is not defined some_other_key: 'bbb' // this fixes [Vue warn]: Property or method "some_other_key" is not defined } });
 <script src="https://unpkg.com/vue@2.5.13/dist/vue.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vue-resource@1.4.0"></script> <div id="app"> <myapp v-bind:source-key="some_key" v-bind:destination-key="some_other_key"></myapp> </div> <template id="myAppTemplate"> <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card card-default"> <div class="card-header">Example Component</div> <div class="card-body"> I'm an example component. </div> </div> </div> </div> </div> </template>

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

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