【发布时间】:2017-11-20 18:39:37
【问题描述】:
我开始使用 Typescript 并尝试将其应用到我的项目中。但是,我无法使用像 vue-resource 这样的 Vue.js 插件来使用它。
当我使用时
this.$http.post()
我得到错误:
错误 TS2339:“typeof Vue”类型上不存在属性“$http”。
这是有道理的,因为我处于课堂环境中。但我该怎么做呢?这是我的完整组件:
<template>
<div>
<h1>Sign up</h1>
<form>
<div class="form-group">
<label for="name">Name</label>
<input v-model="name" type="text" class="form-control" name="name" placeholder="Name">
<small class="form-text text-muted">Please provide a name.</small>
</div>
<div class="form-group">
<label for="name">Password</label>
<input v-model="password" type="password" class="form-control" name="password" placeholder="Password">
<small class="form-text text-muted">Please provide a password.</small>
</div>
<input type="submit" class="btn btn-primary" value="Submit" @click.prevent="save">
</form>
</div>
</template>
<script lang="ts">
import Component from 'vue-class-component'
@Component
export default class SignUp extends Vue {
name: string = ''
password: string = ''
save(): void {
this.$http.post('/api/sign-up', {
name: this.name,
password: this.password
})
.then((response: any) => {
console.log(response)
})
}
}
</script>
我在我的main.ts 中注册 vue-resource,如下所示:
import Vue from "vue"
import router from "./router"
import App from "./app"
const VueResource = require('vue-resource')
Vue.use(VueResource)
new Vue({
el: "#app",
router,
template: "<App/>",
components: { App },
});
【问题讨论】:
-
如果您使用
import语句而不是require导入VueResource 会发生什么?我不使用打字稿,但$http是未定义的,在js 中,使用require时;import工作正常。 -
是的!做到了!太感谢了。由于一些较旧的错误,我不得不
require它,但现在它似乎工作了。您可以将您的评论转换为答案。祝你有美好的一天。
标签: javascript typescript vue.js vue-resource