【发布时间】:2018-08-11 05:43:36
【问题描述】:
我正在使用一个非常简单的vue 组件,我想通过道具将信息传递给它。在 HTML 中它看起来像:
<myapp v-bind:source-key="some_key" v-bind:destination-key="some_other_key"></myapp>
组件如下所示:
<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>
我希望这会在组件中设置道具 sourceKey 等于 some_key 和 destinationKey 等于 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://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.
所以看起来期望值被视为关键?然后还有更多错误说从未定义过sourceKey 变量:
[Vue warn]: Error in mounted hook: "ReferenceError: sourceKey is not defined"
如果不在props 块中,我在哪里定义道具?
【问题讨论】:
-
this.$http.get('/map/' + sourceKey + '/' + destinationKey)在该行中,您必须使用this关键字 (this.sourceKey) 访问您的道具
标签: javascript vue.js vuejs2