将Vue.js与Typescript一起使用时如何使用vue-resource之类的插件?

Jul*_*ian 5 javascript typescript vue.js vue-resource

我开始使用Typescript并尝试将其应用于我的项目。但是,我无法像vue-resource那样使用Vue.js插件。

当我使用

this.$http.post()
Run Code Online (Sandbox Code Playgroud)

我得到错误:

错误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>
Run Code Online (Sandbox Code Playgroud)

我这样注册vue-resource main.ts

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 },
});
Run Code Online (Sandbox Code Playgroud)

Lui*_*duz 5

也可以import代替requireVueResource 使用。

import VueResource from 'vue-resource'
Run Code Online (Sandbox Code Playgroud)