【发布时间】:2021-06-24 10:07:27
【问题描述】:
在从 API 点获取所需数据之前尝试将默认值传递给我的子组件。但我没有这样做,想知道为什么?
我的子组件恰好是一张传单地图,并且正在向它传递一些国家/地区坐标,例如纬度和经度。我是这样做的:
<template>
<div class="home">
<LeafletMap
:latitude="countryStats.countryInfo.lat || 0"
:longitude="countryStats.countryInfo.long || 0"
/>
</div>
</template>
或者像这样:
<template>
<div class="home">
<LeafletMap
:latitude="countryStats.countryInfo.lat ? countryStats.countryInfo.lat : 0"
:longitude="countryStats.countryInfo.long ? countryStats.countryInfo.long : 0"
/>
</div>
</template>
但这两种方法似乎都行不通。
我只得到:
vue.runtime.esm.js?2b0e:619 [Vue 警告]:渲染错误:“TypeError: 无法读取未定义的属性“纬度”
发现于
---> 在 src/components/home/Home.vue
这是我的子组件:
<template>
<div class="map">
<h1>World Map</h1>
<div id="mapContainer">
<l-map
style="height: 80%; width: 100%"
:zoom="zoom"
:center="center"
@update:zoom="zoomUpdated"
@update:center="centerUpdated"
@update:bounds="boundsUpdated"
>
<l-tile-layer :url="url"></l-tile-layer>
</l-map>
</div>
</div>
</template>
<script>
import "leaflet/dist/leaflet.css";
import { LMap, LTileLayer } from "vue2-leaflet";
export default {
name: "LeafletMap",
components: {
LMap,
LTileLayer
},
props: {
latitude: {
type: Number,
default: 0
},
longitude: {
type: Number,
default: 0
}
},
data() {
return {
url: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
zoom: 4,
center: [this.latitude, this.longitude],
bounds: null
};
},
methods: {
zoomUpdated(zoom) {
this.zoom = zoom;
},
centerUpdated(center) {
this.center = center;
},
boundsUpdated(bounds) {
this.bounds = bounds;
}
}
};
</script>
<style src="./LeafletMap.css" scoped />
想知道,如果我仍然应该在我的子组件中为我的道具(latitude 和 longitude)提供一个默认值,还是会导致冲突?
欢迎任何帮助。
【问题讨论】:
-
你能发布你的 app.vue 吗?
标签: javascript vuejs2 leaflet