【发布时间】:2019-01-27 17:56:48
【问题描述】:
我有以下 Vue 组件。它的目的是构建一个通用的侧边栏,使用不同的数据两次或三次
<template>
<div>
<h5>{{ title }}</h5>
<div v-for="prop of data" :key="prop.id">
<router-link :to="path(prop.id)">{{ prop.name }}
<i class="material-icons right">edit</i></router-link>
</div>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
props: {
data: Array,
title: String,
pathbase: String
},
methods: {
path(id): string {
return `${this.$props.pathbase}/${id}`;
}
}
});
</script>
我的问题是这样的。为什么我必须使用this.$props.pathbase 来访问这里的pathbase 值?为什么 this.pathbase 被 Typescript 视为“无效”?这不是我第一次尝试在没有$props 的情况下访问道具,但这是 Typescript 第一次抱怨它。此外,如果我使用this.pathbase,Typescript 会在我的编辑器 (VSCode) 中报错,但 Vue 编译项目时不会出错,并且组件会显示并正常运行。
VSCode 上的消息说:Property 'pathbase' does not exist on type 'Vue'.
我想知道它为什么会导致这个错误,因为我想更好地理解 typescript。为什么this.pathbase 会导致打字稿抱怨,即使它不是编译错误?为什么我必须使用$props?
【问题讨论】:
标签: typescript vue.js vuejs2